1 import { ChessRules
} from "@/base_rules";
3 export const VariantRules
= class KnightrelayRules
extends ChessRules
{
4 getPotentialMovesFrom([x
, y
]) {
5 let moves
= super.getPotentialMovesFrom([x
, y
]);
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
]) {
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
17 guardedByKnight
= true;
21 if (guardedByKnight
) {
22 for (const step
of V
.steps
[V
.KNIGHT
]) {
24 V
.OnBoard(x
+step
[0],y
+step
[1]) &&
25 this.getColor(x
+step
[0],y
+step
[1]) != color
27 moves
.push(this.getBasicMove([x
,y
], [x
+step
[0],y
+step
[1]]));
36 isAttacked(sq
, colors
) {
37 if (super.isAttacked(sq
, colors
))
40 // Check if a (non-knight) piece at knight distance
41 // is guarded by a knight (and thus attacking)
44 for (const step
of V
.steps
[V
.KNIGHT
]) {
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
50 for (const step2
of V
.steps
[V
.KNIGHT
]) {
51 const xx
= x
+step
[0]+step2
[0],
52 yy
= y
+step
[1]+step2
[1];
55 colors
.includes(this.getColor(xx
,yy
)) &&
56 this.getPiece(xx
,yy
) == V
.KNIGHT
68 if (move.appear
.length
== 2 && move.appear
[0].p
== V
.KING
)
70 return move.end
.y
< move.start
.y
? "0-0-0" : "0-0";
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
);
77 // Since pieces and pawns could move like knight, indicate start and end squares
81 (move.vanish
.length
> move.appear
.length
? "x" : "") +
86 move.appear
.length
> 0 &&
87 move.appear
[0].p
!= V
.PAWN
90 notation
+= "=" + move.appear
[0].p
.toUpperCase();