svelte-tic-tac-toe
svelte-tic-tac-toe copied to clipboard
Winner evaluation
I believe the getWinner
logic is wrong; see the attached screenshot. If a row/column is totally empty, it stops looking at the other rows/columns, and it misses wins. My suggestion would be:
function getWinner(board) {
for (let i = 0; i < 3; i++) {
if (board[i][0] && board[i][0] === board[i][1] && board[i][1] === board[i][2]) {
return board[i][1];
}
if (board[0][i] && board[0][i] === board[1][i] && board[1][i] === board[2][i]) {
return board[1][i];
}
}
if (
(board[0][0] && board[0][0] === board[1][1] && board[1][1] == board[2][2]) ||
(board[0][2] && board[0][2] === board[1][1] && board[1][1] == board[2][0])
) {
return board[1][1];
}
return null;
}