1e7bfabb8fd0d09d77eb05c388835a8f6c1726ca
1 import { ChessRules
} from "@/base_rules";
3 export class Knightrelay1Rules
extends ChessRules
{
5 static get HasEnpassant() {
9 // TODO: IsGoodPosition to check that 2 knights are on the board...
11 getPotentialMovesFrom([x
, y
]) {
12 let moves
= super.getPotentialMovesFrom([x
, y
]);
14 // Expand possible moves if guarded by a knight, and is not a king:
15 const piece
= this.getPiece(x
,y
);
16 if (![V
.KNIGHT
,V
.KING
].includes(piece
)) {
17 const color
= this.turn
;
18 let guardedByKnight
= false;
19 for (const step
of V
.steps
[V
.KNIGHT
]) {
21 V
.OnBoard(x
+step
[0],y
+step
[1]) &&
22 this.getPiece(x
+step
[0],y
+step
[1]) == V
.KNIGHT
&&
23 this.getColor(x
+step
[0],y
+step
[1]) == color
25 guardedByKnight
= true;
29 if (guardedByKnight
) {
30 const lastRank
= (color
== "w" ? 0 : V
.size
.x
- 1);
31 for (const step
of V
.steps
[V
.KNIGHT
]) {
33 V
.OnBoard(x
+step
[0],y
+step
[1]) &&
34 this.getColor(x
+step
[0],y
+step
[1]) != color
&&
35 // Pawns cannot promote by knight-relay
36 (piece
!= V
.PAWN
|| x
+step
[0] != lastRank
)
38 moves
.push(this.getBasicMove([x
,y
], [x
+step
[0],y
+step
[1]]));
44 // Forbid captures of knights (invincible in this variant)
45 return moves
.filter(m
=> {
47 m
.vanish
.length
== 1 ||
48 m
.appear
.length
== 2 ||
49 m
.vanish
[1].p
!= V
.KNIGHT
54 getPotentialKnightMoves(sq
) {
55 // Knights don't capture:
56 return super.getPotentialKnightMoves(sq
).filter(m
=> m
.vanish
.length
== 1);
59 isAttacked(sq
, color
) {
60 if (super.isAttacked(sq
, color
)) return true;
62 // Check if a (non-knight) piece at knight distance
63 // is guarded by a knight (and thus attacking)
64 // --> Except for pawns targetting last rank.
67 // Last rank for me, that is to say oppCol of color:
68 const lastRank
= (color
== 'w' ? V
.size
.x
- 1 : 0);
69 for (const step
of V
.steps
[V
.KNIGHT
]) {
71 V
.OnBoard(x
+step
[0],y
+step
[1]) &&
72 this.getColor(x
+step
[0],y
+step
[1]) == color
74 const piece
= this.getPiece(x
+step
[0],y
+step
[1]);
75 if (piece
!= V
.KNIGHT
&& (piece
!= V
.PAWN
|| x
!= lastRank
)) {
76 for (const step2
of V
.steps
[V
.KNIGHT
]) {
77 const xx
= x
+step
[0]+step2
[0],
78 yy
= y
+step
[1]+step2
[1];
81 this.getColor(xx
,yy
) == color
&&
82 this.getPiece(xx
,yy
) == V
.KNIGHT
94 isAttackedByKnight(sq
, color
) {
95 // Knights don't attack
103 n: 0, //the knight isn't captured - value doesn't matter
110 static get SEARCH_DEPTH() {
115 if (move.appear
.length
== 2 && move.appear
[0].p
== V
.KING
)
117 return move.end
.y
< move.start
.y
? "0-0-0" : "0-0";
119 // Translate final and initial square
120 const initSquare
= V
.CoordsToSquare(move.start
);
121 const finalSquare
= V
.CoordsToSquare(move.end
);
122 const piece
= this.getPiece(move.start
.x
, move.start
.y
);
124 // Since pieces and pawns could move like knight,
125 // indicate start and end squares
127 piece
.toUpperCase() +
129 (move.vanish
.length
> move.appear
.length
? "x" : "") +
134 move.appear
.length
> 0 &&
135 move.appear
[0].p
!= V
.PAWN
138 notation
+= "=" + move.appear
[0].p
.toUpperCase();