X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FBenedict.js;h=5584fb870aa6f2239248d580fa0f2e45945ca1f3;hb=4313762da3237b04f204e121a20cab3ba7bb5dd2;hp=e87827f48e83a86b0bc4032140ef2ef2fd5ab354;hpb=e9b736ee3e72e2ddb9bf4da0c0f1e22a70f7448f;p=vchess.git diff --git a/client/src/variants/Benedict.js b/client/src/variants/Benedict.js index e87827f4..5584fb87 100644 --- a/client/src/variants/Benedict.js +++ b/client/src/variants/Benedict.js @@ -1,26 +1,21 @@ import { ChessRules, PiPo, Move } from "@/base_rules"; -export const VariantRules = class BenedictRules extends ChessRules { +export class BenedictRules extends ChessRules { + static get HasEnpassant() { return false; } - // TODO(?): some duplicated code in 2 next functions - getSlideNJumpMoves([x, y], steps, oneStep) { - let moves = []; - outerLoop: for (let loop = 0; loop < steps.length; loop++) { - const step = steps[loop]; - let i = x + step[0]; - let j = y + step[1]; - while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) { - moves.push(this.getBasicMove([x, y], [i, j])); - if (oneStep) continue outerLoop; - i += step[0]; - j += step[1]; - } - // No capture check: handled elsewhere (next method) - } - return moves; + static get PawnSpecs() { + return Object.assign( + {}, + ChessRules.PawnSpecs, + { canCapture: false } + ); + } + + canTake() { + return false; } // Find possible captures from a square @@ -64,155 +59,6 @@ export const VariantRules = class BenedictRules extends ChessRules { return squares; } - getPotentialPawnMoves([x, y]) { - const color = this.getColor(x, y); - let moves = []; - const sizeY = V.size.y; - const shift = color == "w" ? -1 : 1; - const startRank = color == "w" ? sizeY - 2 : 1; - const firstRank = color == "w" ? sizeY - 1 : 0; - const lastRank = color == "w" ? 0 : sizeY - 1; - - if (x + shift != lastRank) { - // Normal moves - if (this.board[x + shift][y] == V.EMPTY) { - moves.push(this.getBasicMove([x, y], [x + shift, y])); - if ( - [startRank, firstRank].includes(x) && - this.board[x + 2 * shift][y] == V.EMPTY - ) { - // Two squares jump - moves.push(this.getBasicMove([x, y], [x + 2 * shift, y])); - } - } - } - else { - // Promotion - let promotionPieces = [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN]; - promotionPieces.forEach(p => { - // Normal move - if (this.board[x + shift][y] == V.EMPTY) - moves.push( - this.getBasicMove([x, y], [x + shift, y], { c: color, p: p }) - ); - }); - } - - // No en passant here - - return moves; - } - - getPotentialRookMoves(sq) { - return this.getSlideNJumpMoves(sq, V.steps[V.ROOK]); - } - - getPotentialKnightMoves(sq) { - return this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep"); - } - - getPotentialBishopMoves(sq) { - return this.getSlideNJumpMoves(sq, V.steps[V.BISHOP]); - } - - getPotentialQueenMoves(sq) { - return this.getSlideNJumpMoves( - sq, - V.steps[V.ROOK].concat(V.steps[V.BISHOP]) - ); - } - - getPotentialKingMoves(sq) { - // Initialize with normal (non-capturing) moves - let noCaptures = this.getSlideNJumpMoves( - sq, - V.steps[V.ROOK].concat(V.steps[V.BISHOP]), - "oneStep" - ); - return noCaptures.concat(this.getCastleMoves(sq)); - } - - // No "under check" verifications: - getCastleMoves([x, y]) { - 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) - - // Castling ? - const oppCol = V.GetOppCol(c); - let moves = []; - let i = 0; - // King, then rook: - const finalSquares = [ - [2, 3], - [V.size.y - 2, V.size.y - 3] - ]; - castlingCheck: for ( - let castleSide = 0; - castleSide < 2; - castleSide++ //large, then small - ) { - if (!this.castleFlags[c][castleSide]) continue; - // If this code is reached, rooks and king are on initial position - - const rookPos = this.INIT_COL_ROOK[c][castleSide]; - if (this.getColor(x, rookPos) != c) - // Rook is here but changed color - continue; - - // Nothing on the path of the king ? - const finDist = finalSquares[castleSide][0] - y; - let step = finDist / Math.max(1, Math.abs(finDist)); - for (let i = y; i != finalSquares[castleSide][0]; i += step) { - if ( - 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))) - ) { - continue castlingCheck; - } - } - - // Nothing on the path to the rook? - step = castleSide == 0 ? -1 : 1; - for (i = y + step; i != this.INIT_COL_ROOK[c][castleSide]; i += step) { - if (this.board[x][i] != V.EMPTY) continue castlingCheck; - } - - // Nothing on final squares, except maybe king and castling rook? - for (i = 0; i < 2; i++) { - if ( - this.board[x][finalSquares[castleSide][i]] != V.EMPTY && - this.getPiece(x, finalSquares[castleSide][i]) != V.KING && - finalSquares[castleSide][i] != rookPos - ) { - continue castlingCheck; - } - } - - // If this code is reached, castle is valid - moves.push( - 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 }) - ], - vanish: [ - new PiPo({ x: x, y: y, p: V.KING, c: c }), - new PiPo({ x: x, y: rookPos, p: V.ROOK, c: c }) - ], - end: - Math.abs(y - rookPos) <= 2 - ? { x: x, y: rookPos } - : { x: x, y: y + 2 * (castleSide == 0 ? -1 : 1) } - }) - ); - } - - return moves; - } - // TODO: appear/vanish description of a move is too verbose for Benedict. // => Would need a new "flipped" array, to be passed in Game.vue... getPotentialMovesFrom([x, y]) { @@ -225,27 +71,32 @@ export const VariantRules = class BenedictRules extends ChessRules { let newAppear = []; let newVanish = []; V.PlayOnBoard(this.board, m); - // If castling, m.appear has 2 elements: - m.appear.forEach(a => { - const flipped = this.findCaptures([a.x, a.y]); - flipped.forEach(sq => { - const piece = this.getPiece(sq[0],sq[1]); - const pipoA = new PiPo({ - x:sq[0], - y:sq[1], - c:color, - p:piece + // If castling, m.appear has 2 elements. + // In this case, consider the attacks of moving units only. + // (Sometimes the king or rook doesn't move). + for (let i = 0; i < m.appear.length; i++) { + const a = m.appear[i]; + if (m.vanish[i].x != a.x || m.vanish[i].y != a.y) { + const flipped = this.findCaptures([a.x, a.y]); + flipped.forEach(sq => { + const piece = this.getPiece(sq[0],sq[1]); + const pipoA = new PiPo({ + x:sq[0], + y:sq[1], + c:color, + p:piece + }); + const pipoV = new PiPo({ + x:sq[0], + y:sq[1], + c:oppCol, + p:piece + }); + newAppear.push(pipoA); + newVanish.push(pipoV); }); - const pipoV = new PiPo({ - x:sq[0], - y:sq[1], - c:oppCol, - p:piece - }); - newAppear.push(pipoA); - newVanish.push(pipoV); - }); - }); + } + } Array.prototype.push.apply(m.appear, newAppear); Array.prototype.push.apply(m.vanish, newVanish); V.UndoOnBoard(this.board, m); @@ -258,6 +109,11 @@ export const VariantRules = class BenedictRules extends ChessRules { return moves; } + // Since it's used just for the king, and there are no captures: + isAttacked(sq, color) { + return false; + } + // No notion of check here: getCheckSquares() { return []; @@ -266,13 +122,10 @@ export const VariantRules = class BenedictRules extends ChessRules { // Stop at the first move found atLeastOneMove() { const color = this.turn; - const oppCol = V.GetOppCol(color); for (let i = 0; i < V.size.x; i++) { for (let j = 0; j < V.size.y; j++) { - if (this.board[i][j] != V.EMPTY && this.getColor(i, j) != oppCol) { - const moves = this.getPotentialMovesFrom([i, j]); - if (moves.length > 0) - return true; + if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color) { + if (this.getPotentialMovesFrom([i, j]).length > 0) return true; } } } @@ -285,9 +138,20 @@ export const VariantRules = class BenedictRules extends ChessRules { const kp = this.kingPos[color]; if (this.getColor(kp[0], kp[1]) != color) return color == "w" ? "0-1" : "1-0"; - if (this.atLeastOneMove()) - return "*"; + if (this.atLeastOneMove()) return "*"; // Stalemate: return "1/2"; } + + getNotation(move) { + // Just remove flips: + const basicMove = { + appear: [move.appear[0]], + vanish: [move.vanish[0]], + start: move.start, + end: move.end + }; + return super.getNotation(basicMove); + } + };