Commit | Line | Data |
---|---|---|
c322a844 BA |
1 | import { ChessRules } from "@/base_rules"; |
2 | ||
3 | export const VariantRules = class KnightrelayRules extends ChessRules { | |
4 | getPotentialMovesFrom([x, y]) { | |
5 | let moves = super.getPotentialMovesFrom([x, y]); | |
6 | ||
7 | // Expand possible moves if guarded by a knight: | |
8 | if (this.getPiece(x,y) != V.KNIGHT) { | |
9 | const color = this.turn; | |
10 | let guardedByKnight = false; | |
11 | for (const step of V.steps[V.KNIGHT]) { | |
12 | if ( | |
13 | V.OnBoard(x+step[0],y+step[1]) && | |
14 | this.getPiece(x+step[0],y+step[1]) == V.KNIGHT && | |
15 | this.getColor(x+step[0],y+step[1]) == color | |
16 | ) { | |
17 | guardedByKnight = true; | |
18 | break; | |
19 | } | |
20 | } | |
21 | if (guardedByKnight) { | |
22 | for (const step of V.steps[V.KNIGHT]) { | |
23 | if ( | |
24 | V.OnBoard(x+step[0],y+step[1]) && | |
25 | this.getColor(x+step[0],y+step[1]) != color | |
26 | ) { | |
1c5bfdf2 | 27 | moves.push(this.getBasicMove([x,y], [x+step[0],y+step[1]])); |
c322a844 BA |
28 | } |
29 | } | |
30 | } | |
31 | } | |
32 | ||
33 | return moves; | |
34 | } | |
35 | ||
1c5bfdf2 BA |
36 | isAttacked(sq, colors) { |
37 | if (super.isAttacked(sq, colors)) | |
38 | return true; | |
39 | ||
40 | // Check if a (non-knight) piece at knight distance | |
41 | // is guarded by a knight (and thus attacking) | |
42 | const x = sq[0], | |
43 | y = sq[1]; | |
44 | for (const step of V.steps[V.KNIGHT]) { | |
45 | if ( | |
46 | V.OnBoard(x+step[0],y+step[1]) && | |
47 | colors.includes(this.getColor(x+step[0],y+step[1])) && | |
48 | this.getPiece(x+step[0],y+step[1]) != V.KNIGHT | |
49 | ) { | |
50 | for (const step2 of V.steps[V.KNIGHT]) { | |
51 | const xx = x+step[0]+step2[0], | |
52 | yy = y+step[1]+step2[1]; | |
53 | if ( | |
54 | V.OnBoard(xx,yy) && | |
55 | colors.includes(this.getColor(xx,yy)) && | |
56 | this.getPiece(xx,yy) == V.KNIGHT | |
57 | ) { | |
58 | return true; | |
59 | } | |
60 | } | |
61 | } | |
62 | } | |
63 | ||
64 | return false; | |
65 | } | |
66 | ||
c322a844 BA |
67 | getNotation(move) { |
68 | if (move.appear.length == 2 && move.appear[0].p == V.KING) | |
69 | // Castle | |
70 | return move.end.y < move.start.y ? "0-0-0" : "0-0"; | |
71 | ||
72 | // Translate final and initial square | |
73 | const initSquare = V.CoordsToSquare(move.start); | |
74 | const finalSquare = V.CoordsToSquare(move.end); | |
75 | const piece = this.getPiece(move.start.x, move.start.y); | |
76 | ||
77 | // Since pieces and pawns could move like knight, indicate start and end squares | |
78 | let notation = | |
79 | piece.toUpperCase() + | |
80 | initSquare + | |
81 | (move.vanish.length > move.appear.length ? "x" : "") + | |
82 | finalSquare | |
83 | ||
84 | if ( | |
85 | piece == V.PAWN && | |
86 | move.appear.length > 0 && | |
87 | move.appear[0].p != V.PAWN | |
88 | ) { | |
89 | // Promotion | |
90 | notation += "=" + move.appear[0].p.toUpperCase(); | |
91 | } | |
92 | ||
93 | return notation; | |
94 | } | |
95 | }; |