tic tac toe

Pages: 12
You could combine all those winner checking functions into one function that returns an int. Let's say 0 would mean a tie, 1 X wins, 2 O wins, and 3 neither a tie nor win for either player.
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int ticTacToe::determineGameStatus(){ //returns 0 representing a tie, 1 representing a win by Player1, 2 representing a win by Player2, and 3 otherwise.
    for(int i=0; i<3; i++){ // i will checks every row and every column
        if (board[i][0]==board[i][1] && board[i][1]==board[i][2]){ // 3 in a row check
            if (board[i][0]=='X') return 1;
            if (board[i][0]=='O') return 2;
            }
        if (board[0][i]==board[1][i] && board[1][i]==board[2][i]){ // 3 in a column check
            if (board[0][i]=='X') return 1;
            if (board[0][i]=='O') return 2;
        }
    }
    if ((board[0][0]==board[1][1] && board[1][1]==board[2][2]) || (board[0][2]==board[1][1] && board[1][1]==board[2][0])){
        if (board[1][1]=='X') return 1; // 3 in a diagonal check
        if (board[1][1]=='O') return 2;
    }
    bool tieTest = true;
    for (int row=0; row<3; row++) // checks for tie game after the check for every possible 3 in a row is over
        for(int col=0; col<3; col++)
            if (board[row][col]=='*') tieTest = false;
    if (tieTest) return 0;
    return 3;
}
Topic archived. No new replies allowed.
Pages: 12