Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.91 KB | None | 0 0
  1. // This program sets up a TicTacToe board and allows two people
  2. // to play the game
  3. // https://en.wikipedia.org/wiki/Tic-tac-toe
  4.  
  5. import java.util.*;
  6.  
  7.  
  8. public class TicTacToe extends Board
  9. {
  10. public static Scanner in;
  11. public static int board[][] = new int [3][3];
  12. public static String turn;
  13.  
  14.  
  15. public static void main(String[] args)
  16. {
  17. // instantiate a new TicTacToe object to call the start method
  18.  
  19. tictac.start();
  20.  
  21. }
  22.  
  23. // need a non-static method to be able to implement the abstract class
  24. public void start()
  25. {
  26. in = new Scanner (System.in);
  27. String playAgain = "n";
  28.  
  29. do {
  30. String board = new String[9];
  31. turn = "X";
  32. String winner = null;
  33. populateEmptyBoard();
  34.  
  35. System.out.println("Welcome to Multi-Player Tic-Tac-Toe!!!");
  36. System.out.println("--------------------------------------");
  37.  
  38. super.printBoard();
  39.  
  40. System.out.println();
  41. System.out.println("X plays first. Enter a space number to place X in:");
  42.  
  43. // keep looping until a winner is discovered or the game is a draw
  44. while (winner == null) {
  45. int numInput;
  46.  
  47. try {
  48. numInput = in.nextInt();
  49.  
  50. if (!(numInput > 0 && numInput <= 9)) {
  51. System.out.println("Error: Re-enter slot number:");
  52. continue;
  53. }
  54. } catch (InputMismatchException e) {
  55. System.out.println("InputMismatchException: Re-enter slot number:");
  56. in.nextLine();
  57. continue;
  58. }
  59.  
  60. System.out.println();
  61. if (board[0][0].equals(String.valueOf(numInput)))
  62. {
  63. board[0][0] = turn;
  64. checkTurn();
  65. winner = checkWinner();
  66.  
  67. }
  68. else if (board[0][1].equals(String.valueOf(numInput)))
  69. {
  70. board[0][1] = turn;
  71. checkTurn();
  72. winner = checkWinner();
  73.  
  74. }
  75. else if (board[0][2].equals(String.valueOf(numInput)))
  76. {
  77.  
  78. board[0][2] = turn;
  79. checkTurn();
  80. winner = checkWinner();
  81. }
  82. else if (board[1][0].equals(String.valueOf(numInput)))
  83. {
  84.  
  85. board[1][0] = turn;
  86. checkTurn();
  87. winner = checkWinner();
  88. }
  89. else if (board[1][1].equals(String.valueOf(numInput)))
  90. {
  91. board[1][1] = turn;
  92. checkTurn();
  93. winner = checkWinner();
  94. }
  95. else if (board[1][2].equals(String.valueOf(numInput)))
  96. {
  97. board[1][2] = turn;
  98. checkTurn();
  99. winner = checkWinner();
  100. } else if (board[2][0].equals(String.valueOf(numInput)))
  101. {
  102. board[2][0] = turn;
  103. checkTurn();
  104. winner = checkWinner();
  105. }
  106. else if (board[2][1].equals(String.valueOf(numInput)))
  107. {
  108.  
  109. board[2][1] = turn;
  110. checkTurn();
  111. winner = checkWinner();
  112.  
  113. }
  114. else if (board[2][2].equals(String.valueOf(numInput)))
  115. {
  116. board[2][2] = turn;
  117. checkTurn();
  118. winner = checkWinner();
  119.  
  120. } else {
  121. System.out.println("Slot already taken; re-enter slot number:");
  122. continue;
  123. }
  124. }
  125.  
  126. System.out.println();
  127. if (winner.equalsIgnoreCase("draw"))
  128. {
  129. System.out.println("It's a draw! Thanks for playing.");
  130. } else
  131. {
  132. System.out.println("Congratulations! " + winner + "'s have won! Thanks for playing.");
  133. }
  134.  
  135. System.out.println();
  136. System.out.println(" ?");
  137. playAgain = in.nextChar();
  138. } while(playAgain);
  139. }
  140.  
  141. // Change turn to the other player
  142. public void checkTurn()
  143. {
  144. if (turn.equals("X"))
  145. {
  146. turn = "O";
  147. } else
  148. {
  149. turn = "X";
  150. }
  151. }
  152.  
  153. // determines if there is a winner of the current board
  154. public String checkWinner()
  155. {
  156. for (int i = 0; i < 8; i++)
  157. {
  158. String line = null;
  159.  
  160. // check each directional line of the board to see who wins - use ifs or switch
  161. //new
  162. switch (a) {
  163. case 0:
  164. line = board[0] + board[1] + board[2];
  165. break;
  166. case 1:
  167. line = board[3] + board[4] + board[5];
  168. break;
  169. case 2:
  170. line = board[6] + board[7] + board[8];
  171. break;
  172. case 3:
  173. line = board[0] + board[3] + board[6];
  174. break;
  175. case 4:
  176. line = board[1] + board[4] + board[7];
  177. break;
  178. case 5:
  179. line = board[2] + board[5] + board[8];
  180. break;
  181. case 6:
  182. line = board[0] + board[4] + board[8];
  183. break;
  184. case 7:
  185. line = board[2] + board[4] + board[6];
  186. break;
  187. //new
  188. }
  189.  
  190.  
  191. // builds a String line representation to see if there is a winner
  192. if (line.equals("XXX"))
  193. {
  194. return "X";
  195. } else if (line.equals("OOO"))
  196. {
  197. return "O";
  198. }
  199. }
  200.  
  201. //double check that the board is full and leads to a draw
  202. for (int i = 0; i < 9; i++)
  203. {
  204. if (Arrays.asList(board[0]).contains(String.valueOf(i+1))) {
  205. break;
  206. }
  207. else if (Arrays.asList(board[1]).contains(String.valueOf(i+1))) {
  208. break;
  209. }
  210. else if (Arrays.asList(board[2]).contains(String.valueOf(i+1))) {
  211. break;
  212. }
  213. else if (i == 8) {
  214. return "draw";
  215. }
  216. }
  217.  
  218. System.out.println();
  219. System.out.println(turn + "'s turn; Enter a space number to place " + turn + " in:");
  220. return null;
  221. }
  222.  
  223. // create the starting game board
  224. public static void populateEmptyBoard()
  225. {
  226. for (int i = 0; i < 9; i++)
  227. {
  228. if (i == 0)
  229. {
  230. board[0][0] = String.valueOf(i + 1);
  231. } else if (i == 1)
  232. {
  233. board[0][1] = String.valueOf(i + 1);
  234. } else if (i == 2)
  235. {
  236. board[0][2] = String.valueOf(i + 1);
  237. } else if (i == 3)
  238. {
  239. board[1][0] = String.valueOf(i + 1);
  240. } else if (i == 4)
  241. {
  242. board[1][1] = String.valueOf(i + 1);
  243. } else if (i == 5)
  244. {
  245. board[1][2] = String.valueOf(i + 1);
  246. } else if (i == 6)
  247. {
  248. board[2][0] = String.valueOf(i + 1);
  249. } else if (i == 7)
  250. {
  251. board[2][1] = String.valueOf(i + 1);
  252. } else if (i == 8)
  253. {
  254. board[2][2] = String.valueOf(i + 1);
  255. }
  256. }
  257. }
  258. }
  259.  
  260. // Disclaimer:
  261. // The given assignment description, project files, code files and/or solution files
  262. // should not be made available in a public form via methods such as online hosting
  263. // in code repositories, educational resource hosting websites, etc. These projects
  264. // and related files can be hosted in private repositories for situations such as
  265. // showing example work at job interviews, personal reference for future class
  266. // projects, etc.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement