X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FAllmate1.js;h=0e7e8ed51f7e7bcb8ca529d4a6c6a91592cc3708;hb=0eebb0df047580d3f66c3bb2dcdce073b3802e62;hp=f3c0b2ec03c05f217409cef2a96565032e755091;hpb=8a9ed886b3e551e9179fce924349b6552ba574e7;p=vchess.git diff --git a/client/src/variants/Allmate1.js b/client/src/variants/Allmate1.js index f3c0b2ec..0e7e8ed5 100644 --- a/client/src/variants/Allmate1.js +++ b/client/src/variants/Allmate1.js @@ -27,77 +27,97 @@ export class Allmate1Rules extends ChessRules { const oppCol = V.GetOppCol(this.turn); moves.forEach(m => { this.play(m); + let allAttacks = []; - // 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;