Almost added TitanChess + EvolutionChess
[vchess.git] / client / src / variants / Capture.js
CommitLineData
6b7b2cf7 1import { ChessRules } from "@/base_rules";
6b7b2cf7 2
32f6285e 3export class CaptureRules extends ChessRules {
7e8a7ea1 4
6b7b2cf7
BA
5 // Trim all non-capturing moves
6 static KeepCaptures(moves) {
3a2a7b5f 7 return moves.filter(m => m.vanish.length == 2 && m.appear.length == 1);
6b7b2cf7
BA
8 }
9
801e2870 10 // Stop at the first capture found (if any)
6b7b2cf7
BA
11 atLeastOneCapture() {
12 const color = this.turn;
6b7b2cf7
BA
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 &&
5d416f0f
BA
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 })
6b7b2cf7
BA
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();
a34caace
BA
43 if (moves.some(m => m.vanish.length == 2 && m.appear.length == 1))
44 return V.KeepCaptures(moves);
6b7b2cf7
BA
45 return moves;
46 }
7e8a7ea1 47
6b7b2cf7 48};