@@ 5-46 (lines=42) @@ | ||
2 | * King module |
|
3 | * @module |
|
4 | */ |
|
5 | "use strict"; |
|
6 | ||
7 | /** |
|
8 | * Class with King logic |
|
9 | */ |
|
10 | class King { |
|
11 | /** |
|
12 | * init |
|
13 | */ |
|
14 | constructor(color) { |
|
15 | this.symbol = "K"; |
|
16 | this.moved = 0; |
|
17 | this.active = true; |
|
18 | this.color = color; |
|
19 | this.rows = [null, "A", "B", "C", "D", "E", "F", "G", "H"]; |
|
20 | this.img = this.color === "white" ? "w-king" : "b-king"; |
|
21 | } |
|
22 | ||
23 | legalMove(x, y, nx, ny) { |
|
24 | let xNum = this.rows.indexOf(x); |
|
25 | let nxNum = this.rows.indexOf(nx); |
|
26 | ||
27 | let xStep = Math.abs(xNum - nxNum); |
|
28 | let yStep = Math.abs(y - ny) |
|
29 | if (xStep > 1 || yStep > 1) { |
|
30 | return false |
|
31 | } |
|
32 | ||
33 | if (x == nx && y !== ny || x !== nx && y == ny) { |
|
34 | return true |
|
35 | } |
|
36 | ||
37 | if (xStep === yStep) { |
|
38 | return true |
|
39 | } |
|
40 | ||
41 | return false; |
|
42 | } |
|
43 | } |
|
44 | ||
45 | ||
46 | module.exports = King; |
|
47 |
@@ 5-43 (lines=39) @@ | ||
2 | * Queen module |
|
3 | * @module |
|
4 | */ |
|
5 | "use strict"; |
|
6 | ||
7 | /** |
|
8 | * Class with Queen logic |
|
9 | */ |
|
10 | class Queen { |
|
11 | /** |
|
12 | * init |
|
13 | */ |
|
14 | constructor(color) { |
|
15 | this.symbol = "Q"; |
|
16 | this.moved = 0; |
|
17 | this.active = true; |
|
18 | this.color = color; |
|
19 | this.rows = [null, "A", "B", "C", "D", "E", "F", "G", "H"]; |
|
20 | this.img = this.color === "white" ? "w-queen" : "b-queen"; |
|
21 | } |
|
22 | ||
23 | legalMove(x, y, nx, ny) { |
|
24 | // row |
|
25 | if (x == nx && y !== ny || x !== nx && y == ny) { |
|
26 | return true |
|
27 | } |
|
28 | ||
29 | let xNum = this.rows.indexOf(x); |
|
30 | let nxNum = this.rows.indexOf(nx); |
|
31 | ||
32 | let xStep = Math.abs(xNum - nxNum); |
|
33 | let yStep = Math.abs(y - ny) |
|
34 | if (xStep === yStep) { |
|
35 | return true |
|
36 | } |
|
37 | ||
38 | return false; |
|
39 | } |
|
40 | } |
|
41 | ||
42 | ||
43 | module.exports = Queen; |
|
44 |
@@ 5-39 (lines=35) @@ | ||
2 | * Knight module |
|
3 | * @module |
|
4 | */ |
|
5 | "use strict"; |
|
6 | ||
7 | /** |
|
8 | * Class with Knight logic |
|
9 | */ |
|
10 | class Knight { |
|
11 | /** |
|
12 | * init |
|
13 | */ |
|
14 | constructor(color) { |
|
15 | this.symbol = "N"; |
|
16 | this.moved = 0; |
|
17 | this.active = true; |
|
18 | this.color = color; |
|
19 | this.rows = [null, "A", "B", "C", "D", "E", "F", "G", "H"]; |
|
20 | this.img = this.color === "white" ? "w-knight" : "b-knight"; |
|
21 | } |
|
22 | ||
23 | legalMove(x, y, nx, ny) { |
|
24 | let xNum = this.rows.indexOf(x); |
|
25 | let nxNum = this.rows.indexOf(nx); |
|
26 | ||
27 | let xStep = Math.abs(xNum - nxNum); |
|
28 | let yStep = Math.abs(y - ny) |
|
29 | ||
30 | if (xStep == 2 && yStep == 1 || xStep == 1 && yStep == 2) { |
|
31 | return true; |
|
32 | } |
|
33 | ||
34 | return false; |
|
35 | } |
|
36 | } |
|
37 | ||
38 | ||
39 | module.exports = Knight; |
|
40 |