1 import { ChessRules
} from "@/base_rules";
3 export class Knightrelay2Rules
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
, color
) {
50 if (super.isAttacked(sq
, color
)) return true;
52 // Check if a (non-knight) piece at knight distance
53 // is guarded by a knight (and thus attacking)
56 for (const step
of V
.steps
[V
.KNIGHT
]) {
58 V
.OnBoard(x
+step
[0],y
+step
[1]) &&
59 this.getColor(x
+step
[0],y
+step
[1]) == color
&&
60 this.getPiece(x
+step
[0],y
+step
[1]) != V
.KNIGHT
62 for (const step2
of V
.steps
[V
.KNIGHT
]) {
63 const xx
= x
+step
[0]+step2
[0],
64 yy
= y
+step
[1]+step2
[1];
67 this.getColor(xx
,yy
) == color
&&
68 this.getPiece(xx
,yy
) == V
.KNIGHT
83 n: 7, //the knight is valuable
90 static get SEARCH_DEPTH() {
95 if (move.appear
.length
== 2 && move.appear
[0].p
== V
.KING
)
97 return move.end
.y
< move.start
.y
? "0-0-0" : "0-0";
99 // Translate final and initial square
100 const initSquare
= V
.CoordsToSquare(move.start
);
101 const finalSquare
= V
.CoordsToSquare(move.end
);
102 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
104 // Since pieces and pawns could move like knight,
105 // indicate start and end squares
107 piece
.toUpperCase() +
109 (move.vanish
.length
> move.appear
.length
? "x" : "") +
114 move.appear
.length
> 0 &&
115 move.appear
[0].p
!= V
.PAWN
118 notation
+= "=" + move.appear
[0].p
.toUpperCase();