Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.08 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TicTacToe
  4. {
  5. public static char[][] board = new char[3][3];
  6. public Scanner keyboard;
  7.  
  8.  
  9. public static void writeBoard()
  10. {
  11. System.out.println("-----------------");
  12. System.out.println("|R/C| 1 | 2 | 3 |");
  13. System.out.println("-----------------");
  14.  
  15. for (int i = 0; i < 3; i++) //i is the row number
  16. {
  17. System.out.println("| " + (i + 1) + " | " + board[i][0] + " | " + board[i][1] + " | " + board[i][2] + " |");
  18. System.out.println("-----------------");
  19. }
  20. }
  21.  
  22. /*Postcondition:
  23. Prints the TicTacToe board to screen with user moves in the correct rows and columns*/
  24.  
  25. /*Precondition:
  26. The board has been written and a new game has been started or O has just placed their move*/
  27.  
  28. public static void getMoveX()
  29. {
  30. System.out.println("X's turn.");
  31. System.out.println("Where would you like your X placed?");
  32. System.out.println("Please enter row number and column number separated by a space.");
  33. Scanner keyboard = new Scanner(System.in);
  34. String rowcol = keyboard.nextLine(); //user input assigned to rowcol
  35. int row = rowcol.charAt(0);
  36. int column = rowcol.charAt(2);
  37. System.out.println("You have entered row #" + (row - 48) + " and column #" + (column - 48)); /*prints the row and column that the user entered*/
  38.  
  39. if (board[(row - 49)][(column - 49)] != '\0') /*if the spot entered by the user is taken...*/
  40. {
  41. System.out.println("That cell is already taken.");
  42. System.out.println("Please make another selection.");
  43. getMoveX();
  44. }
  45.  
  46. else //if the spot is not taken...
  47. {
  48. System.out.println("Thank you for your selection.");
  49. board[row - 49][column - 49] = 'X'; //X assigned to user row and column
  50. winner(); //checks to see if X is winner
  51. }
  52. }
  53.  
  54. /*Postcondition:
  55. Gets move from player X and places it in the correct spot on the board, prints board to screen*/
  56.  
  57. /*Precondition:
  58. X has moved and has chosen a valid spot, there is no winner yet*/
  59.  
  60.  
  61. public static void getMoveO()
  62. {
  63. System.out.println("O's turn.");
  64. System.out.println("Where would you like your O placed?");
  65. System.out.println("Please enter row number and column number separated by a space.");
  66. Scanner keyboard = new Scanner(System.in);
  67. String rowcol1 = keyboard.nextLine(); //user input assigned to rowcol1
  68. int row1 = rowcol1.charAt(0);
  69. int column1 = rowcol1.charAt(2);
  70. System.out.println("You have entered row #" + (row1 - 48) + " and column #" + (column1 - 48)); /*prints the users row and column number*/
  71. row1--;
  72. column1--;
  73.  
  74. if (board[row1 - 48][column1 - 48] != '\0') /*if the row and column are taken...*/
  75. {
  76. System.out.println("That cell is already taken.");
  77. System.out.println("Please make another selection.");
  78. getMoveO(); //starts method over
  79. }
  80. else /*if row and column are not taken...*/
  81. {
  82. System.out.println("Thank you for your selection.");
  83. board[row1 - 48][column1 - 48] = 'O'; //O placed in designated row and column
  84. winner(); //checks for winner
  85. }
  86. }
  87.  
  88. /*Precondition:
  89. X or O have placed their move*/
  90.  
  91. public static boolean winner()
  92. {
  93. boolean winner;
  94.  
  95. /*checks rows to see if X is winner*/
  96. for (int i = 0; i < 3; i++)
  97. {
  98. if ((board[i][0] == 'X') && (board[i][0] == board[i][1]))
  99. {
  100. if (board[i][0] == board[i][2])
  101. {
  102. winner = true;
  103. System.out.println(board[i][0] + " IS THE WINNER!");
  104. writeBoard();
  105. System.exit(0);
  106. }
  107. else
  108. winner = false;
  109. }
  110.  
  111. else
  112. winner = false;
  113. }
  114.  
  115. /*checks columns to see if X is winner*/
  116. for (int i = 0; i < 3; i++)
  117. {
  118. if ((board[0][i] == 'X') && (board[0][i] == board[1][i]))
  119. {
  120. if (board[0][i] == board[2][i])
  121. {
  122. winner = true;
  123. System.out.println(board[0][i] + " IS THE WINNER!");
  124. writeBoard();
  125. System.exit(0);
  126. }
  127. else
  128. winner = false;
  129. }
  130.  
  131. else
  132. winner = false;
  133. }
  134.  
  135. /*checks diagonals to see if X is winner*/
  136. if ((board[0][0] == 'X') && (board[0][0] == board[1][1]))
  137. {
  138. if (board[0][0] == board[2][2])
  139. {
  140. winner = true;
  141. System.out.println(board[0][0] + " IS THE WINNER!");
  142. writeBoard();
  143. System.exit(0);
  144. }
  145. else
  146. winner = false;
  147. }
  148.  
  149. if ((board[0][2] == 'X') && (board[0][2] == board[1][1]))
  150. {
  151. if (board[1][1] == board[2][0])
  152. {
  153. winner = true;
  154. System.out.println(board[0][2] + " IS THE WINNER!");
  155. writeBoard();
  156. System.exit(0);
  157. }
  158. else
  159. winner = false;
  160. }
  161.  
  162. /*checks rows to see if O is winner*/
  163. for (int i = 0; i < 3; i++)
  164. {
  165. if ((board[i][0] == 'O') && (board[i][0] == board[i][1]))
  166. {
  167. if (board[i][0] == board[i][2])
  168. {
  169. winner = true;
  170. System.out.println(board[i][0] + " IS THE WINNER!");
  171. writeBoard();
  172. System.exit(0);
  173. }
  174. else
  175. winner = false;
  176. }
  177.  
  178. else
  179. winner = false;
  180. }
  181.  
  182. /*checks columns to see if O is winner*/
  183. for (int i = 0; i < 3; i++)
  184. {
  185. if ((board[0][i] == 'O') && (board[0][i] == board[1][i]))
  186. {
  187. if (board[0][i] == board[2][i])
  188. {
  189. winner = true;
  190. System.out.println(board[0][i] + " IS THE WINNER!");
  191. writeBoard();
  192. System.exit(0);
  193. }
  194. else
  195. winner = false;
  196. }
  197.  
  198. else
  199. winner = false;
  200. }
  201.  
  202. /*checks diagonals to see if O is winner*/
  203. if ((board[0][0] == 'O') && (board[0][0] == board[1][1]))
  204. {
  205. if (board[0][0] == board[2][2])
  206. {
  207. winner = true;
  208. System.out.println(board[0][0] + " IS THE WINNER!");
  209. writeBoard();
  210. System.exit(0);
  211. }
  212. else
  213. winner = false;
  214. }
  215.  
  216. if ((board[0][2] == 'O') && (board[0][2] == board[1][1]))
  217. {
  218. if (board[1][1] == board[2][0])
  219. {
  220. winner = true;
  221. System.out.println(board[0][2] + " IS THE WINNER!");
  222. writeBoard();
  223. System.exit(0);
  224. }
  225. else
  226. winner = false;
  227. }
  228. else
  229. winner = false;
  230.  
  231. return winner; //true or false
  232. }
  233.  
  234. public static void newGame()
  235. {
  236. /*assigns null characters to all indexes in the array*/
  237. for (int i = 0; i < 3; i++)
  238. {
  239. board[i][0] = '\0';
  240. board[i][1] = '\0';
  241. board[i][2] = '\0';
  242. }
  243.  
  244. System.out.println("New Game: X goes first.");
  245. }
  246.  
  247. /*Postcondition:
  248. Prints board to screen with blank board*/
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement