3rd Mar 2025, 11:11 AM
Aweneg Rooney
Aweneg Rooney - avatar
4 Answers
+ 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.
3rd Mar 2025, 1:24 PM
Afnan Irtesum Chowdhury
Afnan Irtesum Chowdhury - avatar
0
Can you enlightened me
3rd Mar 2025, 7:15 PM
Aweneg Rooney
Aweneg Rooney - avatar
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;
4th Mar 2025, 5:27 AM
Afnan Irtesum Chowdhury
Afnan Irtesum Chowdhury - avatar
0
Thanks
4th Mar 2025, 11:36 AM
Aweneg Rooney
Aweneg Rooney - avatar