X-Git-Url: https://git.auder.net/?a=blobdiff_plain;ds=sidebyside;f=public%2Fjavascripts%2Fvariants%2FAlice.js;fp=public%2Fjavascripts%2Fvariants%2FAlice.js;h=b7f5a3e1cd77ab191dea341bfe9d82d72fa9f7fb;hb=4b3539364e8fea527158f4ba27db1c0870ffd2fc;hp=9a7cefa0a344267b51b0e8f4100023444db95241;hpb=a0f5dbaac2a9c028aa0ca2559d157b34aeafd3c9;p=vchess.git diff --git a/public/javascripts/variants/Alice.js b/public/javascripts/variants/Alice.js index 9a7cefa0..b7f5a3e1 100644 --- a/public/javascripts/variants/Alice.js +++ b/public/javascripts/variants/Alice.js @@ -1,3 +1,4 @@ +// NOTE: alternative implementation, probably cleaner = use only 1 board class AliceRules extends ChessRules { static get ALICE_PIECES() @@ -82,7 +83,6 @@ class AliceRules extends ChessRules } // NOTE: castle & enPassant https://www.chessvariants.com/other.dir/alice.html - // --> Should be OK as is. getPotentialMovesFrom([x,y], sideBoard) { const pieces = Object.keys(VariantRules.ALICE_CODES); @@ -150,17 +150,12 @@ class AliceRules extends ChessRules return res; } - // NOTE: alternative implementation, recompute sideBoard's in this function - filterValid(moves, sideBoard) + filterValid(moves) { if (moves.length == 0) return []; - const pieces = Object.keys(VariantRules.ALICE_CODES); - return moves.filter(m => { - // WARNING: for underCheck(), we need the sideBoard of the arrival world ! - const mirrorSide = (pieces.includes(this.getPiece(m.start.x,m.start.y)) ? 2 : 1); - return !this.underCheck(m, !!sideBoard ? sideBoard[mirrorSide-1] : null); - }); + let sideBoard = [this.getSideBoard(1), this.getSideBoard(2)]; + return moves.filter(m => { return !this.underCheck(m, sideBoard); }); } getAllValidMoves() @@ -186,18 +181,51 @@ class AliceRules extends ChessRules return this.filterValid(potentialMoves, sideBoard); } - underCheck(move, sideBoard) + // Play on sideboards [TODO: only one sideBoard required] + playSide(move, sideBoard) + { + const pieces = Object.keys(VariantRules.ALICE_CODES); + move.vanish.forEach(psq => { + const mirrorSide = (pieces.includes(psq.p) ? 1 : 2); + sideBoard[mirrorSide-1][psq.x][psq.y] = VariantRules.EMPTY; + }); + move.appear.forEach(psq => { + const mirrorSide = (pieces.includes(psq.p) ? 1 : 2); + const piece = (mirrorSide == 1 ? psq.p : VariantRules.ALICE_PIECES[psq.p]); + sideBoard[mirrorSide-1][psq.x][psq.y] = psq.c + piece; + if (piece == VariantRules.KING) + this.kingPos[psq.c] = [psq.x,psq.y]; + }); + } + + // Undo on sideboards + undoSide(move, sideBoard) { - const color = this.turn; - this.play(move); const pieces = Object.keys(VariantRules.ALICE_CODES); + move.appear.forEach(psq => { + const mirrorSide = (pieces.includes(psq.p) ? 1 : 2); + sideBoard[mirrorSide-1][psq.x][psq.y] = VariantRules.EMPTY; + }); + move.vanish.forEach(psq => { + const mirrorSide = (pieces.includes(psq.p) ? 1 : 2); + const piece = (mirrorSide == 1 ? psq.p : VariantRules.ALICE_PIECES[psq.p]); + sideBoard[mirrorSide-1][psq.x][psq.y] = psq.c + piece; + if (piece == VariantRules.KING) + this.kingPos[psq.c] = [psq.x,psq.y]; + }); + } + + underCheck(move, sideBoard) //sideBoard arg always provided + { + const color = this.turn; + this.playSide(move, sideBoard); //no need to track flags const kp = this.kingPos[color]; - const mirrorSide = (pieces.includes(this.getPiece(kp[0],kp[1])) ? 1 : 2); + const mirrorSide = sideBoard[0][kp[0]][kp[1]] != VariantRules.EMPTY ? 1 : 2; let saveBoard = this.board; - this.board = sideBoard || this.getSideBoard(mirrorSide); - let res = this.isAttacked(this.kingPos[color], this.getOppCol(color)); + this.board = sideBoard[mirrorSide-1]; + let res = this.isAttacked(kp, this.getOppCol(color)); this.board = saveBoard; - this.undo(move); + this.undoSide(move, sideBoard); return res; }