Early draft of TitanChess
[vchess.git] / client / src / variants / Capture.js
CommitLineData
6b7b2cf7 1import { ChessRules } from "@/base_rules";
6b7b2cf7 2
32f6285e 3export class CaptureRules extends ChessRules {
6b7b2cf7
BA
4 // Trim all non-capturing moves
5 static KeepCaptures(moves) {
3a2a7b5f 6 return moves.filter(m => m.vanish.length == 2 && m.appear.length == 1);
6b7b2cf7
BA
7 }
8
801e2870 9 // Stop at the first capture found (if any)
6b7b2cf7
BA
10 atLeastOneCapture() {
11 const color = this.turn;
6b7b2cf7
BA
12 for (let i = 0; i < V.size.x; i++) {
13 for (let j = 0; j < V.size.y; j++) {
14 if (
15 this.board[i][j] != V.EMPTY &&
5d416f0f
BA
16 this.getColor(i, j) == color &&
17 this.filterValid(this.getPotentialMovesFrom([i, j])).some(m => {
18 return (
19 // Warning: discard castle moves
20 m.vanish.length == 2 && m.appear.length == 1 &&
21 this.filterValid([m]).length == 1
22 );
23 })
6b7b2cf7
BA
24 ) {
25 return true;
26 }
27 }
28 }
29 return false;
30 }
31
32 getPossibleMovesFrom(sq) {
33 let moves = this.filterValid(this.getPotentialMovesFrom(sq));
34 const captureMoves = V.KeepCaptures(moves);
35 if (captureMoves.length > 0) return captureMoves;
36 if (this.atLeastOneCapture()) return [];
37 return moves;
38 }
39
40 getAllValidMoves() {
41 const moves = super.getAllValidMoves();
a34caace
BA
42 if (moves.some(m => m.vanish.length == 2 && m.appear.length == 1))
43 return V.KeepCaptures(moves);
6b7b2cf7
BA
44 return moves;
45 }
6b7b2cf7 46};