We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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:
constmoves=[[-2,1],[-2,-1],[1,-2],[-1,-2]];functionwhoWins(x,y,player){if(x<1||y<1)// The last player did not have a legal movereturnplayer;// Only one path needs to return a win for the current player since he will play optimallyreturn(moves.some((val)=>{returnwhoWins(x+val[0],y+val[1],(player+1)%2)===player;}))?player:(player+1)%2;}// Complete the chessboardGame function below.functionchessboardGame(x,y){returnwhoWins(x,y,0)===0?"First":"Second";}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
A Chessboard Game
You are viewing a single comment's thread. Return to all 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: