X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fvariants%2FAlice.js;h=5aa9ee8182ccc3d3f3cbda7e726ab2b11ca34ade;hb=179af6dd72b6194e7512bc076d1901a6c6ea07d8;hp=761682750c5820316d0668376e3d096d98e570b1;hpb=b8121223012a3eb331022adc465bbfb4014363c8;p=vchess.git diff --git a/public/javascripts/variants/Alice.js b/public/javascripts/variants/Alice.js index 76168275..5aa9ee81 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); @@ -132,7 +132,8 @@ class AliceRules extends ChessRules }); } // Fix en-passant captures - if (m.vanish.length == 2 && this.board[m.end.x][m.end.y] == VariantRules.EMPTY) + if (m.vanish[0].p == VariantRules.PAWN + && m.vanish.length == 2 && this.board[m.end.x][m.end.y] == VariantRules.EMPTY) { m.vanish[1].c = this.getOppCol(this.getColor(x,y)); // In the special case of en-passant, if @@ -149,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() @@ -185,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; } @@ -210,7 +239,7 @@ class AliceRules extends ChessRules let sideBoard = this.getSideBoard(mirrorSide); let saveBoard = this.board; this.board = sideBoard; - let res = this.isAttacked(this.kingPos[color], this.getOppCol(color)) + let res = this.isAttacked(this.kingPos[color], [this.getOppCol(color)]) ? [ JSON.parse(JSON.stringify(this.kingPos[color])) ] : [ ]; this.board = saveBoard; @@ -250,7 +279,7 @@ class AliceRules extends ChessRules let saveBoard = this.board; this.board = sideBoard; let res = "*"; - if (!this.isAttacked(this.kingPos[color], this.getOppCol(color))) + if (!this.isAttacked(this.kingPos[color], [this.getOppCol(color)])) res = "1/2"; else res = (color == "w" ? "0-1" : "1-0");