Several small improvements + integrate options + first working draft of Cwda
[vchess.git] / client / src / variants / Shatranj.js
index 969b819..568e2d9 100644 (file)
@@ -1,6 +1,7 @@
 import { ChessRules } from "@/base_rules";
 
-export const VariantRules = class ShatranjRules extends ChessRules {
+export class ShatranjRules extends ChessRules {
+
   static get HasFlags() {
     return false;
   }
@@ -9,6 +10,30 @@ export const VariantRules = class ShatranjRules extends ChessRules {
     return false;
   }
 
+  static get Monochrome() {
+    return true;
+  }
+
+  static get Notoodark() {
+    return true;
+  }
+
+  static get PawnSpecs() {
+    return Object.assign(
+      {},
+      ChessRules.PawnSpecs,
+      {
+        twoSquares: false,
+        promotions: [V.QUEEN]
+      }
+    );
+  }
+
+  getPpath(b) {
+    if (b[1] == 'b') return "Shatranj/" + b;
+    return b;
+  }
+
   static get ElephantSteps() {
     return [
       [-2, -2],
@@ -18,104 +43,40 @@ export const VariantRules = class ShatranjRules extends ChessRules {
     ];
   }
 
-  static GenRandInitFen(randomness) {
+  static GenRandInitFen(options) {
     // Remove castle flags and en-passant indication
-    return ChessRules.GenRandInitFen(randomness).slice(0, -7);
-  }
-
-  getPotentialPawnMoves([x, y]) {
-    const color = this.turn;
-    let moves = [];
-    const [sizeX, sizeY] = [V.size.x, V.size.y];
-    const shiftX = color == "w" ? -1 : 1;
-    const startRank = color == "w" ? sizeX - 2 : 1;
-    const lastRank = color == "w" ? 0 : sizeX - 1;
-    // Promotion in minister (queen) only:
-    const finalPiece = x + shiftX == lastRank ? V.QUEEN : V.PAWN;
-
-    if (this.board[x + shiftX][y] == V.EMPTY) {
-      // One square forward
-      moves.push(
-        this.getBasicMove([x, y], [x + shiftX, y], {
-          c: color,
-          p: finalPiece
-        })
-      );
-    }
-    // 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])
-      ) {
-        moves.push(
-          this.getBasicMove([x, y], [x + shiftX, y + shiftY], {
-            c: color,
-            p: finalPiece
-          })
-        );
-      }
-    }
-
-    return moves;
+    return ChessRules.GenRandInitFen(options).slice(0, -7);
   }
 
   getPotentialBishopMoves(sq) {
-    let moves = this.getSlideNJumpMoves(sq, V.ElephantSteps, "oneStep");
+    let moves = this.getSlideNJumpMoves(sq, V.ElephantSteps, 1);
     // Complete with "repositioning moves": like a queen, without capture
-    let repositioningMoves = this.getSlideNJumpMoves(
-      sq,
-      V.steps[V.BISHOP],
-      "oneStep"
-    ).filter(m => m.vanish.length == 1);
+    let repositioningMoves =
+      this.getSlideNJumpMoves(sq, V.steps[V.BISHOP], 1)
+      .filter(m => m.vanish.length == 1);
     return moves.concat(repositioningMoves);
   }
 
   getPotentialQueenMoves(sq) {
     // Diagonal capturing moves
-    let captures = this.getSlideNJumpMoves(
-      sq,
-      V.steps[V.BISHOP],
-      "oneStep"
-    ).filter(m => m.vanish.length == 2);
+    let captures =
+      this.getSlideNJumpMoves(sq, V.steps[V.BISHOP], 1)
+      .filter(m => m.vanish.length == 2);
     return captures.concat(
       // Orthogonal non-capturing moves
-      this.getSlideNJumpMoves(
-        sq,
-        V.steps[V.ROOK],
-        "oneStep"
-      ).filter(m => m.vanish.length == 1)
-    );
-  }
-
-  getPotentialKingMoves(sq) {
-    return this.getSlideNJumpMoves(
-      sq,
-      V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
-      "oneStep"
+      this.getSlideNJumpMoves(sq, V.steps[V.ROOK], 1)
+      .filter(m => m.vanish.length == 1)
     );
   }
 
   isAttackedByBishop(sq, color) {
     return this.isAttackedBySlideNJump(
-      sq,
-      color,
-      V.BISHOP,
-      V.ElephantSteps,
-      "oneStep"
-    );
+      sq, color, V.BISHOP, V.ElephantSteps, 1);
   }
 
   isAttackedByQueen(sq, color) {
     return this.isAttackedBySlideNJump(
-      sq,
-      color,
-      V.QUEEN,
-      V.steps[V.BISHOP],
-      "oneStep"
-    );
+      sq, color, V.QUEEN, V.steps[V.BISHOP], 1);
   }
 
   getCurrentScore() {
@@ -174,4 +135,5 @@ export const VariantRules = class ShatranjRules extends ChessRules {
       k: 1000
     };
   }
+
 };