Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. else {
  2. TicTacCollection.gatos.add(otro);
  3.  
  4. public JButton[][] board;
  5. private char currentPlayerMark;
  6. Tablero otro;
  7.  
  8. public Tablero() {
  9.  
  10. board = new JButton[3][3];
  11. currentPlayerMark = 'X';
  12. initComponents();
  13. initializeBoard();
  14. setLocationRelativeTo(null);
  15. }
  16.  
  17. private void initializeBoard() {
  18. board[0][0] = b1;
  19. board[0][1] = b2;
  20. board[0][2] = b3;
  21. board[1][0] = b4;
  22. board[1][1] = b5;
  23. board[1][2] = b6;
  24. board[2][0] = b7;
  25. board[2][1] = b8;
  26. board[2][2] = b9;
  27. }
  28.  
  29. private void reset(){
  30. for (int i = 0; i < 3; i++) {
  31. for (int j = 0; j < 3; j++) {
  32. board[i][j].setText("");
  33. }
  34. }
  35. }
  36.  
  37. public boolean isBoardFull() {
  38. boolean isFull = true;
  39.  
  40. for (int i = 0; i < 3; i++) {
  41. for (int j = 0; j < 3; j++) {
  42. if (board[i][j].getText().equals("")) {
  43. isFull = false;
  44. }
  45. }
  46. }
  47. return isFull;
  48. }
  49.  
  50. public boolean checkForWin() {
  51. boolean winner = (checkRowsForWin() || checkColumnsForWin() || checkDiagonalsForWin());
  52.  
  53. if (winner) {
  54. if (TicTacCollection.gatos.isEmpty()) {
  55. TicTacCollection.gatos.add(this);
  56. } else {
  57. TicTacCollection.gatos.add(this);
  58. }
  59. }
  60. return winner;
  61. }
  62.  
  63. private boolean checkRowsForWin() {
  64. for (int i = 0; i < 3; i++) {
  65. if (checkRowCol(board[i][0].getText(), board[i][1].getText(), board[i][2].getText()) == true) {
  66. return true;
  67. }
  68.  
  69. }
  70. return false;
  71. }
  72.  
  73. private boolean checkColumnsForWin() {
  74. for (int i = 0; i < 3; i++) {
  75. if (checkRowCol(board[0][i].getText(), board[1][i].getText(), board[2][i].getText()) == true) {
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81.  
  82. private boolean checkDiagonalsForWin() {
  83. return ((checkRowCol(board[0][0].getText(), board[1][1].getText(), board[2][2].getText()) == true) || (checkRowCol(board[0][2].getText(), board[1][1].getText(), board[2][0].getText()) == true));
  84. }
  85.  
  86. private boolean checkRowCol(String c1, String c2, String c3) {
  87. return ((c1.length() > 0) && (c1.equals(c2)) && (c2.equals(c3)));
  88. }
  89.  
  90. public void setMarca(JButton boton) {
  91. if (currentPlayerMark == 'X') {
  92. boton.setText("X");
  93. currentPlayerMark = 'O';
  94. } else {
  95. currentPlayerMark = 'X';
  96. boton.setText("O");
  97. }
  98.  
  99. if (checkForWin()){
  100. JOptionPane.showMessageDialog(null, "El ganador fue el jugador con la marca: " + boton.getText());
  101.  
  102. System.out.println("EN GATO HAY " + TicTacCollection.gatos.size());
  103. if (TicTacCollection.gatos.size() == 4) {
  104. TicTacCollection.showMatches();
  105. }
  106. if (TicTacCollection.gatos.size() >= 1 && TicTacCollection.gatos.size() < 4) {
  107.  
  108. otro = new Tablero();
  109. this.hide();
  110. otro.setVisible(true);
  111. otro.setTitle("otro tablero");
  112. }
  113.  
  114. } else if (isBoardFull()) {
  115. JOptionPane.showMessageDialog(null, "Empate");
  116. reset();
  117. }
  118.  
  119. }
  120. @SuppressWarnings("unchecked") //Código auto generado ...
  121.  
  122. /* Create and display the form */
  123. java.awt.EventQueue.invokeLater(new Runnable() {
  124. public void run() {
  125. Tablero tabla = new Tablero();
  126. tabla.setVisible(true);
  127.  
  128. }
  129. });
  130. }
  131.  
  132. //Cada uno de los nueve botones tiene su `ActionPerformed` donde se invoca `setMarca(boton)`
  133.  
  134. private void b1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN- FIRST:event_b1ActionPerformed
  135. setMarca(b1);
  136. }//GEN-LAST:event_b1ActionPerformed
  137.  
  138. public class TicTacCollection {
  139. public static ArrayList<Tablero> gatos = new ArrayList<Tablero>();
  140.  
  141. static int x =10,y=20;
  142.  
  143. public ArrayList<Tablero> devuelveGatos(){
  144. return gatos;
  145. }
  146.  
  147. public static void showMatches(){
  148.  
  149. System.out.println("Estas son las partidas y los resultados");
  150. for (int i = 0; i < gatos.size(); i++) {
  151.  
  152. if(i != 0){
  153. gatos.get(i).setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
  154. }
  155. gatos.get(i).setTitle("Gato"+i);
  156. gatos.get(i).setLocation(x,y);x+=286;
  157. gatos.get(i).setVisible(true);
  158. }
  159. }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement