svelte-tic-tac-toe icon indicating copy to clipboard operation
svelte-tic-tac-toe copied to clipboard

Winner evaluation

Open fkereki opened this issue 4 years ago • 5 comments

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;
}

Screenshot_20190722_201034

fkereki avatar Jul 22 '19 23:07 fkereki