Some fixes, draw lines on board, add 7 variants
[vchess.git] / client / src / variants / Shatranj.js
index bb00101..d8e4460 100644 (file)
@@ -1,9 +1,6 @@
-// TODO: bishop OK, but queen should move vertical/horizontal and capture diagonally.
-// ==> then the pawn promotion is a real promotion (enhancement).
-
 import { ChessRules } from "@/base_rules";
 
-export const VariantRules = class ShatranjRules extends ChessRules {
+export class ShatranjRules extends ChessRules {
   static get HasFlags() {
     return false;
   }
@@ -12,6 +9,26 @@ export const VariantRules = class ShatranjRules extends ChessRules {
     return false;
   }
 
+  static get Monochrome() {
+    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],
@@ -21,47 +38,9 @@ export const VariantRules = class ShatranjRules extends ChessRules {
     ];
   }
 
-  static GenRandInitFen() {
-    return ChessRules.GenRandInitFen().replace("w 1111 -", "w");
-  }
-
-  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;
+  static GenRandInitFen(randomness) {
+    // Remove castle flags and en-passant indication
+    return ChessRules.GenRandInitFen(randomness).slice(0, -7);
   }
 
   getPotentialBishopMoves(sq) {
@@ -92,28 +71,20 @@ export const VariantRules = class ShatranjRules extends ChessRules {
     );
   }
 
-  getPotentialKingMoves(sq) {
-    return this.getSlideNJumpMoves(
-      sq,
-      V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
-      "oneStep"
-    );
-  }
-
-  isAttackedByBishop(sq, colors) {
+  isAttackedByBishop(sq, color) {
     return this.isAttackedBySlideNJump(
       sq,
-      colors,
+      color,
       V.BISHOP,
       V.ElephantSteps,
       "oneStep"
     );
   }
 
-  isAttackedByQueen(sq, colors) {
+  isAttackedByQueen(sq, color) {
     return this.isAttackedBySlideNJump(
       sq,
-      colors,
+      color,
       V.QUEEN,
       V.steps[V.BISHOP],
       "oneStep"
@@ -159,7 +130,7 @@ export const VariantRules = class ShatranjRules extends ChessRules {
       // 2 enemy units or more: I lose
       return getScoreLost();
     // I don't have any piece, my opponent have one: can I take it?
-    if (this.isAttacked(piecesLeft[oppCol].square, [color]))
+    if (this.isAttacked(piecesLeft[oppCol].square, color))
       // Yes! But I still need to take it
       return "*";
     // No :(