X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FAllmate1.js;h=88599e49c69719655cccda1c2c176f697c6d8f41;hb=7e8a7ea1cb66adb4a987badfb0a3c2f99a21bd0a;hp=d07474b3ef8e8918a42aeac0f7dad2f12b0a4dcb;hpb=bb688df52df0713aba7b2c1c068614544f5ae96d;p=vchess.git diff --git a/client/src/variants/Allmate1.js b/client/src/variants/Allmate1.js index d07474b3..88599e49 100644 --- a/client/src/variants/Allmate1.js +++ b/client/src/variants/Allmate1.js @@ -1,6 +1,7 @@ import { ChessRules, PiPo, Move } from "@/base_rules"; export class Allmate1Rules extends ChessRules { + static get HasEnpassant() { return false; } @@ -27,72 +28,97 @@ export class Allmate1Rules extends ChessRules { const oppCol = V.GetOppCol(this.turn); moves.forEach(m => { this.play(m); + let allAttacks = []; + + // While something has been captured: + // remove it, and keep looking for captures + outerLoop: while (true) { - // 1) What is attacked? - let attacked = {}; - for (let i=0; i infinite recursion - outerLoop: for (let i=0; i { - const origSq = [sq[0], sq[1]]; - if (om.start.x == sq[0] && om.start.y == sq[1]) - // Piece moved: - sq = [om.appear[0].x, om.appear[0].y]; - if (!this.isAttacked(sq, color)) - delete attacked[origSq[0]+"_"+origSq[1]]; - }); - V.UndoOnBoard(this.board, om); - if (Object.keys(attacked).length == 0) - // No need to explore more moves - break outerLoop; + // 2) Among attacked pieces, which cannot escape capture? + // Avoid "oppMoves = this.getAllValidMoves();" => infinite recursion + for (let i=0; i { + const origSq = [sq[0], sq[1]]; + if (om.start.x == sq[0] && om.start.y == sq[1]) + // Piece moved: + sq = [om.appear[0].x, om.appear[0].y]; + if (!this.isAttacked(sq, color)) + delete attacked[origSq[0]+"_"+origSq[1]]; + }); + V.UndoOnBoard(this.board, om); + if (Object.keys(attacked).length == 0) + // No need to explore more moves + break outerLoop; + } } } } + + // 3) Add mate-captures and remove pieces from board: + Object.keys(attacked).forEach(k => { + allAttacks.push({ + sq: attacked[k], + p: this.getPiece(attacked[k][0], attacked[k][1]) + }); + this.board[attacked[k][0]][attacked[k][1]] = V.EMPTY; + }); } - // 3) Add mate-captures: - Object.values(attacked).forEach(sq => { - m.vanish.push(new PiPo({ - x: sq[0], - y: sq[1], - c: oppCol, - p: this.getPiece(sq[0], sq[1]) - })); + // Put removed pieces back on board: + allAttacks.forEach(v => { + this.board[v.sq[0]][v.sq[1]] = oppCol + v.p; }); - this.undo(m); + allAttacks.forEach(v => { + m.vanish.push( + new PiPo({ + x: v.sq[0], + y: v.sq[1], + c: oppCol, + p: v.p + }) + ); + }); }); return moves; @@ -100,7 +126,7 @@ export class Allmate1Rules extends ChessRules { // No "under check" conditions in castling getCastleMoves(sq) { - return super.getCastleMoves(sq, "castleInCheck"); + return super.getCastleMoves(sq, null, "castleInCheck"); } // TODO: allow pieces to "commit suicide"? (Currently yes except king) @@ -176,10 +202,10 @@ export class Allmate1Rules extends ChessRules { this.kingPos[this.turn] = [-1, -1]; // Or maybe a rook? else if (v.p == V.ROOK) { - if (v.y < this.INIT_COL_KING[v.c]) + if (v.y < this.kingPos[v.c][1]) this.castleFlags[v.c][0] = 8; else - // v.y > this.INIT_COL_KING[v.c] + // v.y > this.kingPos[v.c][1] this.castleFlags[v.c][1] = 8; } } @@ -223,4 +249,5 @@ export class Allmate1Rules extends ChessRules { } return notation; } + };