X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FCheckered.js;h=d601d14a440cc90fe1feb425bc54ad3611943d41;hb=32f6285ee325a14286562a53baefc647201df2af;hp=0e089f38048f36b4eab74212e335da00f53f7bc0;hpb=3a2a7b5fd3c6bfd0752838094c27e1fb6172d109;p=vchess.git diff --git a/client/src/variants/Checkered.js b/client/src/variants/Checkered.js index 0e089f38..d601d14a 100644 --- a/client/src/variants/Checkered.js +++ b/client/src/variants/Checkered.js @@ -1,6 +1,6 @@ -import { ChessRules } from "@/base_rules"; +import { ChessRules, Move, PiPo } from "@/base_rules"; -export const VariantRules = class CheckeredRules extends ChessRules { +export class CheckeredRules extends ChessRules { static board2fen(b) { const checkered_codes = { p: "s", @@ -118,7 +118,7 @@ export const VariantRules = class CheckeredRules extends ChessRules { getPotentialMovesFrom([x, y]) { let standardMoves = super.getPotentialMovesFrom([x, y]); const lastRank = this.turn == "w" ? 0 : 7; - // King has to be treated differently (for castles) + // King is treated differently: it never turn checkered if (this.getPiece(x, y) == V.KING) return standardMoves; let moves = []; standardMoves.forEach(m => { @@ -126,8 +126,9 @@ export const VariantRules = class CheckeredRules extends ChessRules { if ( Math.abs(m.end.x - m.start.x) == 2 && !this.pawnFlags[this.turn][m.start.y] - ) + ) { return; //skip forbidden 2-squares jumps + } if ( this.board[m.end.x][m.end.y] == V.EMPTY && m.vanish.length == 2 && @@ -136,14 +137,16 @@ export const VariantRules = class CheckeredRules extends ChessRules { return; //checkered pawns cannot take en-passant } } - if (m.vanish.length == 1) moves.push(m); - // No capture + if (m.vanish.length == 1) + // No capture + moves.push(m); else { // A capture occured (m.vanish.length == 2) m.appear[0].c = "c"; moves.push(m); if ( - m.appear[0].p != m.vanish[1].p && //avoid promotions (already treated): + // Avoid promotions (already treated): + m.appear[0].p != m.vanish[1].p && (m.vanish[0].p != V.PAWN || m.end.x != lastRank) ) { // Add transformation into captured piece @@ -157,73 +160,13 @@ export const VariantRules = class CheckeredRules extends ChessRules { } getPotentialPawnMoves([x, y]) { - const color = this.turn; - let moves = []; - const [sizeX, sizeY] = [V.size.x, V.size.y]; - const shiftX = color == "w" ? -1 : 1; - const startRank = color == "w" ? sizeX - 2 : 1; - const lastRank = color == "w" ? 0 : sizeX - 1; - const pawnColor = this.getColor(x, y); //can be checkered - - const finalPieces = - x + shiftX == lastRank - ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN] - : [V.PAWN]; - if (this.board[x + shiftX][y] == V.EMPTY) { - // One square forward - for (let piece of finalPieces) { - moves.push( - this.getBasicMove([x, y], [x + shiftX, y], { - c: pawnColor, - p: piece - }) - ); - } - if ( - x == startRank && - this.board[x + 2 * shiftX][y] == V.EMPTY - ) { - // Two squares jump - moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y])); - } - } - // Captures - for (let shiftY of [-1, 1]) { - if ( - y + shiftY >= 0 && - y + shiftY < sizeY && - this.board[x + shiftX][y + shiftY] != V.EMPTY && - this.canTake([x, y], [x + shiftX, y + shiftY]) - ) { - for (let piece of finalPieces) { - moves.push( - this.getBasicMove([x, y], [x + shiftX, y + shiftY], { - c: pawnColor, - p: piece - }) - ); - } - } - } - - // En passant - const Lep = this.epSquares.length; - const epSquare = this.epSquares[Lep - 1]; //always at least one element - if ( - !!epSquare && - epSquare.x == x + shiftX && - Math.abs(epSquare.y - y) == 1 - ) { - let enpassantMove = this.getBasicMove([x, y], [epSquare.x, epSquare.y]); - enpassantMove.vanish.push({ - x: x, - y: epSquare.y, - p: "p", - c: this.getColor(x, epSquare.y) + let moves = super.getPotentialPawnMoves([x, y]); + // Post-process: set right color for checkered moves + if (this.getColor(x, y) == 'c') + moves.forEach(m => { + m.appear[0].c = 'c'; //may be done twice if capture + m.vanish[0].c = 'c'; }); - moves.push(enpassantMove); - } - return moves; } @@ -293,9 +236,22 @@ export const VariantRules = class CheckeredRules extends ChessRules { return false; } + // colors: array, generally 'w' and 'c' or 'b' and 'c' + isAttacked(sq, colors) { + if (!Array.isArray(colors)) colors = [colors]; + return ( + this.isAttackedByPawn(sq, colors) || + this.isAttackedByRook(sq, colors) || + this.isAttackedByKnight(sq, colors) || + this.isAttackedByBishop(sq, colors) || + this.isAttackedByQueen(sq, colors) || + this.isAttackedByKing(sq, colors) + ); + } + isAttackedByPawn([x, y], colors) { for (let c of colors) { - const color = c == "c" ? this.turn : c; + const color = (c == "c" ? this.turn : c); let pawnShift = color == "w" ? 1 : -1; if (x + pawnShift >= 0 && x + pawnShift < 8) { for (let i of [-1, 1]) { @@ -313,6 +269,62 @@ export const VariantRules = class CheckeredRules extends ChessRules { return false; } + isAttackedBySlideNJump([x, y], colors, piece, steps, oneStep) { + for (let step of steps) { + let rx = x + step[0], + ry = y + step[1]; + while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) { + rx += step[0]; + ry += step[1]; + } + if ( + V.OnBoard(rx, ry) && + this.getPiece(rx, ry) === piece && + colors.includes(this.getColor(rx, ry)) + ) { + return true; + } + } + return false; + } + + isAttackedByRook(sq, colors) { + return this.isAttackedBySlideNJump(sq, colors, V.ROOK, V.steps[V.ROOK]); + } + + isAttackedByKnight(sq, colors) { + return this.isAttackedBySlideNJump( + sq, + colors, + V.KNIGHT, + V.steps[V.KNIGHT], + "oneStep" + ); + } + + isAttackedByBishop(sq, colors) { + return this.isAttackedBySlideNJump(sq, colors, V.BISHOP, V.steps[V.BISHOP]); + } + + isAttackedByQueen(sq, colors) { + return this.isAttackedBySlideNJump( + sq, + colors, + V.QUEEN, + V.steps[V.ROOK].concat(V.steps[V.BISHOP]) + ); + } + + isAttackedByKing(sq, colors) { + return this.isAttackedBySlideNJump( + sq, + colors, + V.KING, + V.steps[V.ROOK].concat(V.steps[V.BISHOP]), + "oneStep" + ); + } + underCheck(color) { return this.isAttacked(this.kingPos[color], [V.GetOppCol(color), "c"]); }