Add Bario draft. Small bugs to fix in Refusal and Bario
[vchess.git] / client / src / variants / Wormhole.js
index 41c934a..05d8f2a 100644 (file)
@@ -1,6 +1,7 @@
 import { ChessRules } from "@/base_rules";
 
-export const VariantRules = class WormholeRules extends ChessRules {
+export class WormholeRules extends ChessRules {
+
   static get HasFlags() {
     return false;
   }
@@ -28,13 +29,36 @@ export const VariantRules = class WormholeRules extends ChessRules {
     return b;
   }
 
+  static IsGoodPosition(position) {
+    if (position.length == 0) return false;
+    const rows = position.split("/");
+    if (rows.length != V.size.x) return false;
+    let kings = { "k": 0, "K": 0 };
+    for (let row of rows) {
+      let sumElts = 0;
+      for (let i = 0; i < row.length; i++) {
+        if (['K','k'].includes(row[i])) kings[row[i]]++;
+        if (['x'].concat(V.PIECES).includes(row[i].toLowerCase())) sumElts++;
+        else {
+          const num = parseInt(row[i], 10);
+          if (isNaN(num)) return false;
+          sumElts += num;
+        }
+      }
+      if (sumElts != V.size.y) return false;
+    }
+    if (Object.values(kings).some(v => v != 1)) return false;
+    return true;
+  }
+
   getSquareAfter(square, movement) {
     let shift1, shift2;
     if (Array.isArray(movement[0])) {
       // A knight
       shift1 = movement[0];
       shift2 = movement[1];
-    } else {
+    }
+    else {
       shift1 = movement;
       shift2 = null;
     }
@@ -270,8 +294,7 @@ export const VariantRules = class WormholeRules extends ChessRules {
   }
 
   getCurrentScore() {
-    if (this.atLeastOneMove())
-      return "*";
+    if (this.atLeastOneMove()) return "*";
     // No valid move: I lose
     return this.turn == "w" ? "0-1" : "1-0";
   }
@@ -306,4 +329,5 @@ export const VariantRules = class WormholeRules extends ChessRules {
       notation += "=" + move.appear[0].p.toUpperCase();
     return notation;
   }
+
 };