4b5ed33912e96ae4924e046e2e8b2df0a6b2c7e1
1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
3 export const VariantRules
= class EnpassantRules
extends ChessRules
{
5 static IsGoodEnpassant(enpassant
) {
6 if (enpassant
!= "-") {
7 const squares
= enpassant
.split(",");
8 for (let sq
of squares
) {
9 const ep
= V
.SquareToCoords(sq
);
10 if (isNaN(ep
.x
) || !V
.OnBoard(ep
)) return false;
16 getEpSquare(moveOrSquare
) {
17 if (!moveOrSquare
) return undefined;
18 if (typeof moveOrSquare
=== "string") {
19 const square
= moveOrSquare
;
20 if (square
== "-") return undefined;
22 square
.split(",").forEach(sq
=> {
23 res
.push(V
.SquareToCoords(sq
));
27 // Argument is a move: all intermediate squares are en-passant candidates,
28 // except if the moving piece is a king.
29 const move = moveOrSquare
;
30 const piece
= move.appear
[0].p
;
31 if (piece
== V
.KING
||
33 Math
.abs(move.end
.x
-move.start
.x
) <= 1 &&
34 Math
.abs(move.end
.y
-move.start
.y
) <= 1
39 const delta
= [move.end
.x
-move.start
.x
, move.end
.y
-move.start
.y
];
41 if (piece
== V
.KNIGHT
) {
42 const divisor
= Math
.min(Math
.abs(delta
[0]), Math
.abs(delta
[1]));
43 step
= [delta
[0]/divisor
|| 0, delta
[1]/divisor
|| 0];
45 step
= [delta
[0]/Math
.abs(delta
[0]) || 0, delta
[1]/Math
.abs(delta
[1]) || 0];
49 let [x
,y
] = [move.start
.x
+step
[0],move.start
.y
+step
[1]];
50 x
!= move.end
.x
|| y
!= move.end
.y
;
51 x
+= step
[0], y
+= step
[1]
55 // Add final square to know which piece is taken en passant:
61 const L
= this.epSquares
.length
;
62 if (!this.epSquares
[L
- 1]) return "-"; //no en-passant
64 this.epSquares
[L
- 1].forEach(sq
=> {
65 res
+= V
.CoordsToSquare(sq
) + ",";
67 return res
.slice(0, -1); //remove last comma
70 getPotentialMovesFrom([x
, y
]) {
71 let moves
= super.getPotentialMovesFrom([x
,y
]);
72 // Add en-passant captures from this square:
73 const L
= this.epSquares
.length
;
74 if (!this.epSquares
[L
- 1]) return moves
;
75 const squares
= this.epSquares
[L
- 1];
76 const S
= squares
.length
;
77 // Object describing the removed opponent's piece:
78 const pipoV
= new PiPo({
81 c: V
.GetOppCol(this.turn
),
82 p: this.getPiece(squares
[S
-1].x
, squares
[S
-1].y
)
84 // Check if existing non-capturing moves could also capture en passant
87 m
.appear
[0].p
!= V
.PAWN
&& //special pawn case is handled elsewhere
88 m
.vanish
.length
<= 1 &&
89 [...Array(S
-1).keys()].some(i
=> {
90 return m
.end
.x
== squares
[i
].x
&& m
.end
.y
== squares
[i
].y
;
96 // Special case of the king knight's movement:
97 if (this.getPiece(x
, y
) == V
.KING
) {
98 V
.steps
[V
.KNIGHT
].forEach(step
=> {
99 const endX
= x
+ step
[0];
100 const endY
= y
+ step
[1];
102 V
.OnBoard(endX
, endY
) &&
103 [...Array(S
-1).keys()].some(i
=> {
104 return endX
== squares
[i
].x
&& endY
== squares
[i
].y
;
107 let enpassantMove
= this.getBasicMove([x
, y
], [endX
, endY
]);
108 enpassantMove
.vanish
.push(pipoV
);
109 moves
.push(enpassantMove
);
116 // TODO: this getPotentialPawnMovesFrom() is mostly duplicated:
117 // it could be split in "capture", "promotion", "enpassant"...
118 getPotentialPawnMoves([x
, y
]) {
119 const color
= this.turn
;
121 const [sizeX
, sizeY
] = [V
.size
.x
, V
.size
.y
];
122 const shiftX
= color
== "w" ? -1 : 1;
123 const startRank
= color
== "w" ? sizeX
- 2 : 1;
124 const lastRank
= color
== "w" ? 0 : sizeX
- 1;
127 x
+ shiftX
== lastRank
128 ? [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
]
130 // One square forward
131 if (this.board
[x
+ shiftX
][y
] == V
.EMPTY
) {
132 for (let piece
of finalPieces
) {
134 this.getBasicMove([x
, y
], [x
+ shiftX
, y
], {
142 this.board
[x
+ 2 * shiftX
][y
] == V
.EMPTY
145 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shiftX
, y
]));
149 for (let shiftY
of [-1, 1]) {
152 y
+ shiftY
< sizeY
&&
153 this.board
[x
+ shiftX
][y
+ shiftY
] != V
.EMPTY
&&
154 this.canTake([x
, y
], [x
+ shiftX
, y
+ shiftY
])
156 for (let piece
of finalPieces
) {
158 this.getBasicMove([x
, y
], [x
+ shiftX
, y
+ shiftY
], {
168 const Lep
= this.epSquares
.length
;
169 const squares
= this.epSquares
[Lep
- 1];
171 const S
= squares
.length
;
172 const taken
= squares
[S
-1];
173 const pipoV
= new PiPo({
176 p: this.getPiece(taken
.x
, taken
.y
),
177 c: this.getColor(taken
.x
, taken
.y
)
179 [...Array(S
-1).keys()].forEach(i
=> {
180 const sq
= squares
[i
];
181 if (sq
.x
== x
+ shiftX
&& Math
.abs(sq
.y
- y
) == 1) {
182 let enpassantMove
= this.getBasicMove([x
, y
], [sq
.x
, sq
.y
]);
183 enpassantMove
.vanish
.push(pipoV
);
184 moves
.push(enpassantMove
);
192 // Remove the "onestep" condition: knight promote to knightrider:
193 getPotentialKnightMoves(sq
) {
194 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
]);
198 const filteredMoves
= super.filterValid(moves
);
199 // If at least one full move made, everything is allowed:
200 if (this.movesCount
>= 2)
201 return filteredMoves
;
202 // Else, forbid captures:
203 return filteredMoves
.filter(m
=> m
.vanish
.length
== 1);
206 isAttackedByKnight(sq
, colors
) {
207 return this.isAttackedBySlideNJump(
215 static get SEARCH_DEPTH() {
219 static get VALUES() {