Improve FEN length for Eightpieces variant
[vchess.git] / client / src / variants / Checkless.js
CommitLineData
0d5335de
BA
1import { ChessRules } from "@/base_rules";
2
3export class ChecklessRules extends ChessRules {
4 // Cannot use super.atLeastOneMove: lead to infinite recursion
5 atLeastOneMove_aux() {
6 const color = this.turn;
7 const oppCol = V.GetOppCol(color);
8 for (let i = 0; i < V.size.x; i++) {
9 for (let j = 0; j < V.size.y; j++) {
10 if (this.getColor(i, j) == color) {
11 const moves = this.getPotentialMovesFrom([i, j]);
12 if (moves.length > 0) {
13 for (let k = 0; k < moves.length; k++) {
14 let res = false;
15 this.play(moves[k]);
16 res = !this.underCheck(color) && !this.underCheck(oppCol);
17 this.undo(moves[k]);
18 if (res) return true;
19 }
20 }
21 }
22 }
23 }
24 return false;
25 }
26
27 filterValid(moves) {
28 if (moves.length == 0) return [];
29 const color = this.turn;
30 const oppCol = V.GetOppCol(color);
31 return moves.filter(m => {
32 this.play(m);
33 // Move shouldn't result in self-check:
34 let res = !this.underCheck(color);
35 if (res) {
36 const checkOpp = this.underCheck(oppCol);
37 // If checking the opponent, he shouldn't have any legal move:
38 res = !checkOpp || checkOpp && !this.atLeastOneMove_aux();
39 }
40 this.undo(m);
41 return res;
42 });
43 }
44};