Experimental symmetric randomness + deterministic option
[vchess.git] / client / src / variants / Baroque.js
index 8f1f9ba..a860fe7 100644 (file)
@@ -72,7 +72,7 @@ export const VariantRules = class BaroqueRules extends ChessRules {
       ) {
         const oppPiece = this.getPiece(i, j);
         if (oppPiece == V.IMMOBILIZER) {
-          // Moving is impossible only if this immobilizer is not neutralized
+          // Moving is possible only if this immobilizer is neutralized
           for (let step2 of adjacentSteps) {
             const [i2, j2] = [i + step2[0], j + step2[1]];
             if (i2 == x && j2 == y) continue; //skip initial piece!
@@ -474,8 +474,8 @@ export const VariantRules = class BaroqueRules extends ChessRules {
   }
 
   isAttackedByBishop([x, y], colors) {
-    // We cheat a little here: since this function is used exclusively for king,
-    // it's enough to check the immediate surrounding of the square.
+    // We cheat a little here: since this function is used exclusively for
+    // the king, it's enough to check the immediate surrounding of the square.
     const adjacentSteps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
     for (let step of adjacentSteps) {
       const [i, j] = [x + step[0], y + step[1]];
@@ -512,6 +512,23 @@ export const VariantRules = class BaroqueRules extends ChessRules {
     return false;
   }
 
+  isAttackedByKing([x, y], colors) {
+    const steps = V.steps[V.ROOK].concat(V.steps[V.BISHOP]);
+    for (let step of steps) {
+      let rx = x + step[0],
+          ry = y + step[1];
+      if (
+        V.OnBoard(rx, ry) &&
+        this.getPiece(rx, ry) === V.KING &&
+        colors.includes(this.getColor(rx, ry)) &&
+        !this.isImmobilized([rx, ry])
+      ) {
+        return true;
+      }
+    }
+    return false;
+  }
+
   static get VALUES() {
     return {
       p: 1,
@@ -528,10 +545,20 @@ export const VariantRules = class BaroqueRules extends ChessRules {
     return 2;
   }
 
-  static GenRandInitFen() {
+  static GenRandInitFen(randomness) {
+    if (!randomness) randomness = 2;
+    if (randomness == 0)
+      // Deterministic:
+      return "rnbqkbnrm/pppppppp/8/8/8/8/PPPPPPPP/MNBKQBNR w 0";
+
     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) {
+        pieces['b'] = pieces['w'];
+        break;
+      }
+
       let positions = ArrayFun.range(8);
       // Get random squares for every piece, totally freely