Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Tic Tac Toe Game in Console
- * @author Bruce Zou
- */
- import java.util.*;
- public class TicTacToe {
- public static final int board_size = 3;
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int[][] board = createBoard(board_size);
- boolean whoseTurn = true; //keeps track of whose turn it is true = p1 false = p2.
- int turnsTaken = 0;
- while(checkWinner(board)==0 && turnsTaken<board_size*board_size) {
- int row, col;
- do {
- if(whoseTurn) System.out.println("Player 1");
- else System.out.println("Player 2");
- System.out.println("Enter row you want to play (0-2): ");
- row = in.nextInt();
- System.out.println("Enter col you want to play (0-2): ");
- col = in.nextInt();
- }while(board[row][col]!=0);
- //Update the game board according to the player's chosen location
- if(whoseTurn) board[row][col]=1;
- else board[row][col]=2;
- drawBoard(board);
- whoseTurn=!whoseTurn;
- turnsTaken++;
- }
- displayWinner(checkWinner(board));
- }
- /**
- * Creates an array filled with 0's as the initial game board of dimensions size x size
- * @param size size of one side of the game board
- * @return An array representation of the board (size x size)
- */
- public static int[][] createBoard(int size){
- int[][] board = new int[size][size];
- for(int[] row : board)
- Arrays.fill(row, 0);
- return board;
- }
- /**
- * Helper method to draw the horizontal lines by multiplying the given string a set number of times
- * @param s String to multiply
- * @param n number of times to duplicate/multiply the string
- * @return a string that contains the String s n times
- */
- public static String StringMultiply(String s, int n) {
- StringBuilder sb = new StringBuilder();
- for(int i=0;i<n;i++) {
- sb.append(s);
- }
- return sb.toString();
- }
- /**
- * Draws the board in it's current state
- * @param board Current game board
- */
- public static void drawBoard(int[][] board) {
- for(int i=0;i<board.length;i++) {
- System.out.println(StringMultiply("----",board_size)); //Print horizontal lines
- for(int j=0;j<board.length;j++) {
- //Set string to fill that box of the grid
- String out = " ";
- if(board[i][j]==1) out = "X";
- else if(board[i][j]==2) out = "O";
- System.out.print("| " + out + " "); //Print numbers and vertical lines
- if(j==board.length-1) System.out.print("|\n"); //Print the last vertical line
- }
- }
- System.out.println(StringMultiply("----",board_size)); //Print the last horizontal line
- }
- /**
- * Checks if there is a winner in the current board's state
- * @param board Current game board
- * @return 1 or 2 if that player has won. 0 if no player won
- */
- public static int checkWinner(int[][] board) {
- //Check Row Winner
- for(int i=0;i<3;i++) {
- //Create a set of the current row
- Integer[] rowToCheck = {board[i][0],board[i][1],board[i][2]};
- Set<Integer> row = new HashSet<Integer>(Arrays.asList(rowToCheck));
- //Winner if the set only has 1 value and it isn't 0
- if(row.size()==1 && board[i][0]!=0)
- return board[i][0];
- }
- //Check Col Winner
- for(int i=0;i<3;i++) {
- //Create a set of the current col
- Integer[] colToCheck = {board[0][i],board[1][i],board[2][i]};
- Set<Integer> col = new HashSet<Integer>(Arrays.asList(colToCheck));
- //Winner if the set only has 1 value and it isn't 0
- if(col.size()==1 && board[0][i]!=0)
- return board[0][i];
- }
- //Check Diagonal
- //Create a set for each diagonal
- Integer[] diag1ToCheck = {board[0][0],board[1][1],board[2][2] };
- Integer[] diag2ToCheck = {board[0][2],board[1][1],board[2][0] };
- Set<Integer> diag1 = new HashSet<Integer>(Arrays.asList(diag1ToCheck));
- Set<Integer> diag2 = new HashSet<Integer>(Arrays.asList(diag2ToCheck));
- //Winner if either set has only 1 value and that value isn't 0
- if(diag1.size()==1 || diag2.size()==1 && board[1][1]!=0)
- return board[1][1];
- return 0;
- }
- /**
- * Prints out the winner of the game.
- * @param winner The int result of the checkWinner method
- */
- public static void displayWinner(int winner) {
- if(winner!=0)
- System.out.println("Player " + winner + " wins!");
- else
- System.out.println("Tie.");
- }
- }
Add Comment
Please, Sign In to add comment