From: Benjamin Auder Date: Fri, 27 Mar 2020 23:41:32 +0000 (+0100) Subject: Improve FEN length for Eightpieces variant X-Git-Url: https://git.auder.net/%7B%7B%20asset%28%27mixstore/css/user/doc/pieces/common.css?a=commitdiff_plain;h=0a9cef131612079e55714f56f1466b4e2bd749af;p=vchess.git Improve FEN length for Eightpieces variant --- diff --git a/client/src/variants/Eightpieces.js b/client/src/variants/Eightpieces.js index 4f487647..a05ba1e2 100644 --- a/client/src/variants/Eightpieces.js +++ b/client/src/variants/Eightpieces.js @@ -127,9 +127,11 @@ export class EightpiecesRules extends ChessRules { const L = this.sentryPush.length; if (!this.sentryPush[L-1]) return "-"; let res = ""; - this.sentryPush[L-1].forEach(coords => - res += V.CoordsToSquare(coords) + ","); - return res.slice(0, -1); + const spL = this.sentryPush[L-1].length; + // Condensate path: just need initial and final squares: + return [0, spL - 1] + .map(i => V.CoordsToSquare(this.sentryPush[L-1][i])) + .join(","); } setOtherVariables(fen) { @@ -142,11 +144,26 @@ export class EightpiecesRules extends ChessRules { const parsedFen = V.ParseFen(fen); if (parsedFen.sentrypush == "-") this.sentryPush = [null]; else { - this.sentryPush = [ - parsedFen.sentrypush.split(",").map(sq => { - return V.SquareToCoords(sq); - }) - ]; + // Expand init + dest squares into a full path: + const [init, dest] = + parsedFen.sentrypush.split(",").map(sq => V.SquareToCoords(sq)); + let newPath = [init]; + const delta = ['x', 'y'].map(i => Math.abs(dest[i] - init[i])); + // Check that it's not a knight movement: + if (delta[0] == 0 || delta[1] == 0 || delta[0] == delta[1]) { + const step = ['x', 'y'].map((i, idx) => { + return (dest[i] - init[i]) / delta[idx] || 0 + }); + let x = init.x + step[0], + y = init.y + step[1]; + while (x != dest.x || y != dest.y) { + newPath.push({ x: x, y: y }); + x += step[0]; + y += step[1]; + } + } + newPath.push(dest); + this.sentryPush = [newPath]; } }