1 import { ChessRules
} from "@/base_rules";
3 export const VariantRules
= class Knightrelay1Rules
extends ChessRules
{
4 static get HasEnpassant() {
8 getPotentialMovesFrom([x
, y
]) {
9 let moves
= super.getPotentialMovesFrom([x
, y
]);
11 // Expand possible moves if guarded by a knight, and is not a king:
12 const piece
= this.getPiece(x
,y
);
13 if (![V
.KNIGHT
,V
.KING
].includes(piece
)) {
14 const color
= this.turn
;
15 let guardedByKnight
= false;
16 for (const step
of V
.steps
[V
.KNIGHT
]) {
18 V
.OnBoard(x
+step
[0],y
+step
[1]) &&
19 this.getPiece(x
+step
[0],y
+step
[1]) == V
.KNIGHT
&&
20 this.getColor(x
+step
[0],y
+step
[1]) == color
22 guardedByKnight
= true;
26 if (guardedByKnight
) {
27 const lastRank
= (color
== "w" ? 0 : V
.size
.x
- 1);
28 for (const step
of V
.steps
[V
.KNIGHT
]) {
30 V
.OnBoard(x
+step
[0],y
+step
[1]) &&
31 this.getColor(x
+step
[0],y
+step
[1]) != color
&&
32 // Pawns cannot promote by knight-relay
33 (piece
!= V
.PAWN
|| x
+step
[0] != lastRank
)
35 moves
.push(this.getBasicMove([x
,y
], [x
+step
[0],y
+step
[1]]));
41 // Forbid captures of knights (invincible in this variant)
42 return moves
.filter(m
=> {
44 m
.vanish
.length
== 1 ||
45 m
.appear
.length
== 2 ||
46 m
.vanish
[1].p
!= V
.KNIGHT
51 getPotentialKnightMoves(sq
) {
52 // Knights don't capture:
53 return super.getPotentialKnightMoves(sq
).filter(m
=> m
.vanish
.length
== 1);
56 isAttacked(sq
, color
) {
57 if (super.isAttacked(sq
, color
)) return true;
59 // Check if a (non-knight) piece at knight distance
60 // is guarded by a knight (and thus attacking)
61 // --> Except for pawns targetting last rank.
64 // Last rank for me, that is to say oppCol of color:
65 const lastRank
= (color
== 'w' ? V
.size
.x
- 1 : 0);
66 for (const step
of V
.steps
[V
.KNIGHT
]) {
68 V
.OnBoard(x
+step
[0],y
+step
[1]) &&
69 this.getColor(x
+step
[0],y
+step
[1]) == color
71 const piece
= this.getPiece(x
+step
[0],y
+step
[1]);
72 if (piece
!= V
.KNIGHT
&& (piece
!= V
.PAWN
|| x
!= lastRank
)) {
73 for (const step2
of V
.steps
[V
.KNIGHT
]) {
74 const xx
= x
+step
[0]+step2
[0],
75 yy
= y
+step
[1]+step2
[1];
78 this.getColor(xx
,yy
) == color
&&
79 this.getPiece(xx
,yy
) == V
.KNIGHT
91 isAttackedByKnight(sq
, color
) {
92 // Knights don't attack
100 n: 7, //the knight is valuable
107 static get SEARCH_DEPTH() {
112 if (move.appear
.length
== 2 && move.appear
[0].p
== V
.KING
)
114 return move.end
.y
< move.start
.y
? "0-0-0" : "0-0";
116 // Translate final and initial square
117 const initSquare
= V
.CoordsToSquare(move.start
);
118 const finalSquare
= V
.CoordsToSquare(move.end
);
119 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
121 // Since pieces and pawns could move like knight, indicate start and end squares
123 piece
.toUpperCase() +
125 (move.vanish
.length
> move.appear
.length
? "x" : "") +
130 move.appear
.length
> 0 &&
131 move.appear
[0].p
!= V
.PAWN
134 notation
+= "=" + move.appear
[0].p
.toUpperCase();