/** * Implements the game of Tic Tac Toe * @author cs161 * @version spring19 */ public class TicTacToe{ private final String EMPTY = " "; private final String PLAYER1 = "x"; private final String PLAYER2 = "o"; private String cellA1, cellA2, cellA3; private String cellB1, cellB2, cellB3; private String cellC1, cellC2, cellC3; private int numCellsRemaining; private String currPlayer; /** * Initializes the game of Tic Tac Toe */ public TicTacToe(){ cellA1 = EMPTY; cellA2 = EMPTY; cellA3 = EMPTY; cellB1 = EMPTY; cellB2 = EMPTY; cellB3 = EMPTY; cellC1 = EMPTY; cellC2 = EMPTY; cellC3 = EMPTY; numCellsRemaining = 9; currPlayer = "x"; } /** * Prints the board to the screen */ public void printBoard(){ System.out.println(); System.out.println(cellA1 + " | " + cellA2 + " | " + cellA3); System.out.println("---------"); System.out.println(cellB1 + " | " + cellB2 + " | " + cellB3); System.out.println("---------"); System.out.println(cellC1 + " | " + cellC2 + " | " + cellC3); System.out.println(); } /** * Returns true if one of the players has won the game or there are no * empty cells remaining * * @return true if the game is over, false otherwise */ public boolean gameOver(){ boolean player1 = checkWin(PLAYER1); boolean player2 = checkWin(PLAYER2); boolean noTurns = (numCellsRemaining == 0); return player1 || player2 || noTurns; } /** * Returns a string indicating the mark of the winning player ("x" or "o") * or "tie" if the game was a tie * * @return A string representing the end result of the game */ public String winner(){ boolean player1 = checkWin(PLAYER1); boolean player2 = checkWin(PLAYER2); if(!player1 && !player2){ return "tie"; } else if(player1){ return PLAYER1; } else{ return PLAYER2; } } /** * Stores the current player's mark in the specified row and column * @param row * The row in which the player plays * @param col * The column in which the player plays */ public void move(int row, int col){ if(row == 1 && col == 1){ if(cellA1.equals(EMPTY)){ cellA1 = currPlayer; } } else if(row == 1 && col == 2){ if(cellA2.equals(EMPTY)){ cellA2 = currPlayer; } } else if(row == 1 && col == 3){ if(cellA3.equals(EMPTY)){ cellA3 = currPlayer; } } else if(row == 2 && col == 1){ if(cellB1.equals(EMPTY)){ cellB1 = currPlayer; } } else if (row == 2 && col == 2){ if(cellB2.equals(EMPTY)){ cellB2 = currPlayer; } } else if (row == 2 && col == 3){ if(cellB3.equals(EMPTY)){ cellB3 = currPlayer; } } else if(row == 3 && col == 1){ if(cellC1.equals(EMPTY)){ cellC1 = currPlayer; } } else if(row == 3 && col == 2){ if(cellC2.equals(EMPTY)){ cellC2 = currPlayer; } } else if(row == 3 && col == 3){ if(cellC3.equals(EMPTY)){ cellC3 = currPlayer; } } // switch players in anticipation of the next round if(currPlayer.equals(PLAYER1)){ currPlayer = PLAYER2; } else{ currPlayer = PLAYER1; } numCellsRemaining--; } /*---------------------------------------------- * Private Helper Methods *----------------------------------------------*/ private boolean checkWin(String player){ if(cellA1.equals(player) && cellA2.equals(player) && cellA3.equals(player)){ return true; } else if(cellB1.equals(player) && cellB2.equals(player) && cellB3.equals(player)){ return true; } else if(cellC1.equals(player) && cellC2.equals(player) && cellC3.equals(player)){ return true; } else if(cellA1.equals(player) && cellB1.equals(player) && cellC1.equals(player)){ return true; } else if(cellA2.equals(player) && cellB2.equals(player) && cellC2.equals(player)){ return true; } else if(cellA3.equals(player) && cellB3.equals(player) && cellC3.equals(player)){ return true; } else if(cellA1.equals(player) && cellB2.equals(player) && cellC3.equals(player)){ return true; } else if(cellA3.equals(player) && cellB2.equals(player) && cellC1.equals(player)){ return true; } else{ return false; } } }