/** * Restart or start a new game, will clear the board and win status */ public void restart() { clearCells(); winner = null; currentTurn = Player.X; state = GameState.IN_PROGRESS; }
private void clearCells() { for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { cells[i][j] = new Cell(); } } }
private boolean isValid(int row, int col ) { if( state == GameState.FINISHED ) { return false; } else if( isOutOfBounds(row) || isOutOfBounds(col) ) { return false; } else if( isCellValueAlreadySet(row, col) ) { return false; } else { return true; } }
/** * Mark the current row for the player who's current turn it is. * Will perform no-op if the arguments are out of range or if that position is already played. * Will also perform a no-op if the game is already over. * * @param row 0..2 * @param col 0..2 * */ public void mark( int row, int col ) { if(isValid(row, col)) { cells[row][col].setValue(currentTurn); if(isWinningMoveByPlayer(currentTurn, row, col)) { state = GameState.FINISHED; winner = currentTurn; } else { // flip the current turn and continue flipCurrentTurn(); } } }
/** * This test will simulate and verify x is the winner. * * X | X | X * O | | * | O | */ @Test public void test3inRowAcrossTopForX() { board.mark(0,0); // x assertNull(board.getWinner()); board.mark(1,0); // o assertNull(board.getWinner()); board.mark(0,1); // x assertNull(board.getWinner()); board.mark(2,1); // o assertNull(board.getWinner()); board.mark(0,2); // x assertEquals(Player.X, board.getWinner()); }
private boolean isCellValueAlreadySet(int row, int col) { return cells[row][col].getValue() != null; }
public Board() { restart(); }
@Before public void setup() { board = new Board(); }
/** * This test will simulate and verify o is the winner. * * O | X | X * | O | * | X | O */ @Test public void test3inRowDiagonalFromTopLeftToBottomForO() { board.mark(0,1); // x assertNull(board.getWinner()); board.mark(0,0); // o assertNull(board.getWinner()); board.mark(2,1); // x assertNull(board.getWinner()); board.mark(1,1); // o assertNull(board.getWinner()); board.mark(0,2); // x assertNull(board.getWinner()); board.mark(2,2); // o assertEquals(Player.O, board.getWinner()); }
/** * Algorithm adapted from http://www.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe.html * @param player * @param currentRow * @param currentCol * @return true if <code>player</code> who just played the move at the <code>currentRow</code>, <code>currentCol</code> * has a tic tac toe. */ private boolean isWinningMoveByPlayer(Player player, int currentRow, int currentCol) { return (cells[currentRow][0].getValue() == player // 3-in-the-row && cells[currentRow][1].getValue() == player && cells[currentRow][2].getValue() == player || cells[0][currentCol].getValue() == player // 3-in-the-column && cells[1][currentCol].getValue() == player && cells[2][currentCol].getValue() == player || currentRow == currentCol // 3-in-the-diagonal && cells[0][0].getValue() == player && cells[1][1].getValue() == player && cells[2][2].getValue() == player || currentRow + currentCol == 2 // 3-in-the-opposite-diagonal && cells[0][2].getValue() == player && cells[1][1].getValue() == player && cells[2][0].getValue() == player); }