413356dd1df7cb51b0f076e8b568d83c6e70e7dc
1 import { ChessRules
} from "@/base_rules";
3 export class CaptureRules
extends ChessRules
{
4 // Trim all non-capturing moves
5 static KeepCaptures(moves
) {
6 return moves
.filter(m
=> m
.vanish
.length
== 2 && m
.appear
.length
== 1);
9 // Stop at the first capture found (if any)
11 const color
= this.turn
;
12 const oppCol
= V
.GetOppCol(color
);
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
) != oppCol
&&
18 this.filterValid(this.getPotentialMovesFrom([i
, j
])).some(m
=>
19 // Warning: discard castle moves
20 m
.vanish
.length
== 2 && m
.appear
.length
== 1)
29 getPossibleMovesFrom(sq
) {
30 let moves
= this.filterValid(this.getPotentialMovesFrom(sq
));
31 const captureMoves
= V
.KeepCaptures(moves
);
32 if (captureMoves
.length
> 0) return captureMoves
;
33 if (this.atLeastOneCapture()) return [];
38 const moves
= super.getAllValidMoves();
39 if (moves
.some(m
=> m
.vanish
.length
== 2 && m
.appear
.length
== 1))
40 return V
.KeepCaptures(moves
);