Created
August 14, 2020 15:55
-
-
Save cplpearce/fc053801a9efca3a810597bd90facbe7 to your computer and use it in GitHub Desktop.
Kata 16 - Queen Threat Detector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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