X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fvariants%2FZen.js;h=c1f48147405893bf6e6af6fb2fe5260f4107c052;hb=9234226104764b91df9d677fb360ad538b98510c;hp=54c77ad2889288c17be3ed264bdfc0ed243f9df0;hpb=1d184b4c016a645228251ce984d4c980e60420b0;p=vchess.git diff --git a/public/javascripts/variants/Zen.js b/public/javascripts/variants/Zen.js index 54c77ad2..c1f48147 100644 --- a/public/javascripts/variants/Zen.js +++ b/public/javascripts/variants/Zen.js @@ -1,21 +1,28 @@ class ZenRules extends ChessRules { - // TODO: some duplicated code in 2 next functions - getSlideNJumpMoves(x, y, color, steps, oneStep) + // NOTE: enPassant, if enabled, would need to redefine carefully getEpSquare + getEpSquare(move) { - var moves = []; - let [sizeX,sizeY] = VariantRules.size; + return undefined; + } + + // TODO(?): some duplicated code in 2 next functions + getSlideNJumpMoves([x,y], steps, oneStep) + { + const color = this.getColor(x,y); + let moves = []; + const [sizeX,sizeY] = VariantRules.size; outerLoop: - for (var loop=0; loop=0 && i=0 && j=0 && i=0 && j=0 && i=0 && j { - moves.push(this.getBasicMove(x, y, i, j, p)); + moves.push(this.getBasicMove([x,y], [i,j], {c:color,p:p})); }); } else { // All other cases - moves.push(this.getBasicMove(x, y, i, j)); + moves.push(this.getBasicMove([x,y], [i,j])); } } } @@ -74,40 +81,28 @@ class ZenRules extends ChessRules } // Find possible captures from a square: look in every direction! - findCaptures(x, y, color) + findCaptures(sq) { - var moves = []; - - // PAWN - Array.prototype.push.apply(moves, - this.findCaptures_aux(x, y, color, VariantRules.PAWN)); - - // ROOK - Array.prototype.push.apply(moves, - this.findCaptures_aux(x, y, color, VariantRules.ROOK)); + let moves = []; - // KNIGHT - Array.prototype.push.apply(moves, - this.findCaptures_aux(x, y, color, VariantRules.KNIGHT)); - - // BISHOP - Array.prototype.push.apply(moves, - this.findCaptures_aux(x, y, color, VariantRules.BISHOP)); - - // QUEEN - Array.prototype.push.apply(moves, - this.findCaptures_aux(x, y, color, VariantRules.QUEEN)); + Array.prototype.push.apply(moves, this.findCaptures_aux(sq, VariantRules.PAWN)); + Array.prototype.push.apply(moves, this.findCaptures_aux(sq, VariantRules.ROOK)); + Array.prototype.push.apply(moves, this.findCaptures_aux(sq, VariantRules.KNIGHT)); + Array.prototype.push.apply(moves, this.findCaptures_aux(sq, VariantRules.BISHOP)); + Array.prototype.push.apply(moves, this.findCaptures_aux(sq, VariantRules.QUEEN)); return moves; } - getPotentialPawnMoves(x, y, color) + getPotentialPawnMoves([x,y]) { + const color = this.getColor(x,y); var moves = []; var V = VariantRules; let [sizeX,sizeY] = VariantRules.size; let shift = (color == 'w' ? -1 : 1); let startRank = (color == 'w' ? sizeY-2 : 1); + let firstRank = (color == 'w' ? sizeY-1 : 0); let lastRank = (color == "w" ? 0 : sizeY-1); if (x+shift >= 0 && x+shift < sizeX && x+shift != lastRank) @@ -115,11 +110,11 @@ class ZenRules extends ChessRules // Normal moves if (this.board[x+shift][y] == V.EMPTY) { - moves.push(this.getBasicMove(x, y, x+shift, y)); - if (x==startRank && this.board[x+2*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)); + moves.push(this.getBasicMove([x,y], [x+2*shift,y])); } } } @@ -131,76 +126,59 @@ class ZenRules extends ChessRules promotionPieces.forEach(p => { // Normal move if (this.board[x+shift][y] == V.EMPTY) - moves.push(this.getBasicMove(x, y, x+shift, y, p)); + moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:p})); }); } - // En passant - const Lep = this.epSquares.length; - const epSquare = Lep>0 ? this.epSquares[Lep-1] : undefined; - if (!!epSquare && epSquare.x == x+shift && Math.abs(epSquare.y - y) == 1) - { - let epStep = epSquare.y - y; - var enpassantMove = this.getBasicMove(x, y, x+shift, y+epStep); - enpassantMove.vanish.push({ - x: x, - y: y+epStep, - p: 'p', - c: this.getColor(x,y+epStep) - }); - moves.push(enpassantMove); - } + // No en passant here // Add "zen" captures - Array.prototype.push.apply(moves, this.findCaptures(x, y, color)); + Array.prototype.push.apply(moves, this.findCaptures([x,y])); return moves; } - getPotentialRookMoves(x, y, color) + getPotentialRookMoves(sq) { let noCaptures = this.getSlideNJumpMoves( - x, y, color, VariantRules.steps[VariantRules.ROOK]); - let captures = this.findCaptures(x, y, color); + sq, VariantRules.steps[VariantRules.ROOK]); + let captures = this.findCaptures(sq); return noCaptures.concat(captures); } - getPotentialKnightMoves(x, y, color) + getPotentialKnightMoves(sq) { let noCaptures = this.getSlideNJumpMoves( - x, y, color, VariantRules.steps[VariantRules.KNIGHT], "oneStep"); - let captures = this.findCaptures(x, y, color); + sq, VariantRules.steps[VariantRules.KNIGHT], "oneStep"); + let captures = this.findCaptures(sq); return noCaptures.concat(captures); } - getPotentialBishopMoves(x, y, color) + getPotentialBishopMoves(sq) { let noCaptures = this.getSlideNJumpMoves( - x, y, color, VariantRules.steps[VariantRules.BISHOP]); - let captures = this.findCaptures(x, y, color); + sq, VariantRules.steps[VariantRules.BISHOP]); + let captures = this.findCaptures(sq); return noCaptures.concat(captures); } - getPotentialQueenMoves(x, y, color) + getPotentialQueenMoves(sq) { + const V = VariantRules; let noCaptures = this.getSlideNJumpMoves( - x, y, color, VariantRules.steps[VariantRules.QUEEN]); - let captures = this.findCaptures(x, y, color); + sq, V.steps[V.ROOK].concat(V.steps[V.BISHOP])); + let captures = this.findCaptures(sq); return noCaptures.concat(captures); } - getPotentialKingMoves(x, y, c) + getPotentialKingMoves(sq) { + const V = VariantRules; // Initialize with normal moves - let noCaptures = this.getSlideNJumpMoves( - x, y, c, VariantRules.steps[VariantRules.QUEEN], "oneStep"); - let captures = this.findCaptures(x, y, c); - - let moves = noCaptures - .concat(captures) - .concat(this.getCastleMoves(x, y, c)); - - return 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)); } getNotation(move) @@ -224,7 +202,7 @@ class ZenRules extends ChessRules String.fromCharCode(97 + move.end.y) + (VariantRules.size[0]-move.end.x); let notation = ""; - let piece = this.rules.getPiece(move.start.x, move.start.y); + let piece = this.getPiece(move.start.x, move.start.y); if (piece == VariantRules.PAWN) { // pawn move (TODO: enPassant indication)