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