Several small improvements + integrate options + first working draft of Cwda
[vchess.git] / client / src / variants / Royalrace.js
index ff6e1e1..3be0c14 100644 (file)
@@ -2,7 +2,8 @@ import { ChessRules } from "@/base_rules";
 import { ArrayFun } from "@/utils/array";
 import { randInt, shuffle } from "@/utils/alea";
 
-export const VariantRules = class RoyalraceRules extends ChessRules {
+export class RoyalraceRules extends ChessRules {
+
   static get HasFlags() {
     return false;
   }
@@ -19,15 +20,18 @@ export const VariantRules = class RoyalraceRules extends ChessRules {
     return { x: 11, y: 11 };
   }
 
-  static GenRandInitFen(randomness) {
-    if (!randomness) randomness = 2;
-    if (randomness == 0)
-      return "11/11/11/11/11/11/11/11/11/QRBNP1pnbrq/KRBNP1pnbrk w 0";
+  getPpath(b) {
+    return (b[1] == V.KNIGHT ? "Enpassant/" : "") + b;
+  }
+
+  static GenRandInitFen(options) {
+    if (options.randomness == 0)
+      return "92/92/92/92/92/92/92/92/92/qrbnp1PNBRQ/krbnp1PNBRK w 0";
 
     let pieces = { w: new Array(10), b: new Array(10) };
     // Shuffle pieces on first and second rank
     for (let c of ["w", "b"]) {
-      if (c == 'b' && randomness == 1) {
+      if (c == 'b' && options.randomness == 1) {
         pieces['b'] = JSON.parse(JSON.stringify(pieces['w'])).reverse();
         pieces['b'] =
           pieces['b'].splice(5,10).reverse().concat(
@@ -93,18 +97,19 @@ export const VariantRules = class RoyalraceRules extends ChessRules {
     const whiteFen = pieces["w"].join("").toUpperCase();
     const blackFen = pieces["b"].join("");
     return (
-      "11/11/11/11/11/11/11/11/11/" +
-      whiteFen.substr(5).split("").reverse().join("") +
+      "92/92/92/92/92/92/92/92/92/" +
+      blackFen.substr(5).split("").reverse().join("") +
       "1" +
-      blackFen.substr(5).split("").join("") +
+      whiteFen.substr(5).split("").join("") +
       "/" +
-      whiteFen.substr(0,5) +
+      blackFen.substr(0,5) +
       "1" +
-      blackFen.substr(0,5).split("").reverse().join("") +
+      whiteFen.substr(0,5).split("").reverse().join("") +
       " w 0"
     );
   }
 
+  // TODO: simplify this when base function is more general.
   getPotentialPawnMoves([x, y]) {
     // Normal moves (as a rook)
     let moves =
@@ -134,15 +139,6 @@ export const VariantRules = class RoyalraceRules extends ChessRules {
     return this.getSlideNJumpMoves(sq, V.steps[V.KNIGHT]);
   }
 
-  // What are the king moves from square x,y ?
-  getPotentialKingMoves(sq) {
-    return this.getSlideNJumpMoves(
-      sq,
-      V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
-      "oneStep"
-    );
-  }
-
   filterValid(moves) {
     if (moves.length == 0) return [];
     const color = this.turn;
@@ -156,32 +152,15 @@ export const VariantRules = class RoyalraceRules extends ChessRules {
     });
   }
 
-  isAttackedByPawn([x, y], colors) {
-    const pawnShift = 1;
-    if (x + pawnShift < V.size.x) {
-      for (let c of colors) {
-        for (let i of [-1, 1]) {
-          if (
-            y + i >= 0 &&
-            y + i < V.size.y &&
-            this.getPiece(x + pawnShift, y + i) == V.PAWN &&
-            this.getColor(x + pawnShift, y + i) == c
-          ) {
-            return true;
-          }
-        }
-      }
-    }
-    return false;
+  isAttackedByPawn([x, y], color) {
+    // Pawns can capture forward and backward:
+    return this.isAttackedBySlideNJump(
+      sq, color, V.PAWN, V.steps[V.BISHOP], 1);
   }
 
-  isAttackedByKnight(sq, colors) {
+  isAttackedByKnight(sq, color) {
     return this.isAttackedBySlideNJump(
-      sq,
-      colors,
-      V.KNIGHT,
-      V.steps[V.KNIGHT]
-    );
+      sq, color, V.KNIGHT, V.steps[V.KNIGHT]);
   }
 
   getCurrentScore() {
@@ -190,8 +169,7 @@ export const VariantRules = class RoyalraceRules extends ChessRules {
     if (this.kingPos[color][0] == 0)
       // The opposing edge is reached!
       return color == "w" ? "1-0" : "0-1";
-    if (this.atLeastOneMove())
-      return "*";
+    if (this.atLeastOneMove()) return "*";
     // Stalemate (will probably never happen)
     return "1/2";
   }
@@ -226,4 +204,5 @@ export const VariantRules = class RoyalraceRules extends ChessRules {
       V.CoordsToSquare(move.end)
     );
   }
+
 };