samedi 4 juillet 2015

Shortening C++ If condition

I have a very long if statement that checks for winning conditions of a tic-tac-toe board:

if 
((tttPositions[i][j][0] && tttPositions[i][j][1] && tttPositions[i][j][2] == 1) 
|| (tttPositions[i][j][3] && tttPositions[i][j][4] && tttPositions[i][j][5] == 1) 
|| (tttPositions[i][j][6] && tttPositions[i][j][7] && tttPositions[i][j][8] == 1) 
|| (tttPositions[i][j][0] && tttPositions[i][j][3] && tttPositions[i][j][6] == 1) 
|| (tttPositions[i][j][1] && tttPositions[i][j][4] && tttPositions[i][j][7] == 1) 
|| (tttPositions[i][j][2] && tttPositions[i][j][5] && tttPositions[i][j][8] == 1) 
|| (tttPositions[i][j][0] && tttPositions[i][j][4] && tttPositions[i][j][8] == 1) 
|| (tttPositions[i][j][2] && tttPositions[i][j][4] && tttPositions[i][j][6] == 1))
{
    if (j = 0)
        cout << "x wins" << endl;
    if (j = 1)
        cout << "o wins" << endl;
}   

This looks really ugly.. is there a way to keep track of the winning conditions separately to greatly reduce the length of this if statement?

I put the multiple tic tac toe boards into a 3D vector where each box is 2x9, the first row represents the 'x' positions and the second row represents the 'o' positions, so like:

110000011
001110000

represents:

x x o
o o _
_ x x

Aucun commentaire:

Enregistrer un commentaire