Generalize pawn movements: cleaner and smaller code
[vchess.git] / client / src / variants / Rifle.js
index 61c2c35..98661ae 100644 (file)
@@ -1,9 +1,11 @@
 import { ChessRules, PiPo, Move } from "@/base_rules";
 
-export const VariantRules = class RifleRules extends ChessRules {
-  static get HasEnpassant() {
-    // Due to the capturing mode, en passant is disabled
-    return false;
+export class RifleRules extends ChessRules {
+  getEpSquare(moveOrSquare) {
+    if (typeof moveOrSquare !== "object" || moveOrSquare.appear.length > 0)
+      return super.getEpSquare(moveOrSquare);
+    // Capturing move: no en-passant
+    return undefined;
   }
 
   getBasicMove([sx, sy], [ex, ey], tr) {
@@ -46,4 +48,34 @@ export const VariantRules = class RifleRules extends ChessRules {
 
     return mv;
   }
+
+  getEnpassantCaptures([x, y], shiftX) {
+    let moves = [];
+    const Lep = this.epSquares.length;
+    const epSquare = this.epSquares[Lep - 1]; //always at least one element
+    if (
+      !!epSquare &&
+      epSquare.x == x + shiftX &&
+      Math.abs(epSquare.y - y) == 1
+    ) {
+      let enpassantMove = new Move({
+        appear: [],
+        vanish: [],
+        start: {x:x, y:y},
+        end: {x:x+shiftX, y:epSquare.y}
+      });
+      enpassantMove.vanish.push({
+        x: x,
+        y: epSquare.y,
+        p: "p",
+        c: this.getColor(x, epSquare.y)
+      });
+      moves.push(enpassantMove);
+    }
+    return moves;
+  }
+
+  static get SEARCH_DEPTH() {
+    return 2;
+  }
 };