Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3.  
  4. public class JuegoTateti
  5. {
  6. public static void main(String[] args)
  7. {
  8. int cont=0;
  9. Tateti t1=new Tateti();
  10. Jugador j1=new Jugador("juan","X",t1);
  11. Jugador j2=new Jugador("pedro","O",t1);
  12. t1.crearTablero();
  13. do
  14. {
  15.  
  16. t1.mostrarTablero();
  17. j1.Jugar();
  18. t1.mostrarTablero();
  19.  
  20. j2.Jugar();
  21. cont++;
  22.  
  23.  
  24. }while((t1.hayTateti()==false)&&(cont<8));
  25.  
  26. if(t1.hayTateti())
  27. {
  28. System.out.println("hay tateti");
  29. }
  30.  
  31.  
  32. }
  33. }
  34.  
  35. class Jugador
  36. {
  37. public Ficha fi;//=new Ficha("X");
  38. public String nombre;
  39. public Tateti t=null;
  40. public Jugador(String nombre,String fichaValor,Tateti t)
  41. {
  42. this.nombre=nombre;
  43. fi=new Ficha(fichaValor);
  44. this.t=t;
  45. }
  46.  
  47. public void Jugar()
  48. {
  49. int f=0,c=0;
  50. int pos=Integer.parseInt(JOptionPane.showInputDialog(null,"Ingrese posicion"));
  51. switch(pos)
  52. {
  53. case 7: f=0;c=0;break;
  54. case 8: f=0;c=1;break;
  55. case 9: f=0;c=2;break;
  56.  
  57. case 4: f=1;c=0;break;
  58. case 5: f=1;c=1;break;
  59. case 6: f=1;c=2;break;
  60.  
  61. case 1: f=2;c=0;break;
  62. case 2: f=2;c=1;break;
  63. case 3: f=2;c=2;break;
  64. }
  65.  
  66.  
  67.  
  68. if(!t.ponerFicha(f, c,fi))
  69. {
  70. System.out.println("jugada incorrecta");
  71. }
  72.  
  73.  
  74.  
  75. }
  76. }
  77.  
  78. class Ficha
  79. {
  80. public String valor="_";
  81. public Ficha(String v)
  82. {
  83. valor=v;
  84. }
  85. }
  86.  
  87. class Tateti
  88. {
  89. private Ficha tablero[][] = new Ficha[3][3];
  90.  
  91. public void crearTablero()
  92. {
  93. for(int f=0;f<tablero.length;f++)
  94. {
  95. for(int c=0;c<tablero[f].length;c++)
  96. {
  97. tablero[f][c]=new Ficha("_");
  98. }
  99. }
  100. }
  101.  
  102. public void mostrarTablero()
  103. {
  104. System.out.println(" ");
  105. for(int f=0;f<tablero.length;f++)
  106. {
  107. for(int c=0;c<tablero[f].length;c++)
  108. {
  109. System.out.print(tablero[f][c].valor+" ");
  110. }
  111. System.out.println(" ");
  112. }
  113. }
  114.  
  115. public boolean hayTateti()
  116. {
  117. //diago \
  118. if(( !(tablero[0][0].valor).equalsIgnoreCase("_") )&&((tablero[0][0].valor).equalsIgnoreCase(tablero[1][1].valor))&&((tablero[1][1].valor).equalsIgnoreCase(tablero[2][2].valor)) )
  119. {
  120. return(true);
  121. }
  122.  
  123. if( ( ( !(tablero[0][2].valor).equalsIgnoreCase("_") ) ) && ((tablero[0][2].valor).equalsIgnoreCase(tablero[1][1].valor))&&((tablero[1][1].valor).equalsIgnoreCase(tablero[2][0].valor)) )
  124. {
  125. return(true);
  126. }
  127.  
  128. //tateti filas
  129.  
  130. if(( !(tablero[0][0].valor).equalsIgnoreCase("_") )&&((tablero[0][0].valor).equalsIgnoreCase(tablero[0][1].valor))&&((tablero[0][1].valor).equalsIgnoreCase(tablero[0][2].valor)) )
  131. {
  132. return(true);
  133. }
  134.  
  135. if(( !(tablero[1][0].valor).equalsIgnoreCase("_") )&&((tablero[1][0].valor).equalsIgnoreCase(tablero[1][1].valor))&&((tablero[1][1].valor).equalsIgnoreCase(tablero[1][2].valor)) )
  136. {
  137. return(true);
  138. }
  139.  
  140. if(( !(tablero[2][0].valor).equalsIgnoreCase("_") )&&((tablero[2][0].valor).equalsIgnoreCase(tablero[2][1].valor))&&((tablero[2][1].valor).equalsIgnoreCase(tablero[2][2].valor)) )
  141. {
  142. return(true);
  143. }
  144.  
  145.  
  146. //tateti columnas
  147.  
  148. if(( !(tablero[0][0].valor).equalsIgnoreCase("_") )&&((tablero[0][0].valor).equalsIgnoreCase(tablero[1][0].valor))&&((tablero[1][0].valor).equalsIgnoreCase(tablero[2][0].valor)) )
  149. {
  150. return(true);
  151. }
  152.  
  153. if(( !(tablero[0][1].valor).equalsIgnoreCase("_") )&&((tablero[0][1].valor).equalsIgnoreCase(tablero[1][1].valor))&&((tablero[1][1].valor).equalsIgnoreCase(tablero[2][1].valor)) )
  154. {
  155. return(true);
  156. }
  157.  
  158. if(( !(tablero[0][2].valor).equalsIgnoreCase("_") )&&((tablero[0][2].valor).equalsIgnoreCase(tablero[1][2].valor))&&((tablero[1][2].valor).equalsIgnoreCase(tablero[2][2].valor)) )
  159. {
  160. return(true);
  161. }
  162.  
  163.  
  164. return(false);
  165. }
  166.  
  167. public boolean ponerFicha(int f,int c,Ficha fi)
  168. {
  169. if(tablero[f][c].valor.equalsIgnoreCase("_"))
  170. {
  171. tablero[f][c].valor=fi.valor;
  172. return(true);
  173. }
  174.  
  175. return(false);
  176. }
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement