-
Notifications
You must be signed in to change notification settings - Fork 1
/
queen-attack.js
37 lines (30 loc) · 1.42 KB
/
queen-attack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
export class QueenAttack {
constructor(args = { white: [0, 3], black: [7, 3] }) {
if (args.white.every((n, i) => n === args.black[i])) {
throw new Error("Queens cannot share the same space");
}
this.white = args.white;
this.black = args.black;
}
canAttack() {
const [wx, wy] = this.white;
const [bx, by] = this.black;
return bx === wx || by === wy || Math.abs(bx - wx) === Math.abs(by - wy);
}
toString() {
const toBoardImageIndex = ([x, y]) => x * 16 + y * 2;
const boardImage = [
'_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', '\n',
'_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', '\n',
'_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', '\n',
'_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', '\n',
'_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', '\n',
'_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', '\n',
'_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', '\n',
'_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ', '_', '\n',
];
boardImage[toBoardImageIndex(this.white)] = "W";
boardImage[toBoardImageIndex(this.black)] = "B";
return boardImage.join("");
}
}