ayuda con programa

Rommel
31 de Enero del 2006
compila pero no corre les agradezco su ayuda lo mas rapido posible puesto q lo necesito para mañana GRACIAS!!

import javax.swing.*;
import java.io.*;
import java.awt.*;
class CPrueba
{
static int cantidad,cuantos=0,pelota=6;
public static void main(String args[])throws IOException
{
int matriz[][]=new int[6][6];
String datos[][]=new String[50][15];
String[] alternativasM={"Número de ambiente","Posición inicial"};
cuantos=LeerArchivo(datos);
int seguir=0;
while (seguir==0)
{
String[] alternativas={"Eliminar","Actualizar","Buscar","Crear","Ver Reporte","Jugar"};
int opcion=JOptionPane.showOptionDialog(null,"¿Qué desea hacer?","Manejo de ambientes",JOptionPane.YES_NO_OPTION,JOptionPane.PLAIN_MESSAGE,null,alternativas,"Ninguna");
switch (opcion)
{
case 0://eliminar registro
EliminarRegistro(datos,alternativasM);
break;
case 1://actualizar
ActualizarRegistro(datos,alternativasM);
break;
case 2://buscar
BuscarRegistro(datos,alternativasM);
break;
case 3://crear registro
CrearRegistro(datos);
break;
case 4://ver reporte
VerReporte(datos,alternativasM);
break;
case 5://jugar
{
int numFila=ambiente();
recorrido(datos,matriz,numFila);
movida(datos,matriz,numFila);
break;
}
}
seguir=JOptionPane.showConfirmDialog(null,"¿Desea volver al menu principal?","Manejo de ambientes",JOptionPane.YES_NO_OPTION);
}
JOptionPane.showMessageDialog(null,"Usted ha cerrado el programa","Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
}

static int LeerArchivo(String dat[][])throws IOException
{
int a=0,b=0;
String st="";
File entrada=new File("ambientes.txt");
FileReader input=new FileReader(entrada);
int c=input.read();
while(c!=-1)
{
while(c!='n')
{
if(c!=';')
{
st+=(char)c;
}
else
{
dat[a][b]=st;
b++;
st="";
}
c=input.read();
}
dat[a][b]=st;
a++;
b=0;
st="";
c=input.read();
}
int cuant=a;
cantidad=cuant+1;
return cuant;
}

static void EliminarRegistro(String dat[][],String alt[])throws IOException
{
int como=JOptionPane.showOptionDialog(null,"¿Qué parámetro desea utilizar para buscar?","Manejo de ambientes",JOptionPane.YES_NO_OPTION,JOptionPane.PLAIN_MESSAGE,null,alt,"Ninguna");
String parametro=PreguntarComo(como,dat);
for(int a=0;a<cuantos;a++)
{
if(dat[a][como].equals(parametro))
{
dat[a][0]="";
dat[a][1]="";
dat[a][2]="";
dat[a][3]="";
dat[a][4]="";
dat[a][5]="";
dat[a][6]="";
dat[a][7]="";
dat[a][8]="";
dat[a][9]="";
dat[a][10]="";
dat[a][11]="";
dat[a][12]="";
dat[a][13]="";
dat[a][14]="";
break;
}
}

for(int a=0;a<cuantos;a++)
{
if(dat[a][0].equals(""))
{
dat[a][0]=dat[a+1][0];
dat[a][1]=dat[a+1][1];
dat[a][2]=dat[a+1][2];
dat[a][3]=dat[a+1][3];
dat[a][4]=dat[a+1][4];
dat[a][5]=dat[a+1][5];
dat[a][6]=dat[a+1][6];
dat[a][7]=dat[a+1][7];
dat[a][8]=dat[a+1][8];
dat[a][9]=dat[a+1][9];
dat[a][10]=dat[a+1][10];
dat[a][11]=dat[a+1][11];
dat[a][12]=dat[a+1][12];
dat[a][13]=dat[a+1][13];
dat[a][14]=dat[a+1][14];
dat[a+1][0]="";
dat[a+1][1]="";
dat[a+1][2]="";
dat[a+1][3]="";
dat[a+1][4]="";
dat[a+1][5]="";
dat[a+1][6]="";
dat[a+1][7]="";
dat[a+1][8]="";
dat[a+1][9]="";
dat[a+1][10]="";
dat[a+1][11]="";
dat[a+1][12]="";
dat[a+1][13]="";
dat[a+1][14]="";
break;
}
}
cuantos--;
escribirDatos(dat);
}

static void ActualizarRegistro(String dat[][],String alt[])throws IOException
{
int como=JOptionPane.showOptionDialog(null,"¿Qué parámetro desea utilizar para buscar?","Manejo de ambientes",JOptionPane.YES_NO_OPTION,JOptionPane.PLAIN_MESSAGE,null,alt,"Ninguna");
String parametro=PreguntarComo(como,dat);
int seguir=0;
while(seguir==0)
{
String[] opciones={"Número de ambiente","Posición Inicial","Dirección"};
int que=JOptionPane.showOptionDialog(null,"¿Qué desea actualizar?","Manejo de ambientes",JOptionPane.YES_NO_OPTION,JOptionPane.PLAIN_MESSAGE,null,opciones,"Ninguna");
int aa=0;
for(int a=0;a<cuantos;a++)
{
if(dat[a][como].equals(parametro))
{
aa=a;
}
}
int donde=0;
String queCosa="";
if(que==0)
{
queCosa="Número de ambiente";
donde=0;
}
else if(que==1)
{
queCosa="Posición inicial";
donde=1;
}
else if(que==2)
{
queCosa="Dirección";
donde=2;
}
dat[aa][donde]=JOptionPane.showInputDialog(null,"Introduzca el nuevo "+queCosa+" para el registro","Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
seguir=JOptionPane.showConfirmDialog(null,"¿Desea actualizar otro dato del mismo ambiente?","Manejo de ambientes",JOptionPane.YES_NO_OPTION);
}
escribirDatos(dat);
}

static void BuscarRegistro (String dat[][],String alt[])
{
int como=JOptionPane.showOptionDialog(null,"¿Qué parámetro desea utilizar para buscar?","Campamento Aconcagua",JOptionPane.YES_NO_OPTION,JOptionPane.PLAIN_MESSAGE,null,alt,"Ninguna");
String parametro=PreguntarComo(como,dat);
int quien=0;
for(int a=0;a<cuantos;a++)
{
if(dat[a][como].equals(parametro))
quien=a;
}
MostrarRegistro(dat,quien);
}

static void CrearRegistro(String dat[][])throws IOException
{
dat[cuantos][0]=JOptionPane.showInputDialog(null,"Introduzca el número de ambiente","Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
dat[cuantos][1]=JOptionPane.showInputDialog(null,"Introduzca la posición inicial","Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
dat[cuantos][2]=JOptionPane.showInputDialog(null,"Introduzca la dirección inicial","Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
dat[cuantos][3]=JOptionPane.showInputDialog(null,"Pelota número 1","Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
dat[cuantos][4]=JOptionPane.showInputDialog(null,"Pelota número 2","Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
dat[cuantos][5]=JOptionPane.showInputDialog(null,"Pelota número 3","Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
dat[cuantos][6]=JOptionPane.showInputDialog(null,"Pelota número 4","Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
dat[cuantos][7]=JOptionPane.showInputDialog(null,"Pelota número 5","Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
dat[cuantos][8]=JOptionPane.showInputDialog(null,"Pelota número 6","Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
dat[cuantos][9]=JOptionPane.showInputDialog(null,"Pared número 1","Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
dat[cuantos][10]=JOptionPane.showInputDialog(null,"Pared número 2","Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
dat[cuantos][11]=JOptionPane.showInputDialog(null,"Pared número 3","Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
dat[cuantos][12]=JOptionPane.showInputDialog(null,"Pared número 4","Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
dat[cuantos][13]=JOptionPane.showInputDialog(null,"Pared número 5","Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
dat[cuantos][14]=JOptionPane.showInputDialog(null,"Pared número 6","Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
cuantos++;
escribirDatos(dat);
}

static void VerReporte(String dat[][],String alt[])
{
String[] opciones={"Todo","Un ambiente"};
int comoR=JOptionPane.showOptionDialog(null,"Desea ver un registro de: ","Manejo de ambientes",JOptionPane.YES_NO_OPTION,JOptionPane.PLAIN_MESSAGE,null,opciones,"Ninguna");
if(comoR==0)
{
MostrarMatriz(dat);
}
else
{
if(comoR==1)
{
int comoB=JOptionPane.showOptionDialog(null,"¿Qué parámetro desea utilizar para buscar?","Manejo de ambientes",JOptionPane.YES_NO_OPTION,JOptionPane.PLAIN_MESSAGE,null,alt,"Ninguna");
String parametro=PreguntarComo(comoB,dat);
int quien=0;
for(int a=0;a<cuantos;a++)
{
if(dat[a][comoB].equals(parametro))
quien=a;
}
MostrarRegistro(dat,quien);
}
}
}

static String PreguntarComo(int com,String dat[][])
{
String parametro="";
boolean valido=false;
while(valido==false)
{
switch(com)
{
case 0://número de ambiente
parametro=JOptionPane.showInputDialog(null,"Introduzca el número de ambiente","Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
break;
case 1://posición inicial
parametro=JOptionPane.showInputDialog(null,"Introduzca la posición inicial","Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
break;
}
for(int a=0;a<cuantos;a++)
{
if(dat[a][com].equals(parametro))
valido=true;
}
if(valido==false)
JOptionPane.showMessageDialog(null,"¡Dato introducido no fue encontrado!","Robot Karel",JOptionPane.PLAIN_MESSAGE);
}
return parametro;
}

static void MostrarMatriz(String da[][])
{
JTextArea areaSalida=new JTextArea();
String salida="";
for(int a=0;a<cuantos;a++)
{
for(int b=0;b<15;b++)
{
salida+=da[a][b]+"tr";
}
salida+="nn";
}
areaSalida.setText(salida);
JOptionPane.showMessageDialog(null,areaSalida,"Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
}

static void MostrarRegistro(String dat[][],int qui)
{
JTextArea areaSalida=new JTextArea();
String salida="";
for(int b=0;b<15;b++)
{
salida+=dat[qui][b]+"t";
}
areaSalida.setText(salida);
JOptionPane.showMessageDialog(null,areaSalida,"Manejo de ambientes",JOptionPane.PLAIN_MESSAGE);
}

static int ambiente()
{
int nFila=Integer.parseInt(JOptionPane.showInputDialog("¿Qué ambiente desea utilizar?n(número de fila)"));
return nFila;
}

static void recorrido(String dat[][],int mat[][],int nFila)throws IOException
{
int x,y,x1,y1;
String pelota,pared;
for(int i=3;i<=8;i++)
{
pelota=dat[nFila-1][i];
x=Integer.parseInt(pelota.substring(2,3));
y=Integer.parseInt(pelota.substring(4));
mat[x][y]=2;
}
for(int j=9;j<=14;j++)
{
pared=dat[nFila-1][j];
x1=Integer.parseInt(pared.substring(2,3));
y1=Integer.parseInt(pared.substring(4,5));
mat[x1][y1]=1;
}
}

static void movida(String dat[][],int mat[][],int nFila)throws IOException
{
String posInic=dat[nFila-1][1];
int x2=Integer.parseInt(posInic.substring(0,1));
int y2=Integer.parseInt(posInic.substring(2));
int posInic2=mat[x2][y2];
mat[x2][y2]=5;
String direccion;
direccion=dat[nFila-1][2];
int mov,pos;
mostrarArreglo(mat);
while(pelota!=0)
{
while(direccion.equalsIgnoreCase("Norte"))
{
pos=mat[x2-1][y2];
if(pos==2)
{
JOptionPane.showMessageDialog(null,"¡Te has encontrado con una pelota!","¡¡¡PELOTA!!!",JOptionPane.PLAIN_MESSAGE);
pelota--;
mat[x2][y2]=5;
mostrarArreglo(mat);
}
else if(pos==1)
{
JOptionPane.showMessageDialog(null,"¡Te has encontrado con una pared!","¡¡¡PARED!!!",JOptionPane.WARNING_MESSAGE);
mostrarArreglo(mat);
direccion="Oeste";
pos=mat[x2][y2-1];
if(pos==2)
{
JOptionPane.showMessageDialog(null,"¡Te has encontrado con una pelota!","¡¡¡PELOTA!!!",JOptionPane.PLAIN_MESSAGE);
pelota--;
mat[x2][y2]=5;
mostrarArreglo(mat);
}
else if(pos==1)
{
JOptionPane.showMessageDialog(null,"¡Te has encontrado con una pared!","¡¡¡PARED!!!",JOptionPane.WARNING_MESSAGE);
pos=mat[x2+1][y2];
direccion="Sur";
mostrarArreglo(mat);
}
else
{
direccion="Oeste";
pos=mat[x2][y2-1];
mat[x2][y2-1]=5;
mostrarArreglo(mat);
}
}
else
{
direccion="Norte";
pos=mat[x2-1][y2];
mat[x2-1][y2]=5;
mostrarArreglo(mat);
}
}
while(direccion.equalsIgnoreCase("Este"))
{
pos=mat[x2][y2+1];
if(pos==2)
{
JOptionPane.showMessageDialog(null,"¡Te has encontrado con una pelota!","¡¡¡PELOTA!!!",JOptionPane.PLAIN_MESSAGE);
pelota--;
mat[x2][y2]=5;
mostrarArreglo(mat);
}
else if(pos==1)
{
JOptionPane.showMessageDialog(null,"¡Te has encontrado con una pared!","¡¡¡PARED!!!",JOptionPane.WARNING_MESSAGE);
direccion="Norte";
pos=mat[x2-1][y2];
mostrarArreglo(mat);
if(pos==2)
{
JOptionPane.showMessageDialog(null,"¡Te has encontrado con una pelota!","¡¡¡PELOTA!!!",JOptionPane.PLAIN_MESSAGE);
pelota--;
mat[x2-1][y2]=5;
mostrarArreglo(mat);
}
else if(pos==1)
{
JOptionPane.showMessageDialog(null,"¡Te has encontrado con una pared!","¡¡¡PARED!!!",JOptionPane.WARNING_MESSAGE);
pos=mat[x2][y2-1];
direccion="Oeste";
mostrarArreglo(mat);
}
else
{
direccion="Norte";
pos=mat[x2-1][y2];
mat[x2-1][y2]=5;
mostrarArreglo(mat);
}
}
else
{
direccion="Este";
pos=mat[x2][y2+1];
mat[x2][y2+1]=5;
mostrarArreglo(mat);
}
}
while(direccion.equalsIgnoreCase("Sur"))
{
pos=mat[x2+1][y2];
if(pos==2)
{
JOptionPane.showMessageDialog(null,"¡Te has encontrado con una pelota!","¡¡¡PELOTA!!!",JOptionPane.PLAIN_MESSAGE);
pelota--;
mat[x2+1][y2]=5;
mostrarArreglo(mat);
}
else if(pos==1)
{
JOptionPane.showMessageDialog(null,"¡Te has encontrado con una pared!","¡¡¡PARED!!!",JOptionPane.WARNING_MESSAGE);
mostrarArreglo(mat);
direccion="Este";
pos=mat[x2][y2+1];
if(pos==2)
{
JOptionPane.showMessageDialog(null,"¡Te has encontrado con una pelota!","¡¡¡PELOTA!!!",JOptionPane.PLAIN_MESSAGE);
pelota--;
mat[x2][y2+1]=5;
mostrarArreglo(mat);
}
else if(pos==1)
{
JOptionPane.showMessageDialog(null,"¡Te has encontrado con una pared!","¡¡¡PARED!!!",JOptionPane.WARNING_MESSAGE);
pos=mat[x2-1][y2];
direccion="Norte";
mostrarArreglo(mat);
}
else
{
direccion="Este";
pos=mat[x2][y2+1];
mat[x2][y2+1]=5;
mostrarArreglo(mat);
}
}
else
{
direccion="Sur";
pos=mat[x2][y2+1];
mat[x2][y2+1]=5;
mostrarArreglo(mat);
}
}
while(direccion.equalsIgnoreCase("Oeste"))
{
pos=mat[x2][y2-1];
if(pos==2)
{
JOptionPane.showMessageDialog(null,"¡Te has encontrado con una pelota!","¡¡¡PELOTA!!!",JOptionPane.PLAIN_MESSAGE);
pelota--;
mat[x2][y2-1]=5;
mostrarArreglo(mat);
}
else if(pos==1)
{
JOptionPane.showMessageDialog(null,"¡Te has encontrado con una pared!","¡¡¡PARED!!!",JOptionPane.WARNING_MESSAGE);
direccion="Sur";
pos=mat[x2+1][y2];
mostrarArreglo(mat);
if(pos==2)
{
JOptionPane.showMessageDialog(null,"¡Te has encontrado con una pelota!","¡¡¡PELOTA!!!",JOptionPane.PLAIN_MESSAGE);
pelota--;
mat[x2+1][y2]=5;
mostrarArreglo(mat);
}
else if(pos==1)
{
JOptionPane.showMessageDialog(null,"¡Te has encontrado con una pared!","¡¡¡PARED!!!",JOptionPane.WARNING_MESSAGE);
pos=mat[x2][y2+1];
direccion="Este";
mostrarArreglo(mat);
}
else
{
direccion="Sur";
pos=mat[x2+1][y2];
mat[x2-1][y2]=5;
mostrarArreglo(mat);
}
}
else
{
direccion="Oeste";
pos=mat[x2][y2+1];
mat[x2][y2+1]=5;
mostrarArreglo(mat);
}
}
}
}

static void mostrarArreglo(int mat[][])
{
String salida="";
JTextArea areaSalida=new JTextArea();
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
{
salida+=mat[i][j]+" -------- ";
}
salida+="n";
}
areaSalida.setText(salida);
JOptionPane.showMessageDialog(null,areaSalida,"Matriz de movimiento: Robot Karel",JOptionPane.PLAIN_MESSAGE);
}

static void escribirDatos(String dat[][])throws IOException
{
File outputFile=new File("ambientes.txt");
FileWriter out=new FileWriter(outputFile);//escribe
String str="";
for(int i=0;i<cuantos;i++)
{
for(int j=0;j<15;j++)
{
str=str+dat[i][j]+";";
}
str=str.substring(0,str.length()-1);
str=str+"rn";
}
out.write(str);//escribo en el archivo el contenido del nuevo arreglo
out.close();//cierro archivo de salida
}
}