X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FBenedict.js;h=fbc4b488a1753a298f95f19d151eab1fb19c3a85;hb=32f6285ee325a14286562a53baefc647201df2af;hp=fbeef895522204565f4ab050f9bcf4bd12fff3c4;hpb=1af00c56319683edd9215457b2c84840215399ab;p=vchess.git diff --git a/client/src/variants/Benedict.js b/client/src/variants/Benedict.js index fbeef895..fbc4b488 100644 --- a/client/src/variants/Benedict.js +++ b/client/src/variants/Benedict.js @@ -1,10 +1,18 @@ import { ChessRules, PiPo, Move } from "@/base_rules"; -export const VariantRules = class BenedictRules extends ChessRules { +export class BenedictRules extends ChessRules { static get HasEnpassant() { return false; } + static get PawnSpecs() { + return Object.assign( + {}, + ChessRules.PawnSpecs, + { canCapture: false } + ); + } + // TODO(?): some duplicated code in 2 next functions getSlideNJumpMoves([x, y], steps, oneStep) { let moves = []; @@ -64,74 +72,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)); - } - // 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]) { @@ -140,33 +80,38 @@ export const VariantRules = class BenedictRules extends ChessRules { // Get all moves from x,y without captures: let moves = super.getPotentialMovesFrom([x, y]); // Add flips: - let newAppear = []; - let newVanish = []; moves.forEach(m => { + 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); - }); + } + } + Array.prototype.push.apply(m.appear, newAppear); + Array.prototype.push.apply(m.vanish, newVanish); V.UndoOnBoard(this.board, m); }); return moves; @@ -177,6 +122,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 []; @@ -209,4 +159,15 @@ export const VariantRules = class BenedictRules extends ChessRules { // 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); + } };