Ver Mensaje Individual
  #3 (permalink)  
Antiguo 23/12/2011, 15:28
Avatar de s00rk
s00rk
 
Fecha de Ingreso: octubre-2010
Ubicación: Mexico
Mensajes: 238
Antigüedad: 13 años, 6 meses
Puntos: 48
Respuesta: Construyendo un 3 en raya en C++

La verdad no lei mucho el codigo puesto que si se me hizo muy am enredoso asi jeje, pero recorde que habia hecho algo asi hace tiempo y pues aqui te lo dejo si deceas:

TresRaya.java
Código Javascript:
Ver original
  1. package TresEnRaya;
  2. import java.util.*;
  3.  
  4. public class TresRaya
  5. {
  6.    
  7.     private char  [][] tablero = new char[3][3];
  8.     private int jugador = 1;
  9.     private char clave;
  10.     private boolean HayDisponibles;
  11.     private boolean Ganador = false;
  12.     private Scanner leer = new Scanner(System.in);
  13.    
  14.     public TresRaya()
  15.     {
  16.         for(int x = 0; x < 3; x++)
  17.             for(int xx = 0; xx < 3; xx++)
  18.                 tablero[x][xx] = 'T';
  19.     }
  20.    
  21.     public void jugar()
  22.     {
  23.         do{
  24.             if(jugador == 1)
  25.             {
  26.                 SOP("Turno Jugador 1\n");
  27.                 jugador = 2;
  28.                 clave = 'X';
  29.             }else{
  30.                 SOP("Turno Jugador 2\n");
  31.                 jugador = 1;
  32.                 clave = 'O';
  33.             }
  34.            
  35.             MostrarTabla();
  36.             SOP("\nPosiciones Disponibles");           
  37.             SOP("\n");
  38.             int [] pos = Posiciones();
  39.             tablero[pos[0]][pos[1]] = clave;
  40.             checar();
  41.         }while(HayDisponibles && !Ganador);
  42.         if(!Ganador)
  43.             SOP("Empate No Hubo Ganadores!!");
  44.            
  45.     }
  46.    
  47.     private void ChecarGanador()
  48.     {
  49.         char X = 'X', O = 'O';
  50.         if((tablero[0][0] == X && tablero[0][1] == X && tablero[0][2] == X) ||
  51.                 (tablero[1][0] == X && tablero[1][1] == X && tablero[1][2] == X) ||
  52.                 (tablero[2][0] == X && tablero[2][1] == X && tablero[2][2] == X) ||
  53.                 (tablero[0][0] == X && tablero[1][1] == X && tablero[2][2] == X) ||
  54.                 (tablero[0][2] == X && tablero[1][1] == X && tablero[2][0] == X) ||
  55.                 (tablero[0][0] == X && tablero[1][0] == X && tablero[1][0] == X) ||
  56.                 (tablero[1][0] == X && tablero[1][1] == X && tablero[1][2] == X) ||
  57.                 (tablero[2][0] == X && tablero[2][1] == X && tablero[2][2] == X))
  58.         {
  59.             SOP("Jugador 1 ha ganado!!");
  60.             Ganador = true;
  61.         }
  62.        
  63.         if((tablero[0][0] == O && tablero[0][1] == O && tablero[0][2] == O) ||
  64.                 (tablero[1][0] == O && tablero[1][1] == O && tablero[1][2] == O) ||
  65.                 (tablero[2][0] == O && tablero[2][1] == O && tablero[2][2] == O) ||
  66.                 (tablero[0][0] == O && tablero[1][1] == O && tablero[2][2] == O) ||
  67.                 (tablero[0][2] == O && tablero[1][1] == O && tablero[2][0] == O) ||
  68.                 (tablero[0][0] == O && tablero[1][0] == O && tablero[1][0] == O) ||
  69.                 (tablero[1][0] == O && tablero[1][1] == O && tablero[1][2] == O) ||
  70.                 (tablero[2][0] == O && tablero[2][1] == O && tablero[2][2] == O))
  71.         {
  72.             SOP("Jugador 2 ha ganado!!");
  73.             Ganador = true;
  74.         }
  75.        
  76.     }
  77.    
  78.     private void checar()
  79.     {
  80.         ChecarGanador();
  81.         if(!Ganador)
  82.         {
  83.             HayDisponibles = false;
  84.             for(int x = 0; x < 3; x++)
  85.                 for(int xx = 0; xx < 3; xx++)
  86.                     if(tablero[x][xx] == 'T')
  87.                         HayDisponibles = true;
  88.         }
  89.     }
  90.    
  91.     private void MostrarTabla()
  92.     {
  93.         for(int x = 0; x < 3; x++)
  94.         {
  95.             for(int xx = 0; xx < 3; xx++)
  96.             {
  97.                 System.out.print(tablero[x][xx]);
  98.                 if( xx < 2)
  99.                     System.out.print("|");
  100.             }
  101.             System.out.println();
  102.         }
  103.     }
  104.    
  105.     private int [] Posiciones()
  106.     {
  107.         int [][] resp = new int[10][2];
  108.         int cont = 0;
  109.         for(int x = 0; x < 3; x++)
  110.         {
  111.             for(int xx = 0; xx < 3; xx++)
  112.             {
  113.                 if(tablero[x][xx] == 'T')
  114.                 {
  115.                     cont++;
  116.                     SOP(cont + ".- [" + x + "][" + xx + "]");
  117.                     resp[cont][0] = x;
  118.                     resp[cont][1] = xx;
  119.                 }
  120.             }
  121.         }
  122.         int ver = -1;
  123.         do{
  124.             SOP("\nQue Posicion deceas?");
  125.             ver = leer.nextInt();
  126.         }while(ver <= 0 || ver > cont);
  127.         int [] devolver = { resp[ver][0], resp[ver][1] };
  128.         return devolver;
  129.     }
  130.    
  131.     public void SOP(String msj)
  132.     {
  133.         System.out.println(msj);
  134.     }
  135.  
  136. }

Principal.java
Código Javascript:
Ver original
  1. package TresEnRaya;
  2.  
  3. public class Principal
  4. {
  5.  
  6.     public static void main(String[] args)
  7.     {
  8.         TresRaya Juego = new TresRaya();
  9.         Juego.jugar();
  10.     }
  11.  
  12. }