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 const piece
= this.getPiece(x
,y
);
9 if (piece
!= V
.KNIGHT
) {
10 const color
= this.turn
;
11 let guardedByKnight
= false;
12 for (const step
of V
.steps
[V
.KNIGHT
]) {
14 V
.OnBoard(x
+step
[0],y
+step
[1]) &&
15 this.getPiece(x
+step
[0],y
+step
[1]) == V
.KNIGHT
&&
16 this.getColor(x
+step
[0],y
+step
[1]) == color
18 guardedByKnight
= true;
22 if (guardedByKnight
) {
23 const lastRank
= color
== "w" ? 0 : V
.size
.x
- 1;
24 for (const step
of V
.steps
[V
.KNIGHT
]) {
26 V
.OnBoard(x
+step
[0],y
+step
[1]) &&
27 this.getColor(x
+step
[0],y
+step
[1]) != color
29 // Potential promotions:
30 const finalPieces
= piece
== V
.PAWN
&& x
+ step
[0] == lastRank
31 ? [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
]
33 for (let p
of finalPieces
) {
35 this.getBasicMove([x
,y
], [x
+step
[0],y
+step
[1]], {
49 isAttacked(sq
, colors
) {
50 if (super.isAttacked(sq
, colors
))
53 // Check if a (non-knight) piece at knight distance
54 // is guarded by a knight (and thus attacking)
57 for (const step
of V
.steps
[V
.KNIGHT
]) {
59 V
.OnBoard(x
+step
[0],y
+step
[1]) &&
60 colors
.includes(this.getColor(x
+step
[0],y
+step
[1])) &&
61 this.getPiece(x
+step
[0],y
+step
[1]) != V
.KNIGHT
63 for (const step2
of V
.steps
[V
.KNIGHT
]) {
64 const xx
= x
+step
[0]+step2
[0],
65 yy
= y
+step
[1]+step2
[1];
68 colors
.includes(this.getColor(xx
,yy
)) &&
69 this.getPiece(xx
,yy
) == V
.KNIGHT
84 n: 7, //the knight is valuable
92 if (move.appear
.length
== 2 && move.appear
[0].p
== V
.KING
)
94 return move.end
.y
< move.start
.y
? "0-0-0" : "0-0";
96 // Translate final and initial square
97 const initSquare
= V
.CoordsToSquare(move.start
);
98 const finalSquare
= V
.CoordsToSquare(move.end
);
99 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
101 // Since pieces and pawns could move like knight, indicate start and end squares
103 piece
.toUpperCase() +
105 (move.vanish
.length
> move.appear
.length
? "x" : "") +
110 move.appear
.length
> 0 &&
111 move.appear
[0].p
!= V
.PAWN
114 notation
+= "=" + move.appear
[0].p
.toUpperCase();