X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fvariants%2FZen.js;h=0424e07f089f22c5cd8a0c00a4df8e64e4dd7c0c;hb=ffeaef854d80445ef87f2cdf4fe71d60347a81bf;hp=256b80dcbd701e87c419abdaec9d8adf048ce414;hpb=4f298adbee00942323fc7ec517117552aeb5a08a;p=vchess.git diff --git a/client/src/variants/Zen.js b/client/src/variants/Zen.js index 256b80dc..0424e07f 100644 --- a/client/src/variants/Zen.js +++ b/client/src/variants/Zen.js @@ -1,26 +1,41 @@ 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 - getSlideNJumpMoves([x,y], steps, oneStep) - { - const color = this.getColor(x,y); + getSlideNJumpMoves([x, y], steps, oneStep) { let moves = []; - outerLoop: - for (let loop=0; loop { - moves.push(this.getBasicMove([x,y], [i,j], {c:color,p:p})); + moves.push(this.getBasicMove([x, y], [i, j], { c: color, p: p })); }); - } - else - { + } else { // All other cases - moves.push(this.getBasicMove([x,y], [i,j])); + moves.push(this.getBasicMove([x, y], [i, j])); } } } @@ -76,8 +96,7 @@ export const VariantRules = class ZenRules extends ChessRules } // Find possible captures from a square: look in every direction! - findCaptures(sq) - { + findCaptures(sq) { let moves = []; Array.prototype.push.apply(moves, this.findCaptures_aux(sq, V.PAWN)); @@ -89,99 +108,64 @@ export const VariantRules = class ZenRules extends ChessRules return moves; } - getPotentialPawnMoves([x,y]) - { - const color = this.getColor(x,y); - let moves = []; - const [sizeX,sizeY] = [V.size.x,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 + canTake(sq1, sq2) { + return false; //captures handled separately + } + getPotentialPawnMoves([x, y]) { + let moves = super.getPotentialPawnMoves([x, y]); // Add "zen" captures - Array.prototype.push.apply(moves, this.findCaptures([x,y])); - + Array.prototype.push.apply(moves, this.findCaptures([x, y])); return moves; } - getPotentialRookMoves(sq) - { + getPotentialRookMoves(sq) { let noCaptures = this.getSlideNJumpMoves(sq, V.steps[V.ROOK]); let captures = this.findCaptures(sq); return noCaptures.concat(captures); } - getPotentialKnightMoves(sq) - { + getPotentialKnightMoves(sq) { let noCaptures = this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT], "oneStep"); let captures = this.findCaptures(sq); return noCaptures.concat(captures); } - getPotentialBishopMoves(sq) - { + getPotentialBishopMoves(sq) { let noCaptures = this.getSlideNJumpMoves(sq, V.steps[V.BISHOP]); let captures = this.findCaptures(sq); return noCaptures.concat(captures); } - getPotentialQueenMoves(sq) - { + getPotentialQueenMoves(sq) { let noCaptures = this.getSlideNJumpMoves( - sq, V.steps[V.ROOK].concat(V.steps[V.BISHOP])); + sq, + V.steps[V.ROOK].concat(V.steps[V.BISHOP]) + ); let captures = this.findCaptures(sq); return noCaptures.concat(captures); } - getPotentialKingMoves(sq) - { + getPotentialKingMoves(sq) { // Initialize with normal moves - let noCaptures = this.getSlideNJumpMoves(sq, - V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep"); + 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)); } - getNotation(move) - { + getNotation(move) { // Recognize special moves first - if (move.appear.length == 2) - { + if (move.appear.length == 2) { // castle - if (move.end.y < move.start.y) - return "0-0-0"; - else - return "0-0"; + if (move.end.y < move.start.y) return "0-0-0"; + 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 @@ -189,40 +173,33 @@ export const VariantRules = class ZenRules extends ChessRules let notation = ""; const piece = this.getPiece(move.start.x, move.start.y); - if (piece == V.PAWN) - { + if (piece == V.PAWN) { // pawn move (TODO: enPassant indication) - if (move.vanish.length > 1) - { + if (move.vanish.length > 1) { // capture notation = initialSquare + "x" + finalSquare; - } - else //no capture - notation = finalSquare; - if (piece != move.appear[0].p) //promotion + } //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"; + if (move.vanish.length > 1) notation += initialSquare + "x"; notation += finalSquare; } return notation; } - static get VALUES() - { + static get VALUES() { return { - 'p': 1, - 'r': 3, - 'n': 2, - 'b': 2, - 'q': 5, - 'k': 1000 - } + p: 1, + r: 3, + n: 2, + b: 2, + q: 5, + k: 1000 + }; } -} +};