Tic tac toe

Is it just me? Or is there ALOT of if statements in games like Tic tac toe? Or is there alot of if statements in most games?

example:

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
if
(marker_X == board[0][0] && marker_X == board[0][1] && marker_X == board[0][2] ||
marker_X == board[1][0] && marker_X == board[1][1] && marker_X == board[1][2] ||
marker_X == board[2][0] && marker_X == board[2][1] && marker_X == board[2][2] ||
marker_X == board[0][0] && marker_X == board[1][0] && marker_X == board[2][0] ||
marker_X == board[0][1] && marker_X == board[1][1] && marker_X == board[2][1] ||
marker_X == board[0][2] && marker_X == board[1][2] && marker_X == board[2][2] ||
marker_X == board[0][0] && marker_X == board[1][1] && marker_X == board[2][2] ||
marker_X == board[0][2] && marker_X == board[1][1] && marker_X == board[2][0])
                        {system("cls");
                         displayBoard(board);
                         cout << "\n           X WINS\n";
                         return 0;}
else if
(marker_O == board[0][0] && marker_O == board[0][1] && marker_O == board[0][2] ||
marker_O == board[1][0] && marker_O == board[1][1] && marker_O == board[1][2] ||
marker_O == board[2][0] && marker_O == board[2][1] && marker_O == board[2][2] ||
marker_O == board[0][0] && marker_O == board[1][0] && marker_O == board[2][0] ||
marker_O == board[0][1] && marker_O == board[1][1] && marker_O == board[2][1] ||
marker_O == board[0][2] && marker_O == board[1][2] && marker_O == board[2][2] ||
marker_O == board[0][0] && marker_O == board[1][1] && marker_O == board[2][2] ||
marker_O == board[0][2] && marker_O == board[1][1] && marker_O == board[2][0])
                        {system("cls");
                         displayBoard(board);
                         cout << "\n           O WINS\n";
                         return 0;}
There's usually not so much. You're just hardcoding all permutations... twice (once for X and once for O). You typically don't need to do that... in tic-tac-toe or in other games.

EDIT:

example:

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
//  assuming board[x][x] is either 'X' or 'O'


// see if there's a winner
char winner = 0;  // 0=no winner, 'X'=X is winner, etc

// see if there's a winner by column or row
for(int i = 0; i < 3; ++i)
{
    // check the rows
    if( (board[i][0] == board[i][1]) && (board[i][0] == board[i][2]) )   winner = board[i][0];
    
    // and columns
    if( (board[0][1] == board[1][i]) && (board[0][i] == board[2][i]) )   winner = board[0][i];
}

// check diagonal
if( (board[0][0] == board[1][1]) && (board[2][2] == board[1][1]) )  winner = board[1][1];
if( (board[0][2] == board[1][1]) && (board[2][0] == board[1][1]) )  winner = board[1][1];

// was there a winner?
if(winner != 0)
{
    cout << "\n      " << winner << WINS\n";
} 
Last edited on
What went through my head:

http://postimg.org/image/41t1hkz9z/

Disch! That's more ifs than the OP!
Topic archived. No new replies allowed.