• + 0 comments

    A lot of posts show a gimmick solution based on the fact that a pattern was found. What if there was not such an easy to observe pattern? Here is a proper recursive solution in Javascript:

    const moves = [[-2, 1], [-2, -1], [1, -2], [-1, -2]];
    function whoWins(x, y, player) {
        if (x < 1 || y < 1) // The last player did not have a legal move
            return player;
    
        // Only one path needs to return a win for the current player since he will play optimally
        return (moves.some((val) => {
            return whoWins(x + val[0], y + val[1], (player + 1) % 2) === player;
        })) ? player : (player + 1) % 2;
    }
    
    // Complete the chessboardGame function below.
    function chessboardGame(x, y) {
     return whoWins(x, y, 0) === 0 ? "First" : "Second";
    }