X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fvariants%2FCrazyhouse.js;h=b297f1fb78cbe776d3abb3d65e18ce2ac9d9da37;hb=9234226104764b91df9d677fb360ad538b98510c;hp=0ee4bd3788bb5898a70a67008b0aef458fb55e93;hpb=a6abf094c35a26019e47fea21302c4be32ff030b;p=vchess.git diff --git a/public/javascripts/variants/Crazyhouse.js b/public/javascripts/variants/Crazyhouse.js index 0ee4bd37..b297f1fb 100644 --- a/public/javascripts/variants/Crazyhouse.js +++ b/public/javascripts/variants/Crazyhouse.js @@ -2,7 +2,7 @@ class CrazyhouseRules extends ChessRules { initVariables(fen) { - super.initVariables(); + super.initVariables(fen); // Also init reserves (used by the interface to show landing pieces) const V = VariantRules; this.reserve = @@ -24,72 +24,190 @@ class CrazyhouseRules extends ChessRules [V.QUEEN]: 0, } }; - // It may be a continuation: adjust numbers of pieces according to captures + rebirths - // TODO + const [sizeX,sizeY] = VariantRules.size; + this.promoted = doubleArray(sizeX, sizeY, false); + // May be a continuation: adjust numbers of pieces in reserve + promoted pieces + this.moves.forEach(m => { this.updateVariables(m); }); + } + + getColor(i,j) + { + const sizeX = VariantRules.size[0]; + if (i >= sizeX) + return (i==sizeX ? "w" : "b"); + return this.board[i][j].charAt(0); + } + getPiece(i,j) + { + const sizeX = VariantRules.size[0]; + if (i >= sizeX) + return VariantRules.RESERVE_PIECES[j]; + return this.board[i][j].charAt(1); } // Used by the interface: - getReservePieces(color) + getReservePpath(color, index) { - return { - [color+V.PAWN]: this.reserve[color][V.PAWN], - [color+V.ROOK]: this.reserve[color][V.ROOK], - [color+V.KNIGHT]: this.reserve[color][V.KNIGHT], - [color+V.BISHOP]: this.reserve[color][V.BISHOP], - [color+V.QUEEN]: this.reserve[color][V.QUEEN], - }; + return color + VariantRules.RESERVE_PIECES[index]; } - getPotentialMovesFrom([x,y]) + // Ordering on reserve pieces + static get RESERVE_PIECES() { + const V = VariantRules; + return [V.PAWN,V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN]; + } + + getReserveMoves([x,y]) { - let moves = super.getPotentialMovesFrom([x,y]); - // Add landing moves: const color = this.turn; - Object.keys(this.reserve[color]).forEach(p => { - - moves.push(...); //concat... just appear - }); + const p = VariantRules.RESERVE_PIECES[y]; + if (this.reserve[color][p] == 0) + return []; + let moves = []; + const [sizeX,sizeY] = VariantRules.size; + const pawnShift = (p==VariantRules.PAWN ? 1 : 0); + for (let i=pawnShift; i special square !!! coordinates ?? - getPossibleMovesFrom(sq) + getPotentialMovesFrom([x,y]) { - // Assuming color is right (already checked) - return this.filterValid( this.getPotentialMovesFrom(sq) ); + const sizeX = VariantRules.size[0]; + if (x >= sizeX) + { + // Reserves, outside of board: x == sizeX(+1) + return this.getReserveMoves([x,y]); + } + // Standard moves + return super.getPotentialMovesFrom([x,y]); } - // TODO: add reserve moves getAllValidMoves() { - + let moves = super.getAllValidMoves(); + const color = this.turn; + const sizeX = VariantRules.size[0]; + for (let i=0; i 0) + return true; + } + return false; + } + return true; } - // TODO: update reserve updateVariables(move) { + super.updateVariables(move); + if (move.vanish.length == 2 && move.appear.length == 2) + return; //skip castle + const color = this.turn; + const V = VariantRules; + if (move.vanish.length == 0) + { + this.reserve[color][move.appear[0].p]--; + return; + } + move.movePromoted = this.promoted[move.start.x][move.start.y]; + move.capturePromoted = this.promoted[move.end.x][move.end.y] + this.promoted[move.start.x][move.start.y] = false; + this.promoted[move.end.x][move.end.y] = move.movePromoted + || (move.vanish[0].p == V.PAWN && move.appear[0].p != V.PAWN); + if (move.capturePromoted) + this.reserve[color][VariantRules.PAWN]++; + else if (move.vanish.length == 2) + this.reserve[color][move.vanish[1].p]++; } + unupdateVariables(move) { + super.unupdateVariables(move); + if (move.vanish.length == 2 && move.appear.length == 2) + return; + const color = this.turn; + const V = VariantRules; + if (move.vanish.length == 0) + { + this.reserve[color][move.appear[0].p]++; + return; + } + if (move.movePromoted) + this.promoted[move.start.x][move.start.y] = true; + this.promoted[move.end.x][move.end.y] = move.capturePromoted; + if (move.capturePromoted) + this.reserve[color][VariantRules.PAWN]--; + else if (move.vanish.length == 2) + this.reserve[color][move.vanish[1].p]--; } static get SEARCH_DEPTH() { return 2; } //high branching factor + evalPosition() + { + let evaluation = super.evalPosition(); + // Add reserves: + for (let i=0; i 0) return super.getNotation(move); // Rebirth: const piece = - (move.appear[0].p != VariantRules.PAWN ? move.appear.p.toUpperCase() : ""); + (move.appear[0].p != VariantRules.PAWN ? move.appear[0].p.toUpperCase() : ""); const finalSquare = String.fromCharCode(97 + move.end.y) + (VariantRules.size[0]-move.end.x); return piece + "@" + finalSquare; } + + getLongNotation(move) + { + if (move.vanish.length > 0) + return super.getLongNotation(move); + const finalSquare = + String.fromCharCode(97 + move.end.y) + (VariantRules.size[0]-move.end.x); + return "@" + finalSquare; + } }