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
|
struct Combo
{ int row;
int col;
};
bool check_winner (string tictac[3][3], string who)
{ const Combo combo[8][3] =
{ 0,0, 0,1, 0,2, // Row 0
1,0, 1,1, 1,2, // Row 1
2,0, 2,1, 2,2, // Row 2
0,0, 1,0, 2,0, // Col 0
1,0, 1,1, 2,1, // Col 1
2,0, 2,1, 2,2, // Col 2
0,0, 1,1, 2,2, // LR Diagonal
0,2, 1,1, 2,0, // RL Diagonal
};
for (int i=0; i<8; i++) // Check each combo
{ int count = 0;
for (int j=0; j<3; j++) // Check row and col
{ if (tictac[combo[i][j].row][combo[i][j].col] == who)
count++; // One cell matched
}
if (count == 3)
return true; // All three cells matched
}
return false; // No combo's matched
}
|