Add Checkered1 + fix last move highlights
[vchess.git] / client / src / variants / Marseille.js
index 121bf01..90084dc 100644 (file)
@@ -1,7 +1,7 @@
 import { ChessRules } from "@/base_rules";
 import { randInt } from "@/utils/alea";
 
-export const VariantRules = class MarseilleRules extends ChessRules {
+export class MarseilleRules extends ChessRules {
   static IsGoodEnpassant(enpassant) {
     const squares = enpassant.split(",");
     if (squares.length > 2) return false;
@@ -14,10 +14,6 @@ export const VariantRules = class MarseilleRules extends ChessRules {
     return true;
   }
 
-  getTurnFen() {
-    return this.turn + this.subTurn;
-  }
-
   // There may be 2 enPassant squares (if 2 pawns jump 2 squares in same turn)
   getEnpassantFen() {
     return this.epSquares[this.epSquares.length - 1].map(
@@ -34,61 +30,15 @@ export const VariantRules = class MarseilleRules extends ChessRules {
       if (sq != "-") return V.SquareToCoords(sq);
       return undefined;
     })];
-    this.scanKingsRooks(fen);
+    this.scanKings(fen);
     // Extract subTurn from turn indicator: "w" (first move), or
     // "w1" or "w2" white subturn 1 or 2, and same for black
-    const fullTurn = V.ParseFen(fen).turn;
-    this.turn = fullTurn[0];
-    // At move 1, the subTurn doesn't need to be specified:
-    this.subTurn = fullTurn[1] || 1;
+    this.turn = parsedFen.turn;
+    this.subTurn = 1;
   }
 
-  getPotentialPawnMoves([x, y]) {
-    const color = this.turn;
+  getEnpassantCaptures([x, y], shiftX) {
     let moves = [];
-    const [sizeX, sizeY] = [V.size.x, V.size.y];
-    const shiftX = color == "w" ? -1 : 1;
-    const firstRank = color == "w" ? sizeX - 1 : 0;
-    const startRank = color == "w" ? sizeX - 2 : 1;
-    const lastRank = color == "w" ? 0 : sizeX - 1;
-    const finalPieces =
-      x + shiftX == lastRank ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN] : [V.PAWN];
-
-    // One square forward
-    if (this.board[x + shiftX][y] == V.EMPTY) {
-      for (let piece of finalPieces) {
-        moves.push(
-          this.getBasicMove([x, y], [x + shiftX, y], { c: color, p: piece })
-        );
-      }
-      // Next condition because pawns on 1st rank can generally jump
-      if (
-        [startRank, firstRank].includes(x) &&
-        this.board[x + 2 * shiftX][y] == V.EMPTY
-      ) {
-        // Two squares jump
-        moves.push(this.getBasicMove([x, y], [x + 2 * shiftX, y]));
-      }
-    }
-    // Captures
-    for (let shiftY of [-1, 1]) {
-      if (
-        y + shiftY >= 0 &&
-        y + shiftY < sizeY &&
-        this.board[x + shiftX][y + shiftY] != V.EMPTY &&
-        this.canTake([x, y], [x + shiftX, y + shiftY])
-      ) {
-        for (let piece of finalPieces) {
-          moves.push(
-            this.getBasicMove([x, y], [x + shiftX, y + shiftY], {
-              c: color,
-              p: piece
-            })
-          );
-        }
-      }
-    }
-
     // En passant: always OK if subturn 1,
     // OK on subturn 2 only if enPassant was played at subturn 1
     // (and if there are two e.p. squares available).
@@ -99,7 +49,7 @@ export const VariantRules = class MarseilleRules extends ChessRules {
       if (sq) epSqs.push(sq);
     });
     if (epSqs.length == 0) return moves;
-    const oppCol = V.GetOppCol(color);
+    const oppCol = V.GetOppCol(this.getColor(x, y));
     for (let sq of epSqs) {
       if (
         this.subTurn == 1 ||
@@ -126,7 +76,6 @@ export const VariantRules = class MarseilleRules extends ChessRules {
         }
       }
     }
-
     return moves;
   }
 
@@ -158,7 +107,35 @@ export const VariantRules = class MarseilleRules extends ChessRules {
       }
       this.subTurn = 3 - this.subTurn;
     }
-    this.updateVariables(move);
+    this.postPlay(move);
+  }
+
+  postPlay(move) {
+    const c = move.turn.charAt(0);
+    const piece = move.vanish[0].p;
+    const firstRank = c == "w" ? V.size.x - 1 : 0;
+
+    if (piece == V.KING && move.appear.length > 0) {
+      this.kingPos[c][0] = move.appear[0].x;
+      this.kingPos[c][1] = move.appear[0].y;
+      this.castleFlags[c] = [V.size.y, V.size.y];
+      return;
+    }
+    const oppCol = V.GetOppCol(c);
+    const oppFirstRank = V.size.x - 1 - firstRank;
+    if (
+      move.start.x == firstRank && //our rook moves?
+      this.castleFlags[c].includes(move.start.y)
+    ) {
+      const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1);
+      this.castleFlags[c][flagIdx] = V.size.y;
+    } else if (
+      move.end.x == oppFirstRank && //we took opponent rook?
+      this.castleFlags[oppCol].includes(move.end.y)
+    ) {
+      const flagIdx = (move.end.y == this.castleFlags[oppCol][0] ? 0 : 1);
+      this.castleFlags[oppCol][flagIdx] = V.size.y;
+    }
   }
 
   undo(move) {
@@ -176,7 +153,7 @@ export const VariantRules = class MarseilleRules extends ChessRules {
     }
     this.turn = move.turn[0];
     this.subTurn = parseInt(move.turn[1]);
-    this.unupdateVariables(move);
+    super.postUndo(move);
   }
 
   // NOTE:  GenRandInitFen() is OK,