1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
3 export class EnpassantRules
extends ChessRules
{
4 static IsGoodEnpassant(enpassant
) {
5 if (enpassant
!= "-") {
6 const squares
= enpassant
.split(",");
7 for (let sq
of squares
) {
8 const ep
= V
.SquareToCoords(sq
);
9 if (isNaN(ep
.x
) || !V
.OnBoard(ep
)) return false;
15 getEpSquare(moveOrSquare
) {
16 if (!moveOrSquare
) return undefined;
17 if (typeof moveOrSquare
=== "string") {
18 const square
= moveOrSquare
;
19 if (square
== "-") return undefined;
21 square
.split(",").forEach(sq
=> {
22 res
.push(V
.SquareToCoords(sq
));
26 // Argument is a move: all intermediate squares are en-passant candidates,
27 // except if the moving piece is a king.
28 const move = moveOrSquare
;
29 const piece
= move.appear
[0].p
;
30 if (piece
== V
.KING
||
32 Math
.abs(move.end
.x
-move.start
.x
) <= 1 &&
33 Math
.abs(move.end
.y
-move.start
.y
) <= 1
38 const delta
= [move.end
.x
-move.start
.x
, move.end
.y
-move.start
.y
];
40 if (piece
== V
.KNIGHT
) {
41 const divisor
= Math
.min(Math
.abs(delta
[0]), Math
.abs(delta
[1]));
42 step
= [delta
[0]/divisor
|| 0, delta
[1]/divisor
|| 0];
45 delta
[0]/Math
.abs(delta
[0]) || 0,
46 delta
[1]/Math
.abs(delta
[1]) || 0
51 let [x
,y
] = [move.start
.x
+step
[0],move.start
.y
+step
[1]];
52 x
!= move.end
.x
|| y
!= move.end
.y
;
53 x
+= step
[0], y
+= step
[1]
57 // Add final square to know which piece is taken en passant:
63 const L
= this.epSquares
.length
;
64 if (!this.epSquares
[L
- 1]) return "-"; //no en-passant
66 this.epSquares
[L
- 1].forEach(sq
=> {
67 res
+= V
.CoordsToSquare(sq
) + ",";
69 return res
.slice(0, -1); //remove last comma
72 getPotentialMovesFrom([x
, y
]) {
73 let moves
= super.getPotentialMovesFrom([x
,y
]);
74 // Add en-passant captures from this square:
75 const L
= this.epSquares
.length
;
76 if (!this.epSquares
[L
- 1]) return moves
;
77 const squares
= this.epSquares
[L
- 1];
78 const S
= squares
.length
;
79 // Object describing the removed opponent's piece:
80 const pipoV
= new PiPo({
83 c: V
.GetOppCol(this.turn
),
84 p: this.getPiece(squares
[S
-1].x
, squares
[S
-1].y
)
86 // Check if existing non-capturing moves could also capture en passant
89 m
.appear
[0].p
!= V
.PAWN
&& //special pawn case is handled elsewhere
90 m
.vanish
.length
<= 1 &&
91 [...Array(S
-1).keys()].some(i
=> {
92 return m
.end
.x
== squares
[i
].x
&& m
.end
.y
== squares
[i
].y
;
98 // Special case of the king knight's movement:
99 if (this.getPiece(x
, y
) == V
.KING
) {
100 V
.steps
[V
.KNIGHT
].forEach(step
=> {
101 const endX
= x
+ step
[0];
102 const endY
= y
+ step
[1];
104 V
.OnBoard(endX
, endY
) &&
105 [...Array(S
-1).keys()].some(i
=> {
106 return endX
== squares
[i
].x
&& endY
== squares
[i
].y
;
109 let enpassantMove
= this.getBasicMove([x
, y
], [endX
, endY
]);
110 enpassantMove
.vanish
.push(pipoV
);
111 moves
.push(enpassantMove
);
118 getEnpassantCaptures([x
, y
], shiftX
) {
119 const Lep
= this.epSquares
.length
;
120 const squares
= this.epSquares
[Lep
- 1];
123 const S
= squares
.length
;
124 const taken
= squares
[S
-1];
125 const pipoV
= new PiPo({
128 p: this.getPiece(taken
.x
, taken
.y
),
129 c: this.getColor(taken
.x
, taken
.y
)
131 [...Array(S
-1).keys()].forEach(i
=> {
132 const sq
= squares
[i
];
133 if (sq
.x
== x
+ shiftX
&& Math
.abs(sq
.y
- y
) == 1) {
134 let enpassantMove
= this.getBasicMove([x
, y
], [sq
.x
, sq
.y
]);
135 enpassantMove
.vanish
.push(pipoV
);
136 moves
.push(enpassantMove
);
143 // Remove the "onestep" condition: knight promote to knightrider:
144 getPotentialKnightMoves(sq
) {
145 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
]);
149 const filteredMoves
= super.filterValid(moves
);
150 // If at least one full move made, everything is allowed:
151 if (this.movesCount
>= 2)
152 return filteredMoves
;
153 // Else, forbid captures:
154 return filteredMoves
.filter(m
=> m
.vanish
.length
== 1);
157 isAttackedByKnight(sq
, color
) {
158 return this.isAttackedBySlideNJump(
166 static get SEARCH_DEPTH() {
170 static get VALUES() {