Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. //Tic Tac Toe
  2. public class TicTacToe
  3. {
  4. //copy over your constructor from the Tic Tac Toe Board activity in the previous lesson!
  5. private int turn;
  6. private String [][] chess = new String [3][3];
  7.  
  8. public TicTacToe()
  9. {
  10. for(int i = 0; i < 3; i++)
  11. {
  12. for(int a = 0; a < 3; a++)
  13. {
  14. chess[i][a] = "-";
  15. }
  16. }
  17. }
  18. //this method returns the current turn
  19. public int getTurn()
  20. {
  21. return turn;
  22. }
  23.  
  24. /*This method prints out the board array on to the console
  25. */
  26. public void printBoard()
  27. {
  28. int a = 0;
  29. System.out.println(" " + "0" + " " + "1" + " " + "2");
  30.  
  31. for(int row = 0; row < chess.length; row++)
  32. {
  33. System.out.print(a + " ");
  34. for(int col = 0; col < chess[row].length; col++)
  35. {
  36. System.out.print(chess[row][col] + " ");
  37. }
  38. a++;
  39. System.out.println("");
  40. }
  41. }
  42.  
  43. //This method returns true if space row, col is a valid space
  44. public boolean pickLocation(int row, int col)
  45. {
  46. if(chess[row][col].equals("-"))
  47. {
  48. return true;
  49. }
  50. else
  51. {
  52. return false;
  53. }
  54. }
  55.  
  56. //This method places an X or O at location row,col based on the int turn
  57. public void takeTurn(int row, int col)
  58. {
  59. if(turn % 2 == 0)
  60. {
  61. chess[row][col] = "X";
  62. }
  63. else if(turn % 2 == 1)
  64. {
  65. chess[row][col] = "O";
  66. }
  67. turn++;
  68. }
  69.  
  70. //This method returns a boolean that returns true if a row has three X or O's in a row
  71. public boolean checkRow()
  72. {
  73. boolean a = false;
  74. for(int row = 0; row < chess.length; row++)
  75. {
  76. if(chess[row][0].equals(chess[row][1]) && chess[row][0].equals(chess[row][2]))
  77. {
  78. a = true;
  79. }
  80. }
  81. return a;
  82. }
  83.  
  84. //This method returns a boolean that returns true if a col has three X or O's
  85. public boolean checkCol()
  86. {
  87. boolean a = false;
  88. for(int col = 0; col < chess.length; col++)
  89. {
  90. if(chess[0][col].equals(chess[1][col]) && chess[0][col].equals(chess[2][col]))
  91. {
  92. a = true;
  93. }
  94. }
  95. return a;
  96. }
  97.  
  98. //This method returns a boolean that returns true if either diagonal has three X or O's
  99. public boolean checkDiag()
  100. {
  101. boolean a = false;
  102. if(chess[1][1].equals(chess[0][0]) && chess[1][1].equals(chess[2][2]))
  103. {
  104. a = true;
  105. }
  106. else if(chess[1][1].equals(chess[0][2]) && chess[1][1].equals(chess[2][0]))
  107. {
  108. a = true;
  109. }
  110. return a;
  111. }
  112.  
  113. //This method returns a boolean that checks if someone has won the game
  114. public boolean checkWin()
  115. {
  116. if(checkRow())
  117. {
  118. return true;
  119. }
  120. else if(checkCol())
  121. {
  122. return true;
  123. }
  124. else if(checkDiag())
  125. {
  126. return true;
  127. }
  128. else
  129. {
  130. return false;
  131. }
  132.  
  133. }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement