Search code examples
javatic-tac-toe

Tic Tac Toe Game (Java): finding a tie game


Making a Tic Tac Toe game for my class, I have all the other methods correct and the game works unless there is a draw. board is a 2D array that represents the tic tac toe board. Here is the Full() method to try and see if the board is full:

public boolean full() {
    boolean full = false;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (board[i][j] == '-') {
                full = false;
            } else {
                full = true;
            }
        }
    }
    return full;
}

I know it does not work, I could not really think of a way to make it work. Anyone have any ideas?


Solution

  • You need to break out of the loops (or return) when you have found out that the board is not full.

    public boolean full() {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[i][j] == '-') {
                    return false;
                }
            }
        }
        return true;
    }