Several small improvements + integrate options + first working draft of Cwda
[vchess.git] / client / src / variants / Monster.js
index 071c8db..bbba319 100644 (file)
@@ -2,21 +2,54 @@ import { ChessRules } from "@/base_rules";
 import { randInt } from "@/utils/alea";
 
 export class MonsterRules extends ChessRules {
+
+  static get Options() {
+    return {
+      check: [
+        {
+          label: "Random",
+          defaut: false,
+          variable: "random"
+        }
+      ],
+      select: [
+        {
+          label: "Number of pawns",
+          variable: "pawnsCount",
+          defaut: 4,
+          options: [
+            { label: "Two", value: 2 },
+            { label: "Four", value: 4 },
+            { label: "Six", value: 6 }
+          ]
+        }
+      ]
+    };
+  }
+
+  static AbbreviateOptions(opts) {
+    return opts["pawnsCount"];
+  }
+
   static IsGoodFlags(flags) {
     // Only black can castle
     return !!flags.match(/^[a-z]{2,2}$/);
   }
 
-  static GenRandInitFen(randomness) {
-    if (randomness == 2) randomness--;
-    const fen = ChessRules.GenRandInitFen(randomness);
+  static GenRandInitFen(options) {
+    const baseFen = ChessRules.GenRandInitFen(
+      { randomness: (options.random ? 1 : 0) });
+    let pawnsLine = "";
+    switch (options.pawnsCount) {
+      case 2: pawnsLine = "3PP3"; break;
+      case 4: pawnsLine = "2PPPP2"; break;
+      case 6: pawnsLine = "1PPPPPP1"; break;
+    }
     return (
       // 26 first chars are 6 rows + 6 slashes
-      fen.substr(0, 26)
+      baseFen.substr(0, 26) + pawnsLine + "/4K3 w 0 " +
       // En passant available, and "half-castle"
-      .concat("1PPPPPP1/4K3 w 0 ")
-      .concat(fen.substr(-6, 2))
-      .concat(" -")
+      baseFen.substr(-6, 2) + " -"
     );
   }
 
@@ -39,13 +72,10 @@ export class MonsterRules extends ChessRules {
     if (this.getColor(x, y) == 'b') return super.getPotentialKingMoves([x, y]);
     // White doesn't castle:
     return this.getSlideNJumpMoves(
-      [x, y],
-      V.steps[V.ROOK].concat(V.steps[V.BISHOP]),
-      "oneStep"
-    );
+      [x, y], V.steps[V.ROOK].concat(V.steps[V.BISHOP]), 1);
   }
 
-  isAttacked(sq, color, castling) {
+  isAttacked() {
     // Goal is king capture => no checks
     return false;
   }
@@ -72,9 +102,17 @@ export class MonsterRules extends ChessRules {
     V.PlayOnBoard(this.board, move);
     if (this.turn == 'w') {
       if (this.subTurn == 1) this.movesCount++;
-      else this.turn = 'b';
-      this.subTurn = 3 - this.subTurn;
-    } else {
+      if (
+        this.subTurn == 2 ||
+        // King captured
+        (move.vanish.length == 2 && move.vanish[1].p == V.KING)
+      ) {
+        this.turn = 'b';
+        this.subTurn = 1;
+      }
+      else this.subTurn = 2;
+    }
+    else {
       this.turn = 'w';
       this.movesCount++;
     }
@@ -108,9 +146,11 @@ export class MonsterRules extends ChessRules {
     const piece = move.vanish[0].p;
     if (piece == V.KING)
       this.kingPos[c] = [move.appear[0].x, move.appear[0].y];
-    if (move.vanish.length == 2 && move.vanish[1].p == V.KING)
+    if (move.vanish.length == 2 && move.vanish[1].p == V.KING) {
       // Opponent's king is captured, game over
       this.kingPos[move.vanish[1].c] = [-1, -1];
+      move.captureKing = true; //for undo
+    }
     this.updateCastleFlags(move, piece);
   }
 
@@ -125,7 +165,7 @@ export class MonsterRules extends ChessRules {
     }
     else {
       this.turn = 'w';
-      this.subTurn = 2;
+      this.subTurn = (!move.captureKing ? 2 : 1);
     }
     this.postUndo(move);
   }
@@ -204,4 +244,5 @@ export class MonsterRules extends ChessRules {
     const color = this.turn;
     return (color == 'w' ? getBestWhiteMove() : getBestBlackMove());
   }
+
 };