Clorange: almost functional draft (promotions, capture non-violent...)
[xogo.git] / variants / Clorange / class.js
index 38cb958..01d7189 100644 (file)
@@ -2,7 +2,13 @@ import ChessRules from "/base_rules.js";
 
 export default class ClorangeRules extends ChessRules {
 
-  // TODO: options : disable teleport/recycle at least ?
+  static get Options() {
+    return {
+      select: C.Options.select,
+      styles:
+        C.Options.styles.filter(s => !["crazyhouse","recycle"].includes(s))
+    };
+  }
 
   get hasReserve() {
     return true;
@@ -23,44 +29,48 @@ export default class ClorangeRules extends ChessRules {
     res['o'] = {"class": "nv-knight", moveas: "n"};
     res['c'] = {"class": "nv-bishop", moveas: "b"};
     res['t'] = {"class": "nv-queen", moveas: "q"};
+    return res;
   }
 
-  setOtherVariables(fen) {
-    super.setOtherVariables(fen,
-      ['p', 'r', 'n', 'b', 'q', 's', 'u', 'o', 'c', 't']);
+  static get V_PIECES() {
+    return ['p', 'r', 'n', 'b', 'q'];
+  }
+  static get NV_PIECES() {
+    return ['s', 'u', 'o', 'c', 't'];
   }
 
-  postProcessPotentialMoves(moves) {
-    // Remove captures for non-violent pieces:
-    return super.postProcessPotentialMoves(moves).filter(m => {
-      return (
-        m.vanish.length != 2 ||
-        m.appear.length != 1 ||
-        ['p', 'r', 'n', 'b', 'q'].includes(m.vanish[0].p)
-      );
-    });
+  setOtherVariables(fen) {
+    super.setOtherVariables(fen, V.V_PIECES.concat(V.NV_PIECES));
   }
 
+  // Forbid non-violent pieces to capture
   canTake([x1, y1], [x2, y2]) {
     return (
       this.getColor(x1, y1) !== this.getColor(x2, y2) &&
-      ['p', 'r', 'n', 'b', 'q', 'k'].includes(this.getPiece(x1, y1))
+      (['k'].concat(V.V_PIECES)).includes(this.getPiece(x1, y1))
     );
   }
 
   prePlay(move) {
     super.prePlay(move);
     // No crazyhouse or recycle, so the last call didn't update reserve:
-    if (move.vanish.length == 2 && move.appear.length == 1) {
-      // Capture: update reserves
-      this.Reserve[move.vanish
-      const pIdx = ['p', 'r', 'n', 'b', 'q'].indexOf(move.vanish[1].p);
-      // TODO
-      if normal
-          ? ChessRules.PIECES.findIndex(p => p == move.vanish[1].p)
-          : V.NON_VIOLENT.findIndex(p => p == move.vanish[1].p);
-      const rPiece = (normal ? V.NON_VIOLENT : ChessRules.PIECES)[pIdx];
-      this.reserve[move.vanish[1].c][rPiece]++;
+    if (
+      (move.vanish.length == 2 && move.appear.length == 1) ||
+      move.vanish.length == 0 //drop
+    ) {
+      const trPiece =
+        (move.vanish.length > 0 ? move.vanish[1].p : move.appear[0].p);
+      const normal = V.V_PIECES.includes(trPiece);
+      const pIdx = (normal ? V.V_PIECES : V.NV_PIECES).indexOf(trPiece);
+      const resPiece = (normal ? V.NV_PIECES : V.V_PIECES)[pIdx];
+      if (move.vanish.length > 0) {
+        super.updateReserve(C.GetOppTurn(this.turn), resPiece,
+          this.reserve[C.GetOppTurn(this.turn)][resPiece] + 1);
+      }
+      else {
+        super.updateReserve(this.turn, resPiece,
+          this.reserve[this.turn][resPiece] - 1);
+      }
     }
   }