Fix Alice rules (en passant)
[vchess.git] / client / src / variants / Alice.js
index aa95692..57de568 100644 (file)
@@ -2,8 +2,9 @@ import { ChessRules } from "@/base_rules";
 import { ArrayFun } from "@/utils/array";
 
 // NOTE: alternative implementation, probably cleaner = use only 1 board
-// TODO? atLeastOneMove() would be more efficient if rewritten here (less sideBoard computations)
-export const VariantRules = class AliceRules extends ChessRules {
+// TODO? atLeastOneMove() would be more efficient if rewritten here
+// (less sideBoard computations)
+export class AliceRules extends ChessRules {
   static get ALICE_PIECES() {
     return {
       s: "p",
@@ -33,11 +34,38 @@ export const VariantRules = class AliceRules extends ChessRules {
     return (Object.keys(V.ALICE_PIECES).includes(b[1]) ? "Alice/" : "") + b;
   }
 
+  getEpSquare(moveOrSquare) {
+    if (!moveOrSquare) return undefined;
+    if (typeof moveOrSquare === "string") {
+      const square = moveOrSquare;
+      if (square == "-") return undefined;
+      return V.SquareToCoords(square);
+    }
+    // Argument is a move:
+    const move = moveOrSquare;
+    const s = move.start,
+          e = move.end;
+    if (
+      s.y == e.y &&
+      Math.abs(s.x - e.x) == 2 &&
+      // Special conditions: a pawn can be on the other side
+      ['p','s'].includes(move.appear[0].p) &&
+      ['p','s'].includes(move.vanish[0].p)
+    ) {
+      return {
+        x: (s.x + e.x) / 2,
+        y: s.y
+      };
+    }
+    return undefined; //default
+  }
+
   setOtherVariables(fen) {
     super.setOtherVariables(fen);
     const rows = V.ParseFen(fen).position.split("/");
     if (this.kingPos["w"][0] < 0 || this.kingPos["b"][0] < 0) {
-      // INIT_COL_XXX won't be required if Alice kings are found (means 'king moved')
+      // INIT_COL_XXX won't be required if Alice kings are found
+      // (it means 'king moved')
       for (let i = 0; i < rows.length; i++) {
         let k = 0; //column index on board
         for (let j = 0; j < rows[i].length; j++) {
@@ -80,7 +108,8 @@ export const VariantRules = class AliceRules extends ChessRules {
     return sideBoard;
   }
 
-  // NOTE: castle & enPassant https://www.chessvariants.com/other.dir/alice.html
+  // NOTE: castle & enPassant
+  // https://www.chessvariants.com/other.dir/alice.html
   getPotentialMovesFrom([x, y], sideBoard) {
     const pieces = Object.keys(V.ALICE_CODES);
     const codes = Object.keys(V.ALICE_PIECES);
@@ -107,11 +136,13 @@ export const VariantRules = class AliceRules extends ChessRules {
     // Finally filter impossible moves
     const res = moves.filter(m => {
       if (m.appear.length == 2) {
-        //castle
-        // appear[i] must be an empty square on the other board
+        // Castle: appear[i] must be an empty square on the other board
         for (let psq of m.appear) {
-          if (this.getSquareOccupation(psq.x, psq.y, 3 - mirrorSide) != V.EMPTY)
+          if (
+            this.getSquareOccupation(psq.x, psq.y, 3 - mirrorSide) != V.EMPTY
+          ) {
             return false;
+          }
         }
       } else if (this.board[m.end.x][m.end.y] != V.EMPTY) {
         // Attempt to capture
@@ -126,11 +157,12 @@ export const VariantRules = class AliceRules extends ChessRules {
       // If the move is computed on board1, m.appear change for Alice pieces.
       if (mirrorSide == 1) {
         m.appear.forEach(psq => {
-          //forEach: castling taken into account
+          // forEach: castling taken into account
           psq.p = V.ALICE_CODES[psq.p]; //goto board2
         });
-      } //move on board2: mark vanishing pieces as Alice
+      }
       else {
+        // Move on board2: mark vanishing pieces as Alice
         m.vanish.forEach(psq => {
           psq.p = V.ALICE_CODES[psq.p];
         });
@@ -159,6 +191,31 @@ export const VariantRules = class AliceRules extends ChessRules {
     return res;
   }
 
+  getEnpassantCaptures([x, y], shiftX) {
+    const Lep = this.epSquares.length;
+    const epSquare = this.epSquares[Lep - 1]; //always at least one element
+    let enpassantMove = null;
+    if (
+      !!epSquare &&
+      epSquare.x == x + shiftX &&
+      Math.abs(epSquare.y - y) == 1
+    ) {
+      enpassantMove = this.getBasicMove([x, y], [epSquare.x, epSquare.y]);
+      // May capture in same world or different:
+      const capturedPiece =
+        this.board[x][epSquare.y] != V.EMPTY
+          ? this.getPiece(x, epSquare.y)
+          : ['p','s'][1 - "ps".indexOf(this.getPiece(x, y))];
+      enpassantMove.vanish.push({
+        x: x,
+        y: epSquare.y,
+        p: capturedPiece,
+        c: V.GetOppCol(this.turn)
+      });
+    }
+    return !!enpassantMove ? [enpassantMove] : [];
+  }
+
   filterValid(moves, sideBoard) {
     if (moves.length == 0) return [];
     if (!sideBoard) sideBoard = [this.getSideBoard(1), this.getSideBoard(2)];
@@ -229,7 +286,8 @@ export const VariantRules = class AliceRules extends ChessRules {
     return res;
   }
 
-  getCheckSquares(color) {
+  getCheckSquares() {
+    const color = this.turn;
     const pieces = Object.keys(V.ALICE_CODES);
     const kp = this.kingPos[color];
     const mirrorSide = pieces.includes(this.getPiece(kp[0], kp[1])) ? 1 : 2;
@@ -243,29 +301,27 @@ export const VariantRules = class AliceRules extends ChessRules {
     return res;
   }
 
-  updateVariables(move) {
-    super.updateVariables(move); //standard king
+  postPlay(move) {
+    super.postPlay(move); //standard king
     const piece = move.vanish[0].p;
     const c = move.vanish[0].c;
     // "l" = Alice king
     if (piece == "l") {
       this.kingPos[c][0] = move.appear[0].x;
       this.kingPos[c][1] = move.appear[0].y;
-      this.castleFlags[c] = [false, false];
+      this.castleFlags[c] = [8, 8];
     }
   }
 
-  unupdateVariables(move) {
-    super.unupdateVariables(move);
+  postUndo(move) {
+    super.postUndo(move);
     const c = move.vanish[0].c;
-    if (move.vanish[0].p == "l") this.kingPos[c] = [move.start.x, move.start.y];
+    if (move.vanish[0].p == "l")
+      this.kingPos[c] = [move.start.x, move.start.y];
   }
 
   getCurrentScore() {
-    if (this.atLeastOneMove())
-      // game not over
-      return "*";
-
+    if (this.atLeastOneMove()) return "*";
     const pieces = Object.keys(V.ALICE_CODES);
     const color = this.turn;
     const kp = this.kingPos[color];
@@ -295,6 +351,10 @@ export const VariantRules = class AliceRules extends ChessRules {
     );
   }
 
+  static get SEARCH_DEPTH() {
+    return 2;
+  }
+
   getNotation(move) {
     if (move.appear.length == 2 && move.appear[0].p == V.KING) {
       if (move.end.y < move.start.y) return "0-0-0";