Simplify Monster + Doublemove2. Smoother example games
[vchess.git] / client / src / variants / Eightpieces.js
index 0f33518..8cf8747 100644 (file)
@@ -108,7 +108,7 @@ export class EightpiecesRules extends ChessRules {
     // 5) Check sentry push (if any)
     if (
       fenParsed.sentrypush != "-" &&
-      !fenParsed.sentrypush.match(/^([a-h][1-8],?)+$/)
+      !fenParsed.sentrypush.match(/^([a-h][1-8]){2,2}$/)
     ) {
       return false;
     }
@@ -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 = V.SquareToCoords(parsedFen.sentrypush.substr(0, 2)),
+            dest = V.SquareToCoords(parsedFen.sentrypush.substr(2));
+      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];
     }
   }
 
@@ -293,7 +310,8 @@ export class EightpiecesRules extends ChessRules {
 
   canIplay(side, [x, y]) {
     return (
-      (this.subTurn == 1 && this.turn == side && this.getColor(x, y) == side) ||
+      (this.subTurn == 1 && this.turn == side && this.getColor(x, y) == side)
+      ||
       (this.subTurn == 2 && x == this.sentryPos.x && y == this.sentryPos.y)
     );
   }
@@ -795,8 +813,8 @@ export class EightpiecesRules extends ChessRules {
       case V.QUEEN:
         return sliderAttack(V.steps[V.ROOK].concat(V.steps[V.BISHOP]));
       case V.LANCER: {
-        // Special case: as long as no enemy units stands in-between, it attacks
-        // (if it points toward the king).
+        // Special case: as long as no enemy units stands in-between,
+        // it attacks (if it points toward the king).
         const allowedStep = V.LANCER_DIRS[this.board[x1][y1].charAt(1)];
         return sliderAttack([allowedStep], "lancer");
       }
@@ -1043,7 +1061,8 @@ export class EightpiecesRules extends ChessRules {
       Object.keys(V.LANCER_DIRNAMES).includes(move.appear[0].p)
     ) {
       // Fix promotions in lancer:
-      notation = notation.slice(0, -1) + "L:" + V.LANCER_DIRNAMES[move.appear[0].p];
+      notation = notation.slice(0, -1) +
+        "L:" + V.LANCER_DIRNAMES[move.appear[0].p];
     }
     return notation;
   }