Last active
May 27, 2020 14:44
-
-
Save kapiljhajhria/3606b74adf7cb65a9b3be1f3b80ae7bc to your computer and use it in GitHub Desktop.
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
class QueenAttack { | |
constructor(position) { | |
this.white=position.white; | |
this.black=position.black; | |
} | |
toString() { | |
let res = new Array(8).fill(new Array(8).fill('_')); | |
res[this.white[0]][this.white[1]] = 'W'; | |
res[this.black[0]][this.black[1]] = 'B'; | |
return res.map(s => s.join(' ') + '\n').join(''); | |
} | |
canAttack() { | |
if(this.white[0]===this.black[0] && this.white[1]===this.black[1]) return false | |
else if(this.white[0]===this.black[0]) return true | |
else if(this.white[1]===this.black[1]) return true | |
else if(Math.abs(this.white[0]-this.black[0])===Math.abs(this.white[1]-this.black[1])) return true | |
return false; | |
} | |
} | |
console.log(new Array(8).fill(new Array(8).fill('_')).map(s => s.join(' ') + '\n').join('')) | |
function queenAttack(white, black) { | |
if(white[0]===black[0] && white[1]===black[1]) return false | |
else if(white[0]===black[0]) return true | |
else if(white[1]===black[1]) return true | |
else if(Math.abs(white[0]-black[0])===Math.abs(white[1]-black[1])) return true | |
return false; | |
} | |
// console.log(queenAttack([4,7],[2,3])===false) | |
// console.log(queenAttack([2,4],[2,7])===true) | |
// console.log(queenAttack([4,4],[2,4])===true) | |
// console.log(queenAttack([1,1],[6,6])===true) | |
// console.log(queenAttack([6,5],[6,6])===true) | |
// console.log(queenAttack([6,5],[3,3])===false) | |
// console.log(queenAttack([3,3],[3,3])===false) | |
// console.log(queenAttack([3,3],[8,6])===false) | |
module.exports = QueenAttack; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment