Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions solutions/java/src/tictactoe/models/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,45 @@

public class Board {
private final int size;
private int movesCount;
private int movesCount; // FIX 1: must NOT be final, because we need to increment it
private final Cell[][] board;

public Board(int size) {
this.size = size;
this.board = new Cell[size][size];
movesCount = 0;
movesCount = 0; // initialize move counter
initializeBoard();
}

private void initializeBoard() {
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
board[row][col] = new Cell();
board[row][col] = new Cell();
}
}
}

// FIX 2: movesCount MUST be incremented inside the move method
public boolean placeSymbol(int row, int col, Symbol symbol) {
// Validate bounds
if (row < 0 || row >= size || col < 0 || col >= size) {
throw new InvalidMoveException("Invalid position: out of bounds.");
}

// Validate that cell is empty
if (board[row][col].getSymbol() != Symbol.EMPTY) {
throw new InvalidMoveException("Invalid position: cell is already occupied.");
}

// Place the move
board[row][col].setSymbol(symbol);
movesCount++;

movesCount++;
// FIX 2 EXPLANATION:
// Earlier this was missing!
// Without this increment, isFull() would NEVER return true.
// Now it correctly tracks the number of moves.

return true;
}

Expand All @@ -43,6 +55,7 @@ public Cell getCell(int row, int col) {
}

public boolean isFull() {
// FIX 3: This now works correctly because movesCount increments properly
return movesCount == size * size;
}

Expand Down