X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=public%2Fjavascripts%2Fvariants%2FGrand.js;h=7248f578cc771952e030db9436675719dae1bd56;hp=f4e325dcbf12da055fec2b468e800d26d77e8a45;hb=375ecdd1387e729f85ed114e82253469e4849869;hpb=3c09dc498791ac478679bf2f42f441342c4fa22c diff --git a/public/javascripts/variants/Grand.js b/public/javascripts/variants/Grand.js index f4e325dc..7248f578 100644 --- a/public/javascripts/variants/Grand.js +++ b/public/javascripts/variants/Grand.js @@ -1,28 +1,117 @@ -//https://www.chessvariants.com/large.dir/freeling.html +// NOTE: initial setup differs from the original; see +// https://www.chessvariants.com/large.dir/freeling.html class GrandRules extends ChessRules { static getPpath(b) { - const V = VariantRules; return ([V.MARSHALL,V.CARDINAL].includes(b[1]) ? "Grand/" : "") + b; } - initVariables(fen) + static IsGoodFen(fen) { - super.initVariables(fen); - this.captures = { "w": {}, "b": {} }; //for promotions + if (!ChessRules.IsGoodFen(fen)) + return false; + const fenParsed = V.ParseFen(fen); + // 5) Check captures + if (!fenParsed.captured || !fenParsed.captured.match(/^[0-9]{10,10}$/)) + return false; + return true; } - static get size() { return [10,10]; } + static ParseFen(fen) + { + const fenParts = fen.split(" "); + return Object.assign( + ChessRules.ParseFen(fen), + { captured: fenParts[4] } + ); + } + + getFen() + { + return super.getFen() + " " + this.getCapturedFen(); + } + + getCapturedFen() + { + let counts = _.map(_.range(10), 0); + for (let i=0; i { + res += V.CoordsToSquare(sq) + ","; + }); + return res.slice(0,-1); //remove last comma + } + // En-passant after 2-sq or 3-sq jumps - getEpSquare(move) + getEpSquare(moveOrSquare) { + if (!moveOrSquare) + return undefined; + if (typeof moveOrSquare === "string") + { + const square = moveOrSquare; + if (square == "-") + return undefined; + let res = []; + square.split(",").forEach(sq => { + res.push(V.SquareToCoords(sq)); + }); + return res; + } + // Argument is a move: + const move = moveOrSquare; const [sx,sy,ex] = [move.start.x,move.start.y,move.end.x]; - if (this.getPiece(sx,sy) == VariantRules.PAWN && Math.abs(sx - ex) >= 2) + if (this.getPiece(sx,sy) == V.PAWN && Math.abs(sx - ex) >= 2) { const step = (ex-sx) / Math.abs(ex-sx); let res = [{ @@ -45,9 +134,9 @@ class GrandRules extends ChessRules { switch (this.getPiece(x,y)) { - case VariantRules.MARSHALL: + case V.MARSHALL: return this.getPotentialMarshallMoves([x,y]); - case VariantRules.CARDINAL: + case V.CARDINAL: return this.getPotentialCardinalMoves([x,y]); default: return super.getPotentialMovesFrom([x,y]) @@ -60,11 +149,10 @@ class GrandRules extends ChessRules { const color = this.turn; let moves = []; - const V = VariantRules; - const [sizeX,sizeY] = VariantRules.size; + const [sizeX,sizeY] = [V.size.x,V.size.y]; const shift = (color == "w" ? -1 : 1); - const startRanks = (color == "w" ? [sizeY-2,sizeY-3] : [1,2]); - const lastRanks = (color == "w" ? [0,1,2] : [sizeY-1,sizeY-2,sizeY-3]); + const startRanks = (color == "w" ? [sizeX-2,sizeX-3] : [1,2]); + const lastRanks = (color == "w" ? [0,1,2] : [sizeX-1,sizeX-2,sizeX-3]); if (x+shift >= 0 && x+shift < sizeX && x+shift != lastRanks[0]) { @@ -76,7 +164,7 @@ class GrandRules extends ChessRules { // Two squares jump moves.push(this.getBasicMove([x,y], [x+2*shift,y])); - if (x == startRanks[0] && this.board[x+2*shift][y] == V.EMPTY) + if (x == startRanks[0] && this.board[x+3*shift][y] == V.EMPTY) { // 3-squares jump moves.push(this.getBasicMove([x,y], [x+3*shift,y])); @@ -84,27 +172,39 @@ class GrandRules extends ChessRules } } // Captures - if (y>0 && this.canTake([x,y], [x+shift,y-1]) && this.board[x+shift][y-1] != V.EMPTY) + if (y>0 && this.canTake([x,y], [x+shift,y-1]) + && this.board[x+shift][y-1] != V.EMPTY) + { moves.push(this.getBasicMove([x,y], [x+shift,y-1])); - if (y { - if (!this.captures[color][p] || this.captures[color][p]==0) + if (this.captured[color][p]==0) return; // Normal move if (this.board[x+shift][y] == V.EMPTY) moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:p})); // Captures - if (y>0 && this.canTake([x,y], [x+shift,y-1]) && this.board[x+shift][y-1] != V.EMPTY) + if (y>0 && this.canTake([x,y], [x+shift,y-1]) + && this.board[x+shift][y-1] != V.EMPTY) + { moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:color,p:p})); - if (y