X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FZen.js;h=808d5436bed14f1db741523a169741bf2e3718dc;hb=8ca6042e7b8cffd4131e81493141ab6261300ff6;hp=f5bff8ffd40c8a63d217c80827c1c0db3df9e87b;hpb=6808d7a16ec1e761c6a2dffec2281c96953e4d89;p=vchess.git diff --git a/client/src/variants/Zen.js b/client/src/variants/Zen.js index f5bff8ff..808d5436 100644 --- a/client/src/variants/Zen.js +++ b/client/src/variants/Zen.js @@ -1,9 +1,29 @@ import { ChessRules } from "@/base_rules"; -export const VariantRules = class ZenRules extends ChessRules { - // NOTE: enPassant, if enabled, would need to redefine carefully getEpSquare - static get HasEnpassant() { - return false; +export class ZenRules extends ChessRules { + getEpSquare(moveOrSquare) { + if (!moveOrSquare) return undefined; + if (typeof moveOrSquare === "string") { + const square = moveOrSquare; + if (square == "-") return undefined; + return V.SquareToCoords(square); + } + const move = moveOrSquare; + const s = move.start, + e = move.end; + if ( + // Exclude captures (of rooks for example) + move.vanish.length == 1 && + s.y == e.y && + Math.abs(s.x - e.x) == 2 && + move.appear[0].p == V.PAWN + ) { + return { + x: (s.x + e.x) / 2, + y: s.y + }; + } + return undefined; } // TODO(?): some duplicated code in 2 next functions @@ -28,7 +48,7 @@ export const VariantRules = class ZenRules extends ChessRules { // if met piece is opponent and same movement (asA): eat it! findCaptures_aux([x, y], asA) { const color = this.getColor(x, y); - var moves = []; + let moves = []; const steps = asA != V.PAWN ? asA == V.QUEEN @@ -43,9 +63,10 @@ export const VariantRules = class ZenRules extends ChessRules { [1, -1], [1, 1] ]; - const oneStep = asA == V.KNIGHT || asA == V.PAWN; //we don't capture king + const oneStep = [V.KNIGHT,V.PAWN].includes(asA); //we don't capture king const lastRank = color == "w" ? 0 : V.size.x - 1; const promotionPieces = [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN]; + const oppCol = V.GetOppCol(color); outerLoop: for (let loop = 0; loop < steps.length; loop++) { const step = steps[loop]; let i = x + step[0]; @@ -57,7 +78,7 @@ export const VariantRules = class ZenRules extends ChessRules { } if ( V.OnBoard(i, j) && - this.getColor(i, j) == V.GetOppCol(color) && + this.getColor(i, j) == oppCol && this.getPiece(i, j) == asA ) { // eat! @@ -78,93 +99,20 @@ export const VariantRules = class ZenRules extends ChessRules { // Find possible captures from a square: look in every direction! findCaptures(sq) { let moves = []; - Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.PAWN)); Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.ROOK)); Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.KNIGHT)); Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.BISHOP)); Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.QUEEN)); - return moves; } - 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])); - } - } - } //promotion - else { - 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 - - // Add "zen" captures - Array.prototype.push.apply(moves, this.findCaptures([x, y])); - - return moves; - } - - getPotentialRookMoves(sq) { - let noCaptures = this.getSlideNJumpMoves(sq, V.steps[V.ROOK]); - let captures = this.findCaptures(sq); - return noCaptures.concat(captures); + canTake(sq1, sq2) { + return false; //captures handled separately } - getPotentialKnightMoves(sq) { - let noCaptures = this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep"); - let captures = this.findCaptures(sq); - return noCaptures.concat(captures); - } - - getPotentialBishopMoves(sq) { - let noCaptures = this.getSlideNJumpMoves(sq, V.steps[V.BISHOP]); - let captures = this.findCaptures(sq); - return noCaptures.concat(captures); - } - - getPotentialQueenMoves(sq) { - let noCaptures = this.getSlideNJumpMoves( - sq, - V.steps[V.ROOK].concat(V.steps[V.BISHOP]) - ); - let captures = this.findCaptures(sq); - return noCaptures.concat(captures); - } - - getPotentialKingMoves(sq) { - // Initialize with normal moves - let noCaptures = this.getSlideNJumpMoves( - sq, - V.steps[V.ROOK].concat(V.steps[V.BISHOP]), - "oneStep" - ); - let captures = this.findCaptures(sq); - return noCaptures.concat(captures).concat(this.getCastleMoves(sq)); + getPotentialMovesFrom(sq) { + return super.getPotentialMovesFrom(sq).concat(this.findCaptures(sq)); } getNotation(move) { @@ -175,7 +123,7 @@ export const VariantRules = class ZenRules extends ChessRules { return "0-0"; } - // Translate initial square (because pieces may fly unusually in this variant!) + // Translate initial square (because pieces may fly unusually!) const initialSquare = V.CoordsToSquare(move.start); // Translate final square @@ -185,15 +133,16 @@ export const VariantRules = class ZenRules extends ChessRules { const piece = this.getPiece(move.start.x, move.start.y); if (piece == V.PAWN) { // pawn move (TODO: enPassant indication) - if (move.vanish.length > 1) { + if (move.vanish.length == 2) { // capture notation = initialSquare + "x" + finalSquare; - } //no capture + } else notation = finalSquare; if (piece != move.appear[0].p) //promotion notation += "=" + move.appear[0].p.toUpperCase(); - } else { + } + else { // Piece movement notation = piece.toUpperCase(); if (move.vanish.length > 1) notation += initialSquare + "x";