X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=client%2Fsrc%2Fbase_rules.js;h=267b2335d8da4ac163b0970edf6a1b5235fd0846;hp=cd4bb77805a483e516afc2eca5c373e3984f123c;hb=a68362420a3a92099dfaacea10f6cbd579161183;hpb=a34caaced0796c9118a12c609463582d656e9daf diff --git a/client/src/base_rules.js b/client/src/base_rules.js index cd4bb778..267b2335 100644 --- a/client/src/base_rules.js +++ b/client/src/base_rules.js @@ -810,7 +810,8 @@ export const ChessRules = class ChessRules { return moves; } - getCastleMoves([x, y]) { + // "castleInCheck" arg to let some variants castle under check + getCastleMoves([x, y], castleInCheck) { const c = this.getColor(x, y); if (x != (c == "w" ? V.size.x - 1 : 0) || y != this.INIT_COL_KING[c]) return []; //x isn't first rank, or king has moved (shortcut) @@ -832,7 +833,9 @@ export const ChessRules = class ChessRules { if (this.castleFlags[c][castleSide] >= V.size.y) continue; // If this code is reached, rooks and king are on initial position + // NOTE: in some variants this is not a rook, but let's keep variable name const rookPos = this.castleFlags[c][castleSide]; + const castlingPiece = this.getPiece(x, rookPos); if (this.getColor(x, rookPos) != c) // Rook is here but changed color (see Benedict) continue; @@ -843,11 +846,11 @@ export const ChessRules = class ChessRules { i = y; do { if ( - this.isAttacked([x, i], oppCol) || + (!castleInCheck && this.isAttacked([x, i], oppCol)) || (this.board[x][i] != V.EMPTY && // NOTE: next check is enough, because of chessboard constraints (this.getColor(x, i) != c || - ![V.KING, V.ROOK].includes(this.getPiece(x, i)))) + ![V.KING, castlingPiece].includes(this.getPiece(x, i)))) ) { continue castlingCheck; } @@ -876,11 +879,11 @@ export const ChessRules = class ChessRules { new Move({ appear: [ new PiPo({ x: x, y: finalSquares[castleSide][0], p: V.KING, c: c }), - new PiPo({ x: x, y: finalSquares[castleSide][1], p: V.ROOK, c: c }) + new PiPo({ x: x, y: finalSquares[castleSide][1], p: castlingPiece, c: c }) ], vanish: [ new PiPo({ x: x, y: y, p: V.KING, c: c }), - new PiPo({ x: x, y: rookPos, p: V.ROOK, c: c }) + new PiPo({ x: x, y: rookPos, p: castlingPiece, c: c }) ], end: Math.abs(y - rookPos) <= 2