1 | /** |
||
2 | * King module |
||
3 | * @module |
||
4 | */ |
||
5 | View Code Duplication | "use strict"; |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
6 | |||
7 | /** |
||
8 | * Class with King logic |
||
9 | */ |
||
10 | class King { |
||
11 | /** |
||
12 | * init |
||
13 | */ |
||
14 | constructor(color) { |
||
15 | 4 | this.symbol = "K"; |
|
16 | 4 | this.moved = 0; |
|
17 | 4 | this.active = true; |
|
18 | 4 | this.color = color; |
|
19 | 4 | this.rows = [null, "A", "B", "C", "D", "E", "F", "G", "H"]; |
|
20 | 4 | 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 | 4 | if (xStep > 1 || yStep > 1) { |
|
30 | return false |
||
31 | } |
||
32 | |||
33 | 6 | if (x == nx && y !== ny || x !== nx && y == ny) { |
|
34 | return true |
||
35 | } |
||
36 | |||
37 | 2 | if (xStep === yStep) { |
|
38 | return true |
||
39 | } |
||
40 | |||
41 | return false; |
||
42 | } |
||
43 | } |
||
44 | |||
45 | |||
46 | module.exports = King; |
||
47 |