Foros del Web » Programación para mayores de 30 ;) » Java »

[Problema] Tres en Raya

Estas en el tema de [Problema] Tres en Raya en el foro de Java en Foros del Web. Bueno estaba haciendo el tres en Raya (Ejercicio Deitel) Tengo unos problemas: Clase "Tres en Raya" (reglas basicas del juego) @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código Java: Ver ...
  #1 (permalink)  
Antiguo 18/07/2012, 17:12
 
Fecha de Ingreso: junio-2012
Mensajes: 17
Antigüedad: 11 años, 9 meses
Puntos: 2
[Problema] Tres en Raya

Bueno estaba haciendo el tres en Raya (Ejercicio Deitel)
Tengo unos problemas:

Clase "Tres en Raya"(reglas basicas del juego)

Código Java:
Ver original
  1. package ochoOnce;
  2.  
  3. public class TresEnRaya {
  4.    
  5.     //tablero del juego
  6.     int tablero[][]=new int[3][3];
  7.     int j1=1;
  8.     int j2=2;
  9.     boolean turno1=true;
  10.     boolean enJuego=true;
  11.     String tImpreso="";
  12.     int Ganador;
  13.     //metodo inicializar tablero en 0;
  14.     public void inicializarTablero(){
  15.         for(int i=0;i<tablero.length;i++){
  16.             for (int j=0;i<tablero.length;i++){
  17.                 tablero[i][j]=0;
  18.             }
  19.         }//fin de for  
  20.     }//fin de "inicializarTablero
  21.     //Dibujar Tablero en String para el JTextArea
  22.     public String dibujarTablero(){
  23.         tImpreso="";
  24.         tImpreso+=tablero[0][0]+"\t"+tablero[0][1]+"\t"+tablero[0][2]+"\n\n\n";
  25.         tImpreso+=tablero[1][0]+"\t"+tablero[1][1]+"\t"+tablero[1][2]+"\n\n\n";
  26.         tImpreso+=tablero[2][0]+"\t"+tablero[2][1]+"\t"+tablero[2][2]+"\n\n\n";
  27.         return tImpreso;
  28.     }
  29.     //Metodo para Cambiar automaticamente de jugador 1 a jugador 2 
  30.     public void cambiarJugador(){
  31.         if (turno1){
  32.             turno1=false;
  33.         }else{
  34.             turno1=true;
  35.         }
  36.     }
  37.     //Verificia si el tablero recibio un "tres en raya"
  38.     public void juegoTerminado(){
  39.         if((tablero[0][0]==1 && tablero[0][1]==1 && tablero[0][2]==1)||(tablero[1][0]==1 && tablero[1][1]==1 && tablero[1][2]==1)||(tablero[2][0]==1 && tablero[2][1]==1 && tablero[2][2]==1)||(tablero[0][0]==1 && tablero[1][0]==1 && tablero[2][0]==1)||(tablero[0][1]==1 && tablero[1][1]==1 && tablero[2][1]==1)||(tablero[0][2]==1 && tablero[1][2]==1 && tablero[2][2]==1)||(tablero[0][0]==1 && tablero[1][1]==1 && tablero[2][2]==1)||(tablero[2][0]==1 && tablero[1][1]==1 && tablero[0][2]==1)){
  40.             enJuego=false;
  41.             Ganador=1;
  42.         }
  43.    
  44.         if((tablero[0][0]==2 && tablero[0][1]==2 && tablero[0][2]==2)||(tablero[1][0]==2 && tablero[1][1]==2 && tablero[1][2]==2)||(tablero[2][0]==2 && tablero[2][1]==2 && tablero[2][2]==2)||(tablero[0][0]==2 && tablero[1][0]==2 && tablero[2][0]==2)||(tablero[0][1]==2 && tablero[1][1]==2 && tablero[2][1]==2)||(tablero[0][2]==2 && tablero[1][2]==2 && tablero[2][2]==2)||(tablero[0][0]==2 && tablero[1][1]==2 && tablero[2][2]==2)||(tablero[2][0]==2 && tablero[1][1]==2 && tablero[0][2]==2)){
  45.             enJuego=false;
  46.             Ganador=2;
  47.         }
  48.         }//fin de juego terminado()
  49.     }

Clase "InitJuego"(determina cuantos jugadores)

Código Java:
Ver original
  1. package ochoOnce;
  2.  
  3. import javax.swing.JOptionPane;
  4.  
  5. public class InitJugadores extends TresEnRaya{
  6.     //dibujar String para Juego.AreaSalida
  7.     String tabla="";
  8. //Para jugador vs Jugador
  9. public void dosJugadores() {
  10.        
  11.         tabla=dibujarTablero();
  12.         int posX;
  13.         int posY;
  14.         boolean movPosible;
  15.        
  16.         do{
  17.            
  18.        if(turno1){
  19.             movPosible=false;
  20.             do{
  21.                 do{
  22.                     posX=((Integer.parseInt((String) JOptionPane.showInputDialog(null,"JUGADOR 1: que fila elige: ","",JOptionPane.INFORMATION_MESSAGE,null,new String[]{"1","2","3"},"1")))-1);
  23.                     }while (posX>3);
  24.                 do{
  25.                     posY=((Integer.parseInt((String) JOptionPane.showInputDialog(null,"JUGADOR 1: que columna elige:","",JOptionPane.INFORMATION_MESSAGE,null,new String[]{"1","2","3"},"1")))-1);
  26.                 }while (posY>3);
  27.                  if (tablero[posX][posY]!=0){
  28.                  JOptionPane.showMessageDialog(null, "JUGADOR 1: ingreso una posicion no valida, intente de nuevo");
  29.                  }else{
  30.                  tablero[posX][posY]=j1;
  31.                  movPosible=true;
  32.                  }
  33.             }while(!movPosible);
  34.             Juego.areaSalida.setText(dibujarTablero());
  35.             cambiarJugador();
  36.             juegoTerminado();
  37.         }else{
  38.             movPosible=false;
  39.             do{
  40.                 do{
  41.                     posX=((Integer.parseInt((String) JOptionPane.showInputDialog(null,"JUGADOR 2: que fila elige: ","",JOptionPane.INFORMATION_MESSAGE,null,new String[]{"1","2","3"},"1")))-1);
  42.                     }while (posX>3);
  43.                 do{
  44.                     posY=((Integer.parseInt((String) JOptionPane.showInputDialog(null,"JUGADOR 2: que columna elige:","",JOptionPane.INFORMATION_MESSAGE,null,new String[]{"1","2","3"},"1")))-1);
  45.                 }while (posY>3);
  46.                
  47.                  if (tablero[posX][posY]!=0){
  48.                  JOptionPane.showMessageDialog(null, "JUGADOR 2: ingreso una posicion no valida, intente de nuevo");
  49.                  }else{
  50.                  tablero[posX][posY]=j2;
  51.                  movPosible=true;
  52.                  }
  53.             }while(!movPosible);
  54.             Juego.areaSalida.setText(dibujarTablero());
  55.             cambiarJugador();
  56.             juegoTerminado();
  57.            
  58.         }
  59.     }while(enJuego==true);
  60.         JOptionPane.showMessageDialog(null, "Felicidades! JUGADOR "+Ganador);
  61. }
  62. //para jugador vs Maquina
  63. public void UnJugador(){
  64.  
  65.     tabla=dibujarTablero();
  66.     int posX;
  67.     int posY;
  68.     boolean movPosible;
  69.    
  70.     do{
  71.        
  72.    if(turno1){
  73.         movPosible=false;
  74.         do{
  75.             do{
  76.                 posX=((Integer.parseInt((String) JOptionPane.showInputDialog(null,"JUGADOR 1: que fila elige: ","",JOptionPane.INFORMATION_MESSAGE,null,new String[]{"1","2","3"},"1")))-1);
  77.                 }while (posX>3);
  78.             do{
  79.                 posY=((Integer.parseInt((String) JOptionPane.showInputDialog(null,"JUGADOR 1: que columna elige:","",JOptionPane.INFORMATION_MESSAGE,null,new String[]{"1","2","3"},"1")))-1);
  80.             }while (posY>3);
  81.              if (tablero[posX][posY]!=0){
  82.              JOptionPane.showMessageDialog(null, "JUGADOR 1: ingreso una posicion no valida, intente de nuevo");
  83.              }else{
  84.              tablero[posX][posY]=j1;
  85.              movPosible=true;
  86.              }
  87.         }while(!movPosible);
  88.         Juego.areaSalida.setText(dibujarTablero());
  89.         cambiarJugador();
  90.         juegoTerminado();
  91.     }else{
  92.         movPosible=false;
  93.         do{
  94.             posX=(int)(Math.random()*2);
  95.             posY=(int)(Math.random()*2);
  96.             if (tablero[posX][posY]!=0){
  97.              break;
  98.              }else{
  99.              tablero[posX][posY]=j2;
  100.              movPosible=true;
  101.              Juego.areaSalida.setText(dibujarTablero());
  102.                 cambiarJugador();
  103.                 juegoTerminado();
  104.              }
  105.         }while(!movPosible);
  106.     }
  107. }while(enJuego==true);
  108.     JOptionPane.showMessageDialog(null, "Felicidades! JUGADOR "+Ganador);
  109. }
  110.  
  111. }

Clase "Juego"
Código Java:
Ver original
  1. package ochoOnce;
  2.  
  3. import java.awt.Container;
  4. import java.awt.FlowLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7.  
  8. import javax.swing.JApplet;
  9. import javax.swing.JButton;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.JTextArea;
  12.  
  13. public class Juego extends JApplet implements ActionListener{
  14. //Parte Grafica
  15.     static JTextArea areaSalida;
  16.     JButton salir,dosJug,unJug,nuevoJuego;
  17. //Inicializo las dos Clases (no entiendo por que, si pongo extends deberia iniciar solo una
  18.     TresEnRaya uno=new TresEnRaya();
  19.     InitJugadores dos=new InitJugadores();
  20. //Inicializar la parte Grafica 
  21.     public void init(){
  22.         Container contenedor=getContentPane();
  23.         contenedor.setLayout(new FlowLayout());
  24.        
  25.         areaSalida=new JTextArea(10,30);
  26.         areaSalida.setEditable(false);
  27.         areaSalida.setVisible(true);
  28.         contenedor.add(areaSalida);
  29.        
  30.         nuevoJuego=new JButton("Nuevo Juego");
  31.         nuevoJuego.addActionListener(this);
  32.         nuevoJuego.setVisible(true);
  33.         contenedor.add(nuevoJuego);
  34.        
  35.         dosJug=new JButton("Dos Jugadores");
  36.         dosJug.addActionListener(this);
  37.         dosJug.setVisible(false);
  38.         contenedor.add(dosJug);
  39.        
  40.         unJug=new JButton("un jugador");
  41.         unJug.addActionListener(this);
  42.         unJug.setVisible(false);
  43.         contenedor.add(unJug);
  44.        
  45.         salir=new JButton("salir");
  46.         salir.addActionListener(this);
  47.         salir.setVisible(true);
  48.         contenedor.add(salir);
  49.         }
  50. //Ejecutar 
  51.     public void paint(){showStatus("si no pongo esto, no anda");}
  52. //accion de los botones
  53.     @Override
  54.     public void actionPerformed(ActionEvent evt) {
  55.         if (salir==evt.getSource()){System.exit(0);}
  56.        
  57.         if (nuevoJuego==evt.getSource()){
  58.             dos.tabla="";
  59.             uno.inicializarTablero();
  60.             areaSalida.setText(uno.dibujarTablero());
  61.             unJug.setVisible(true);
  62.             dosJug.setVisible(true);
  63.             nuevoJuego.setVisible(false);
  64.         }
  65.         if (dosJug==evt.getSource()){
  66.             uno.inicializarTablero();
  67.             areaSalida.setText(uno.dibujarTablero());
  68.             dos.dosJugadores();
  69.             nuevoJuego.setVisible(true);
  70.             unJug.setVisible(false);
  71.             dosJug.setVisible(false);
  72.             }
  73.         if (unJug==evt.getSource()){
  74.             uno.inicializarTablero();
  75.             areaSalida.setText(uno.dibujarTablero());
  76.             dos.UnJugador();
  77.             nuevoJuego.setVisible(true);
  78.             unJug.setVisible(false);
  79.             dosJug.setVisible(false);
  80.         }
  81.        
  82.     }
  83. }

Bueno el juego inicializa, al hacer click en nuevo juego salen las oopciones (1 jugador) y (2 jugadores)

1* Cuando le pongo 1 jugador va perfecto de hecho finaliza bien pero al poner nuevo juego, inicializa e imprime el tablero en 0, sin embargo al jugar tira movimiento no posible en una posicion 0 cualquiera.

2* Cuando juego con la maquina, si no termino en 3 movimientos el juego, el juego se tilda, no avanza.
(EDIT: ya lo solucione el punto 2)
supongo que el codigo es inentendible, son mis primeros pasos en oop :P si ven algo conceptualmente mal hecho, me dicen :P

gracias y disculpen las molestias

Última edición por Sephyneko; 18/07/2012 a las 17:49

Etiquetas: clase, jar, raya, string, tres
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:18.