Diagonal win condition for a tic tac toe game

My code can check straight lines but I can't seem to implement the code for a diagonal win. I've tried it for player one 'X' at the very bottom of the member function. Here is the rest of the code for my program

https://gist.github.com/Ottzoa/589d8e5937f976ee2aa4ca093921bd0e


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

char TicTacToe::winCheck()
{
int i, j;
for (i = 0; i < 3; i++)
{
    for (j = 0; j < 3; j++)
        if (game[i][j] != 'X')
        break;
        if (j == 3)
        return 'X';
    for (j = 0; j < 3; j++)
        if (game[i][j] != 'O')
        break;
        if (j == 3)
        return 'O';
    for (j = 0; j < 3; j++)
        if (game[j][i] != 'X')
        break;
        if (j == 3)
        return 'X';
    for (j = 0; j < 3; j++)
        if (game[j][i] != 'O')
        break;
        if (j == 3)
        return 'O';
}   
if (game[0][0] == game[1][1] == game[2][2] =='X')
    return 'X';
if (game[0][2] == game[1][1] == game[2][0]=='X')
    return 'X';

return 0;
}
Last edited on
closed account (E0p9LyTq)
Your logic for how to check for a diagonal is correct, your coding is a bit off.

1
2
3
if ((game[0][0] == 'X' && game[1][1] == 'X' && game[2][2] == 'X') ||
    (game[0][2] == 'X' && game[1][1] == 'X' && game[2][0] == 'X'))
     { return 'X'; }


A bit hackish, but it works.
Thanks. That worked.
Topic archived. No new replies allowed.