+ 1
TICTACTOE
https://sololearn.com/compiler-playground/cl7758V7thGF/?ref=app Any upgrade needed
4 Réponses
+ 2
Your code is great! But you could improve it.
Use a 2D array as a look-up table instead of multiple conditions for checking wins.
0
Can you enlightened me
0
Aweneg Rooney Using multiple if-conditions for every position is inefficient and is also hard to read.
A 2D array which stores the patterns for win:
bool checkWin(char board[], char player)
{
int winPatterns[8][3] = {
{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, // Rows
{0, 3, 6}, {1, 4, 7}, {2, 5, 8}, // Columns
{0, 4, 8}, {2, 4, 6} // Diagonals
};
And the check for win:
for(int i = 0; i < 8; i++)
{
if(board[winPatterns[i][0]] == player &&
board[winPatterns[i][1]] == player &&
board[winPatterns[i][2]] == player)
{
return true;
}
}
return false;
0
Thanks