New variant idea
[xogo.git] / variants / Suction / class.js
index db10e58..ff307d4 100644 (file)
@@ -1,5 +1,5 @@
 import ChessRules from "/base_rules.js";
-import GiveawayRules from "/variants/Giveaway/class.js";
+import {FenUtil} from "/utils/setupPieces.js";
 import PiPo from "/utils/PiPo.js";
 import Move from "/utils/Move.js";
 
@@ -41,19 +41,28 @@ export default class SuctionRules extends ChessRules {
     }
   }
 
-  genRandInitFen(seed) {
-    const gr = new GiveawayRules(
-      {mode: "suicide", options: this.options, genFenOnly: true});
-    // Add empty cmove:
-    return (
-      gr.genRandInitFen(seed).slice(0, -17) + '{"enpassant":"-","cmove":"-"}');
+  genRandInitBaseFen() {
+    const s = FenUtil.setupPieces(
+      ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'],
+      {
+        randomness: this.options["randomness"],
+        diffCol: ['b']
+      }
+    );
+    return {
+      fen: s.b.join("") + "/pppppppp/8/8/8/8/PPPPPPPP/" +
+           s.w.join("").toUpperCase(),
+      o: {}
+    };
   }
 
-  getFen() {
-    const cmoveFen = !this.cmove
+  getPartFen(o) {
+    let parts = super.getPartFen(o);
+    const cmoveFen = o.init || !this.cmove
       ? "-"
       : C.CoordsToSquare(this.cmove.start) + C.CoordsToSquare(this.cmove.end);
-    return super.getFen().slice(0, -1) + ',"' + cmoveFen + '"}';
+    parts["cmove"] = cmoveFen;
+    return parts;
   }
 
   getBasicMove([sx, sy], [ex, ey]) {
@@ -104,10 +113,19 @@ export default class SuctionRules extends ChessRules {
   getCurrentScore() {
     const color = this.turn;
     const kingPos = super.searchKingPos(color);
-    if (color == "w" && kingPos[0] == 0) return "0-1";
-    if (color == "b" && kingPos[0] == this.size.x - 1) return "1-0";
+    if (color == "w" && kingPos[0][0] == 0) return "0-1";
+    if (color == "b" && kingPos[0][0] == this.size.x - 1) return "1-0";
     // King is not on the opposite edge: game not over
     return "*";
   }
 
+  // Better animation for swaps
+  customAnimate(move, segments, cb) {
+    if (move.vanish.length < 2)
+      return 0;
+    super.animateMoving(move.end, move.start, null,
+                        segments.reverse().map(s => s.reverse()), cb);
+    return 1;
+  }
+
 };