Several small improvements + integrate options + first working draft of Cwda
[vchess.git] / client / src / variants / Rococo.js
index e0838e1..3101a7c 100644 (file)
@@ -3,6 +3,7 @@ import { ArrayFun } from "@/utils/array";
 import { shuffle } from "@/utils/alea";
 
 export class RococoRules extends ChessRules {
+
   static get HasFlags() {
     return false;
   }
@@ -54,7 +55,7 @@ export class RococoRules extends ChessRules {
             this.kingPos["w"] = [i, k];
             break;
           default: {
-            const num = parseInt(position[i].charAt(j));
+            const num = parseInt(position[i].charAt(j), 10);
             if (!isNaN(num)) k += num - 1;
           }
         }
@@ -141,7 +142,8 @@ export class RococoRules extends ChessRules {
     // Pre-check: is thing on this square immobilized?
     const imSq = this.isImmobilized([x, y]);
     const piece = this.getPiece(x, y);
-    if (!!imSq && piece != V.KING) {
+    if (!!imSq) {
+      if (piece == V.KING) return [];
       // Only option is suicide, if I'm not a king:
       return [
         new Move({
@@ -203,23 +205,11 @@ export class RococoRules extends ChessRules {
     // NOTE: not removing "dist" field; shouldn't matter much...
   }
 
-  getSlideNJumpMoves([x, y], steps, oneStep) {
-    const piece = this.getPiece(x, y);
-    let moves = [];
-    outerLoop: for (let step of steps) {
-      let i = x + step[0];
-      let j = y + step[1];
-      while (V.OnBoard(i, j) && this.board[i][j] == V.EMPTY) {
-        moves.push(this.getBasicMove([x, y], [i, j]));
-        if (oneStep !== undefined) continue outerLoop;
-        i += step[0];
-        j += step[1];
-      }
-      // Only king can take on occupied square:
-      if (piece == V.KING && V.OnBoard(i, j) && this.canTake([x, y], [i, j]))
-        moves.push(this.getBasicMove([x, y], [i, j]));
-    }
-    return moves;
+  canTake([x1, y1], [x2, y2]) {
+    return (
+      this.getPiece(x1, y1) == V.KING &&
+      this.getColor(x1, y1) != this.getColor(x2, y2)
+    );
   }
 
   // "Cannon/grasshopper pawn"
@@ -289,13 +279,12 @@ export class RococoRules extends ChessRules {
     return super.getPotentialQueenMoves(sq).concat(this.getRookCaptures(sq));
   }
 
-  getKnightCaptures(startSquare, byChameleon) {
+  getKnightCaptures([x, y], byChameleon) {
     // Look in every direction for captures
     const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
     const color = this.turn;
     const oppCol = V.GetOppCol(color);
     let moves = [];
-    const [x, y] = [startSquare[0], startSquare[1]];
     const piece = this.getPiece(x, y); //might be a chameleon!
     outerLoop: for (let step of steps) {
       let [i, j] = [x + step[0], y + step[1]];
@@ -335,7 +324,8 @@ export class RococoRules extends ChessRules {
             //TODO: redundant test
             continue outerLoop;
           }
-        } else {
+        }
+        else {
           moves.push(
             new Move({
               appear: [new PiPo({ x: cur[0], y: cur[1], c: color, p: piece })],
@@ -630,8 +620,8 @@ export class RococoRules extends ChessRules {
     return false;
   }
 
-  static GenRandInitFen(randomness) {
-    if (randomness == 0) {
+  static GenRandInitFen(options) {
+    if (options.randomness == 0) {
       return (
         "91/1rqnbknqm1/1pppppppp1/91/91/91/91/1PPPPPPPP1/1MQNBKNQR1/91 w 0 -"
       );
@@ -640,7 +630,7 @@ export class RococoRules extends ChessRules {
     let pieces = { w: new Array(8), b: new Array(8) };
     // Shuffle pieces on first and last rank
     for (let c of ["w", "b"]) {
-      if (c == 'b' && randomness == 1) {
+      if (c == 'b' && options.randomness == 1) {
         pieces['b'] = pieces['w'];
         break;
       }
@@ -714,11 +704,15 @@ export class RococoRules extends ChessRules {
     if (move.appear[0].p == V.PAWN) {
       // Pawn: generally ambiguous short notation, so we use full description
       notation = "P" + initialSquare + finalSquare;
-    } else if (move.appear[0].p == V.KING)
+    }
+    else if (move.appear[0].p == V.KING)
       notation = "K" + (move.vanish.length > 1 ? "x" : "") + finalSquare;
-    else notation = move.appear[0].p.toUpperCase() + finalSquare;
-    // Add a capture mark (not describing what is captured...):
-    if (move.vanish.length > 1 && move.appear[0].p != V.KING) notation += "X";
+    else {
+      notation = move.appear[0].p.toUpperCase() + finalSquare;
+      // Add a capture mark (not describing what is captured...):
+      if (move.vanish.length > 1) notation += "X";
+    }
     return notation;
   }
+
 };