Skip to content

Instantly share code, notes, and snippets.

@cplpearce
Created August 14, 2020 15:55
Show Gist options
  • Save cplpearce/fc053801a9efca3a810597bd90facbe7 to your computer and use it in GitHub Desktop.
Save cplpearce/fc053801a9efca3a810597bd90facbe7 to your computer and use it in GitHub Desktop.
Kata 16 - Queen Threat Detector
let wQ = [0, 5];
let bQ = [5, 0];
let board = [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
]
function generateBoard(wQ, bQ) {
board[wQ[0]].splice([wQ[1]], 1, 1);
board[bQ[0]].splice([bQ[1]], 1, 1);
return board.join('\n');
}
// I'm not a mathematician so... citing formula for threat from as
// If qR = oR, it means that both the queen and the opponent are in the same row and the queen can attack the opponent.
// Similarly, if qC = oC then also the queen can attack the opponent as they both are in the same column.
// And for diagonals, if abs(qR – oR) = abs(qC – oC) i.e. queen can attack the opponent diagonally.
// https://www.geeksforgeeks.org/check-if-a-queen-can-attack-a-given-cell-on-chessboard/
function queenThreat() {
let threat = false;
if (Math.abs(wQ[0] - bQ[0]) === (Math.abs(wQ[1] - bQ[1]))) threat = true;
if (wQ[0] === bQ[0] || wQ[1] === bQ[1]) threat = true;
return threat;
}
let generatedBoard = generateBoard(wQ, bQ);
console.log(generatedBoard);
console.log(queenThreat(generatedBoard));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment