Fix Eightpieces
[vchess.git] / client / src / variants / Capture.js
... / ...
CommitLineData
1import { ChessRules } from "@/base_rules";
2
3export class CaptureRules extends ChessRules {
4
5 // Trim all non-capturing moves
6 static KeepCaptures(moves) {
7 return moves.filter(m => m.vanish.length == 2 && m.appear.length == 1);
8 }
9
10 // Stop at the first capture found (if any)
11 atLeastOneCapture() {
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++) {
15 if (
16 this.board[i][j] != V.EMPTY &&
17 this.getColor(i, j) == color &&
18 this.filterValid(this.getPotentialMovesFrom([i, j])).some(m => {
19 return (
20 // Warning: discard castle moves
21 m.vanish.length == 2 && m.appear.length == 1 &&
22 this.filterValid([m]).length == 1
23 );
24 })
25 ) {
26 return true;
27 }
28 }
29 }
30 return false;
31 }
32
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 [];
38 return moves;
39 }
40
41 getAllValidMoves() {
42 const moves = super.getAllValidMoves();
43 if (moves.some(m => m.vanish.length == 2 && m.appear.length == 1))
44 return V.KeepCaptures(moves);
45 return moves;
46 }
47
48};