Some fixes and enhancements as suggested on Discord
[vchess.git] / client / src / variants / Eightpieces.js
index 06ece2a..8fe8935 100644 (file)
@@ -17,83 +17,100 @@ export const VariantRules = class EightpiecesRules extends ChessRules {
     return ChessRules.PIECES.concat([V.JAILER, V.SENTRY, V.LANCER]);
   }
 
+  static get LANCER_DIRS() {
+    return {
+      'c': [-1, 0], //north
+      'd': [-1, 1], //N-E
+      'e': [0, 1], //east
+      'f': [1, 1], //S-E
+      'g': [1, 0], //south
+      'h': [1, -1], //S-W
+      'm': [0, -1], //west
+      'o': [-1, -1] //N-W
+    };
+  }
+
+  getPiece(i, j) {
+    const piece = this.board[i][j].charAt(1);
+    // Special lancer case: 8 possible orientations
+    if (Object.keys(V.LANCER_DIRS).includes(piece)) return V.LANCER;
+    return piece;
+  }
+
   getPpath(b) {
-    // TODO: more subtle, path depends on the orientations
-    // lancerOrientations should probably be a 8x8 array, for speed.
     return (
       ([V.JAILER, V.SENTRY, V.LANCER].includes(b[1])
         ? "Eightpieces/" : "") + b
     );
   }
 
-  static IsGoodFen(fen) {
-    if (!ChessRules.IsGoodFen(fen)) return false;
-    const fenParsed = V.ParseFen(fen);
-    // 5) Check lancers orientations (if there are any left)
-    if (
-      !fenParsed.lancers ||
-      (
-        fenParsed.lancers != "-" &&
-        !fenParsed.lancers.match(/^([a-h][1-8][0-7],?)+$/)
-      )
-    ) {
-      return false;
-    }
-    return true;
+  setOtherVariables(fen) {
+    super.setOtherVariables(fen);
+    // subTurn == 2 only when a sentry moved, and is about to push something
+    this.subTurn = 1;
+    // Stack pieces' forbidden squares after a sentry move at each turn
+    this.sentryPath = [];
   }
 
-  static ParseFen(fen) {
-    const fenParts = fen.split(" ");
-    return Object.assign(ChessRules.ParseFen(fen), {
-      lancers: fenParts[5],
-    });
+  canTake([x1,y1], [x2, y2]) {
+    if (this.subTurn == 2)
+      // Sentry push: pieces can capture own color (only)
+      return this.getColor(x1, y1) == this.getColor(x2, y2);
+    return super.canTake([x1,y1], [x2, y2]);
   }
 
   static GenRandInitFen(randomness) {
     // TODO: special conditions
   }
 
-  getFen() {
-    return (
-      super.getFen() + " " + this.getLancersFen()
-    );
+  // TODO: rook + jailer
+  scanKingsRooks(fen) {
+    this.kingPos = { w: [-1, -1], b: [-1, -1] };
+    const fenRows = V.ParseFen(fen).position.split("/");
+    for (let i = 0; i < fenRows.length; i++) {
+      let k = 0; //column index on board
+      for (let j = 0; j < fenRows[i].length; j++) {
+        switch (fenRows[i].charAt(j)) {
+          case "k":
+          case "l":
+            this.kingPos["b"] = [i, k];
+            break;
+          case "K":
+          case "L":
+            this.kingPos["w"] = [i, k];
+            break;
+          default: {
+            const num = parseInt(fenRows[i].charAt(j));
+            if (!isNaN(num)) k += num - 1;
+          }
+        }
+        k++;
+      }
+    }
   }
 
-  getFenForRepeat() {
-    return (
-      this.getBaseFen() + "_" +
-      this.getTurnFen() + "_" +
-      this.getFlagsFen() + "_" +
-      this.getEnpassantFen() + "_" +
-      this.getLancersFen()
-    );
+  getPotentialMovesFrom([x,y]) {
+    // if subTurn == 2, allow only 
   }
 
-  getLancersFen() {
-    let res = "";
-    this.lancerOrientations.forEach(o => {
-      res += V.CoordsToSquare(o.sq) + o.dir + ",";
-    });
-    res = res.slice(0, -1);
-    return res || "-";
+  // getPotentialMoves, isAttacked: TODO
+  getPotentialCastleMoves(sq) { //TODO: adapt, with jailer
   }
 
-  setOtherVariables(fen) {
-    super.setOtherVariables(fen);
-    const fenParsed = V.ParseFen(fen);
-    // Also init lancer orientations (from FEN):
-    this.lancerOrientations = 32; // TODO
+  updateVariables(move) {
+    // TODO: stack sentryPath if subTurn == 2 --> all squares between move.start et move.end, sauf si c'est un pion
   }
 
-  // getPotentialMoves, isAttacked: TODO
-
-  // updatedVariables: update lancers' orientations
+  // TODO: special pass move: take jailer with king
 
-  // subTurn : if sentry moved to some enemy piece.
+  // subTurn : if sentry moved to some enemy piece --> enregistrer déplacement sentry, subTurn == 2, puis déplacer pièce adverse --> 1st 1/2 of turn, vanish sentry tout simplement.
+  // --> le turn ne change pas !
+  // 2nd half: move only 
+  // FEN flag: sentryPath from init pushing to final enemy square --> forbid some moves (getPotentialMoves)
 
   static get VALUES() {
     return Object.assign(
-      { l: 5, s: 4, j: 5 }, //experimental
+      { l: 4.8, s: 2.8, j: 3.8 }, //Jeff K. estimations
       ChessRules.VALUES
     );
   }