Guest User

Untitled

a guest
Feb 25th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. /**
  2. * Tic Tac Toe Game in Console
  3. * @author Bruce Zou
  4. */
  5. import java.util.*;
  6.  
  7. public class TicTacToe {
  8. public static final int board_size = 3;
  9. public static void main(String[] args) {
  10. Scanner in = new Scanner(System.in);
  11.  
  12. int[][] board = createBoard(board_size);
  13. boolean whoseTurn = true; //keeps track of whose turn it is true = p1 false = p2.
  14. int turnsTaken = 0;
  15.  
  16. while(checkWinner(board)==0 && turnsTaken<board_size*board_size) {
  17. int row, col;
  18.  
  19. do {
  20. if(whoseTurn) System.out.println("Player 1");
  21. else System.out.println("Player 2");
  22. System.out.println("Enter row you want to play (0-2): ");
  23. row = in.nextInt();
  24. System.out.println("Enter col you want to play (0-2): ");
  25. col = in.nextInt();
  26. }while(board[row][col]!=0);
  27.  
  28. //Update the game board according to the player's chosen location
  29. if(whoseTurn) board[row][col]=1;
  30. else board[row][col]=2;
  31. drawBoard(board);
  32. whoseTurn=!whoseTurn;
  33. turnsTaken++;
  34. }
  35. displayWinner(checkWinner(board));
  36. }
  37.  
  38. /**
  39. * Creates an array filled with 0's as the initial game board of dimensions size x size
  40. * @param size size of one side of the game board
  41. * @return An array representation of the board (size x size)
  42. */
  43. public static int[][] createBoard(int size){
  44. int[][] board = new int[size][size];
  45. for(int[] row : board)
  46. Arrays.fill(row, 0);
  47. return board;
  48. }
  49.  
  50. /**
  51. * Helper method to draw the horizontal lines by multiplying the given string a set number of times
  52. * @param s String to multiply
  53. * @param n number of times to duplicate/multiply the string
  54. * @return a string that contains the String s n times
  55. */
  56. public static String StringMultiply(String s, int n) {
  57. StringBuilder sb = new StringBuilder();
  58. for(int i=0;i<n;i++) {
  59. sb.append(s);
  60. }
  61. return sb.toString();
  62. }
  63.  
  64. /**
  65. * Draws the board in it's current state
  66. * @param board Current game board
  67. */
  68. public static void drawBoard(int[][] board) {
  69. for(int i=0;i<board.length;i++) {
  70. System.out.println(StringMultiply("----",board_size)); //Print horizontal lines
  71. for(int j=0;j<board.length;j++) {
  72.  
  73. //Set string to fill that box of the grid
  74. String out = " ";
  75. if(board[i][j]==1) out = "X";
  76. else if(board[i][j]==2) out = "O";
  77.  
  78. System.out.print("| " + out + " "); //Print numbers and vertical lines
  79. if(j==board.length-1) System.out.print("|\n"); //Print the last vertical line
  80. }
  81. }
  82. System.out.println(StringMultiply("----",board_size)); //Print the last horizontal line
  83. }
  84.  
  85.  
  86. /**
  87. * Checks if there is a winner in the current board's state
  88. * @param board Current game board
  89. * @return 1 or 2 if that player has won. 0 if no player won
  90. */
  91. public static int checkWinner(int[][] board) {
  92. //Check Row Winner
  93. for(int i=0;i<3;i++) {
  94. //Create a set of the current row
  95. Integer[] rowToCheck = {board[i][0],board[i][1],board[i][2]};
  96. Set<Integer> row = new HashSet<Integer>(Arrays.asList(rowToCheck));
  97. //Winner if the set only has 1 value and it isn't 0
  98. if(row.size()==1 && board[i][0]!=0)
  99. return board[i][0];
  100. }
  101.  
  102. //Check Col Winner
  103. for(int i=0;i<3;i++) {
  104. //Create a set of the current col
  105. Integer[] colToCheck = {board[0][i],board[1][i],board[2][i]};
  106. Set<Integer> col = new HashSet<Integer>(Arrays.asList(colToCheck));
  107. //Winner if the set only has 1 value and it isn't 0
  108. if(col.size()==1 && board[0][i]!=0)
  109. return board[0][i];
  110. }
  111.  
  112. //Check Diagonal
  113. //Create a set for each diagonal
  114. Integer[] diag1ToCheck = {board[0][0],board[1][1],board[2][2] };
  115. Integer[] diag2ToCheck = {board[0][2],board[1][1],board[2][0] };
  116. Set<Integer> diag1 = new HashSet<Integer>(Arrays.asList(diag1ToCheck));
  117. Set<Integer> diag2 = new HashSet<Integer>(Arrays.asList(diag2ToCheck));
  118. //Winner if either set has only 1 value and that value isn't 0
  119. if(diag1.size()==1 || diag2.size()==1 && board[1][1]!=0)
  120. return board[1][1];
  121.  
  122.  
  123. return 0;
  124. }
  125.  
  126. /**
  127. * Prints out the winner of the game.
  128. * @param winner The int result of the checkWinner method
  129. */
  130. public static void displayWinner(int winner) {
  131. if(winner!=0)
  132. System.out.println("Player " + winner + " wins!");
  133. else
  134. System.out.println("Tie.");
  135. }
  136. }
Add Comment
Please, Sign In to add comment