Fix Checkless and trackKingWrap()
[xogo.git] / variants / Checkless / class.js
1 import ChessRules from "/base_rules.js";
2
3 export default class ChecklessRules extends ChessRules {
4
5 // Cannot use super.atLeastOneMove: lead to infinite recursion
6 atLeastOneMove_aux(kingPos, oppKingPos, color, oppCol) {
7 for (let i = 0; i < this.size.x; i++) {
8 for (let j = 0; j < this.size.y; j++) {
9 if (this.getColor(i, j) == color) {
10 const moves = this.getPotentialMovesFrom([i, j]);
11 for (let m of moves) {
12 this.playOnBoard(m);
13 const res = this.trackKingWrap(m, kingPos, (kp) => {
14 return !this.underCheck(kp, [oppCol]);
15 });
16 this.undoOnBoard(m);
17 if (res)
18 return true;
19 }
20 }
21 }
22 }
23 return false;
24 }
25
26 filterValid(moves) {
27 const fmoves = super.filterValid(moves);
28 // Filter out moves giving check but not checkmate
29 const color = this.turn;
30 const oppCol = C.GetOppTurn(color);
31 let kingPos = this.searchKingPos(color),
32 oppKingPos = this.searchKingPos(oppCol);
33 return fmoves.filter(m => {
34 this.playOnBoard(m);
35 const res = this.trackKingWrap(m, oppKingPos, (oppKp) => {
36 return (
37 !this.underCheck(oppKp, [color]) ||
38 !this.atLeastOneMove_aux(oppKp, kingPos, oppCol, color)
39 );
40 });
41 this.undoOnBoard(m);
42 return res;
43 });
44 }
45
46 };