1 import { ChessRules
} from "@/base_rules";
3 export class Knightrelay2Rules
extends ChessRules
{
5 getPotentialMovesFrom([x
, y
]) {
6 let moves
= super.getPotentialMovesFrom([x
, y
]);
8 // Expand possible moves if guarded by a knight:
9 const piece
= this.getPiece(x
,y
);
10 if (piece
!= V
.KNIGHT
) {
11 const color
= this.turn
;
12 let guardedByKnight
= false;
13 for (const step
of V
.steps
[V
.KNIGHT
]) {
15 V
.OnBoard(x
+step
[0],y
+step
[1]) &&
16 this.getPiece(x
+step
[0],y
+step
[1]) == V
.KNIGHT
&&
17 this.getColor(x
+step
[0],y
+step
[1]) == color
19 guardedByKnight
= true;
23 if (guardedByKnight
) {
24 const lastRank
= color
== "w" ? 0 : V
.size
.x
- 1;
25 for (const step
of V
.steps
[V
.KNIGHT
]) {
27 V
.OnBoard(x
+step
[0],y
+step
[1]) &&
28 this.getColor(x
+step
[0],y
+step
[1]) != color
30 // Potential promotions:
31 const finalPieces
= piece
== V
.PAWN
&& x
+ step
[0] == lastRank
32 ? [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
]
34 for (let p
of finalPieces
) {
36 this.getBasicMove([x
,y
], [x
+step
[0],y
+step
[1]], {
50 isAttacked(sq
, color
) {
51 if (super.isAttacked(sq
, color
)) return true;
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 this.getColor(x
+step
[0],y
+step
[1]) == color
&&
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 this.getColor(xx
,yy
) == color
&&
69 this.getPiece(xx
,yy
) == V
.KNIGHT
84 n: 7, //the knight is valuable
91 static get SEARCH_DEPTH() {
96 if (move.appear
.length
== 2 && move.appear
[0].p
== V
.KING
)
98 return move.end
.y
< move.start
.y
? "0-0-0" : "0-0";
100 // Translate final and initial square
101 const initSquare
= V
.CoordsToSquare(move.start
);
102 const finalSquare
= V
.CoordsToSquare(move.end
);
103 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
105 // Since pieces and pawns could move like knight,
106 // indicate start and end squares
108 piece
.toUpperCase() +
110 (move.vanish
.length
> move.appear
.length
? "x" : "") +
115 move.appear
.length
> 0 &&
116 move.appear
[0].p
!= V
.PAWN
119 notation
+= "=" + move.appear
[0].p
.toUpperCase();