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 if (squares
.length
> 2) return false;
8 for (let sq
of squares
) {
9 const ep
= V
.SquareToCoords(sq
);
10 if (isNaN(ep
.x
) || !V
.OnBoard(ep
)) return false;
17 return (b
[1] == V
.KNIGHT
? "Enpassant/" : "") + b
;
20 getEpSquare(moveOrSquare
) {
21 if (!moveOrSquare
) return undefined;
22 if (typeof moveOrSquare
=== "string") {
23 const square
= moveOrSquare
;
24 if (square
== "-") return undefined;
25 // Expand init + dest squares into a full path:
26 const init
= V
.SquareToCoords(square
.substr(0, 2));
28 if (square
.length
== 2) return newPath
;
29 const dest
= V
.SquareToCoords(square
.substr(2));
30 const delta
= ['x', 'y'].map(i
=> Math
.abs(dest
[i
] - init
[i
]));
31 // Check if it's a knight(rider) movement:
33 if (delta
[0] > 0 && delta
[1] > 0 && delta
[0] != delta
[1]) {
35 const minShift
= Math
.min(delta
[0], delta
[1]);
36 step
[0] = (dest
.x
- init
.x
) / minShift
;
37 step
[1] = (dest
.y
- init
.y
) / minShift
;
40 step
= ['x', 'y'].map((i
, idx
) => {
41 return (dest
[i
] - init
[i
]) / delta
[idx
] || 0
44 let x
= init
.x
+ step
[0],
46 while (x
!= dest
.x
|| y
!= dest
.y
) {
47 newPath
.push({ x: x
, y: y
});
54 // Argument is a move: all intermediate squares are en-passant candidates,
55 // except if the moving piece is a king.
56 const move = moveOrSquare
;
57 const piece
= move.appear
[0].p
;
58 if (piece
== V
.KING
||
60 Math
.abs(move.end
.x
-move.start
.x
) <= 1 &&
61 Math
.abs(move.end
.y
-move.start
.y
) <= 1
66 const delta
= [move.end
.x
-move.start
.x
, move.end
.y
-move.start
.y
];
68 if (piece
== V
.KNIGHT
) {
69 const divisor
= Math
.min(Math
.abs(delta
[0]), Math
.abs(delta
[1]));
70 step
= [delta
[0]/divisor
|| 0, delta
[1]/divisor
|| 0];
73 delta
[0]/Math
.abs(delta
[0]) || 0,
74 delta
[1]/Math
.abs(delta
[1]) || 0
79 let [x
,y
] = [move.start
.x
+step
[0],move.start
.y
+step
[1]];
80 x
!= move.end
.x
|| y
!= move.end
.y
;
81 x
+= step
[0], y
+= step
[1]
83 res
.push({ x: x
, y: y
});
85 // Add final square to know which piece is taken en passant:
91 const L
= this.epSquares
.length
;
92 if (!this.epSquares
[L
- 1]) return "-"; //no en-passant
93 const epsq
= this.epSquares
[L
- 1];
94 if (epsq
.length
<= 2) return epsq
.map(V
.CoordsToSquare
).join("");
95 // Condensate path: just need initial and final squares:
96 return V
.CoordsToSquare(epsq
[0]) + V
.CoordsToSquare(epsq
[epsq
.length
- 1]);
99 getPotentialMovesFrom([x
, y
]) {
100 let moves
= super.getPotentialMovesFrom([x
,y
]);
101 // Add en-passant captures from this square:
102 const L
= this.epSquares
.length
;
103 if (!this.epSquares
[L
- 1]) return moves
;
104 const squares
= this.epSquares
[L
- 1];
105 const S
= squares
.length
;
106 // Object describing the removed opponent's piece:
107 const pipoV
= new PiPo({
110 c: V
.GetOppCol(this.turn
),
111 p: this.getPiece(squares
[S
-1].x
, squares
[S
-1].y
)
113 // Check if existing non-capturing moves could also capture en passant
116 m
.appear
[0].p
!= V
.PAWN
&& //special pawn case is handled elsewhere
117 m
.vanish
.length
<= 1 &&
118 [...Array(S
-1).keys()].some(i
=> {
119 return m
.end
.x
== squares
[i
].x
&& m
.end
.y
== squares
[i
].y
;
122 m
.vanish
.push(pipoV
);
125 // Special case of the king knight's movement:
126 if (this.getPiece(x
, y
) == V
.KING
) {
127 V
.steps
[V
.KNIGHT
].forEach(step
=> {
128 const endX
= x
+ step
[0];
129 const endY
= y
+ step
[1];
131 V
.OnBoard(endX
, endY
) &&
132 [...Array(S
-1).keys()].some(i
=> {
133 return endX
== squares
[i
].x
&& endY
== squares
[i
].y
;
136 let enpassantMove
= this.getBasicMove([x
, y
], [endX
, endY
]);
137 enpassantMove
.vanish
.push(pipoV
);
138 moves
.push(enpassantMove
);
145 getEnpassantCaptures([x
, y
], shiftX
) {
146 const Lep
= this.epSquares
.length
;
147 const squares
= this.epSquares
[Lep
- 1];
150 const S
= squares
.length
;
151 const taken
= squares
[S
-1];
152 const pipoV
= new PiPo({
155 p: this.getPiece(taken
.x
, taken
.y
),
156 c: this.getColor(taken
.x
, taken
.y
)
158 [...Array(S
-1).keys()].forEach(i
=> {
159 const sq
= squares
[i
];
160 if (sq
.x
== x
+ shiftX
&& Math
.abs(sq
.y
- y
) == 1) {
161 let enpassantMove
= this.getBasicMove([x
, y
], [sq
.x
, sq
.y
]);
162 enpassantMove
.vanish
.push(pipoV
);
163 moves
.push(enpassantMove
);
170 // Remove the "onestep" condition: knight promote to knightrider:
171 getPotentialKnightMoves(sq
) {
172 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
]);
176 const filteredMoves
= super.filterValid(moves
);
177 // If at least one full move made, everything is allowed:
178 if (this.movesCount
>= 2)
179 return filteredMoves
;
180 // Else, forbid captures:
181 return filteredMoves
.filter(m
=> m
.vanish
.length
== 1);
184 isAttackedByKnight(sq
, color
) {
185 return this.isAttackedBySlideNJump(
193 static get SEARCH_DEPTH() {
197 static get VALUES() {