[Solved] How to check for a tie game beginner C++ [closed]


The conditions like the following are wrong:

(board[0] == 'X' || 'O')

Because of C++ operator precedence and evaluation rules, the compiler understands it as:

(board[0] == 'X') || ('O' != 0)

The second part is, of course, always true, so it always succeeds for every field and therefore for the whole board.

You would need to write it explicitly in two comparisons like this:

(board[0] == 'X' || board[0] == 'O')

For the future, a better solution than a bunch of conditions would be a loop, for example:

bool tieGame()
{
    for (int i = 0; i < 9; i++) {
        if (board[i] != 'X' && board[i] != 'O') {
            // Some field is empty, not a tie
            return false;
        }
    }
    // All fields are either 'X' or 'O'
    cout << "The game is a tie!  Play again!" << endl;
    return true;
}

And even more better, as Nenad wrote in his answer, just count the number of free spaces left (or the used fields) — that’s just one variable comparison instead of going through the whole board each time.

1

solved How to check for a tie game beginner C++ [closed]