1 import { ChessRules
} from "@/base_rules";
3 export class CaptureRules
extends ChessRules
{
5 // Trim all non-capturing moves
6 static KeepCaptures(moves
) {
7 return moves
.filter(m
=> m
.vanish
.length
== 2 && m
.appear
.length
== 1);
10 // Stop at the first capture found (if any)
12 const color
= this.turn
;
13 for (let i
= 0; i
< V
.size
.x
; i
++) {
14 for (let j
= 0; j
< V
.size
.y
; j
++) {
16 this.board
[i
][j
] != V
.EMPTY
&&
17 this.getColor(i
, j
) == color
&&
18 this.filterValid(this.getPotentialMovesFrom([i
, j
])).some(m
=> {
20 // Warning: discard castle moves
21 m
.vanish
.length
== 2 && m
.appear
.length
== 1 &&
22 this.filterValid([m
]).length
== 1
33 getPossibleMovesFrom(sq
) {
34 let moves
= this.filterValid(this.getPotentialMovesFrom(sq
));
35 const captureMoves
= V
.KeepCaptures(moves
);
36 if (captureMoves
.length
> 0) return captureMoves
;
37 if (this.atLeastOneCapture()) return [];
42 const moves
= super.getAllValidMoves();
43 if (moves
.some(m
=> m
.vanish
.length
== 2 && m
.appear
.length
== 1))
44 return V
.KeepCaptures(moves
);