From d01282a527e60af95f2a71deee1fbac9c0dd26be Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Mon, 1 Jan 2024 08:51:47 +0100
Subject: [PATCH] Draft Dice chess

---
 variants.js              |   2 +-
 variants/Dice/class.js   | 108 +++++++++++++++++++++++++++++++++++++++
 variants/Dice/rules.html |   3 ++
 variants/Dice/style.css  |  12 +++++
 4 files changed, 124 insertions(+), 1 deletion(-)
 create mode 100644 variants/Dice/class.js
 create mode 100644 variants/Dice/rules.html
 create mode 100644 variants/Dice/style.css

diff --git a/variants.js b/variants.js
index c9cdc98..cad081a 100644
--- a/variants.js
+++ b/variants.js
@@ -42,7 +42,7 @@ const variants = [
   {name: 'Cwda', desc: 'New teams', disp: 'Different armies'},
   {name: 'Dark', desc: 'In the shadow'},
   {name: 'Diamond', desc: 'Rotating board'},
-//  {name: 'Dice', desc: 'Roll the dice'},
+  {name: 'Dice', desc: 'Roll the dice'},
 //  {name: 'Discoduel', desc: 'Enter the disco', disp: 'Disco Duel'},
 //  {name: 'Dobutsu', desc: "Let's catch the Lion!"},
 //  {name: 'Doublearmy', desc: '64 pieces on the board', disp: 'Double Army'},
diff --git a/variants/Dice/class.js b/variants/Dice/class.js
new file mode 100644
index 0000000..8c3eed4
--- /dev/null
+++ b/variants/Dice/class.js
@@ -0,0 +1,108 @@
+import ChessRules from "/base_rules.js";
+import {Random} from "/utils/alea.js";
+
+export default class DiceRules extends ChessRules {
+
+  static get Options() {
+    let res = C.Options;
+    res.select["defaut"] = 2;
+    return {
+      select: res.select,
+      input: [
+        {
+          label: "Biased alea",
+          variable: "biased",
+          type: "checkbox",
+          defaut: true
+        },
+        {
+          label: "Falling pawn",
+          variable: "pawnfall",
+          type: "checkbox",
+          defaut: false
+        }
+      ],
+      styles: [
+        "atomic",
+        "capture",
+        "crazyhouse",
+        "cylinder",
+        "madrasi",
+        "recycle",
+        "rifle",
+        "zen"
+      ]
+    };
+  }
+
+  getPartFen(o) {
+    let toplay = '';
+    if (o.init) {
+      let canMove = (this.options["biased"]
+        ? Array(8).fill('p').concat(Array(2).fill('n'))
+        : ['p', 'n']);
+      toplay = canMove[Random.randInt(canMove.length)];
+    }
+    return Object.assign(
+      { toplay: (o.init ? toplay : this.getRandomPiece(this.turn)) },
+      super.getPartFen(o)
+    );
+  }
+
+  constructor(o) {
+    super(o);
+    this.afterPlay = (move_s, newTurn, ops) => {
+      // Movestack contains only one move:
+      move_s[0].toplay = this.getRandomPiece(this.turn);
+      super.displayMessage(this.message, move_s[0].toplay);
+      o.afterPlay(move_s, newTurn, ops);
+    };
+  }
+
+  setOtherVariables(fenParsed) {
+    super.setOtherVariables(fenParsed);
+    this.toplay = fenParsed.toplay;
+    this.message = document.createElement("div");
+    C.AddClass_es(this.message, "piece-text");
+    this.message.innerHTML = this.toplay;
+    let container = document.getElementById(this.containerId);
+    container.appendChild(this.message);
+  }
+
+  getRandomPiece(color) {
+    // Find pieces which can move and roll a (biased) dice
+    let canMove = [];
+    for (let i=0; i<8; i++) {
+      for (let j=0; j<8; j++) {
+        if (this.board[i][j] != "" && this.getColor(i, j) == color) {
+          const piece = this.getPiece(i, j);
+          if (this.findDestSquares([i, j], {one: true}))
+            canMove.push(piece);
+        }
+      }
+    }
+    if (!this.options["biased"])
+      canMove = [...new Set(canMove)];
+    return canMove[Random.randInt(canMove.length)];
+  }
+
+  postProcessPotentialMoves(moves) {
+    return super.postProcessPotentialMoves(moves).filter(m => {
+      return (
+        (m.appear.length >= 1 && m.appear[0].p == this.toplay) ||
+        (m.vanish.length >= 1 && m.vanish[0].p == this.toplay)
+      );
+    });
+  }
+
+  filterValid(moves) {
+    return moves;
+  }
+
+  playReceivedMove(moves, callback) {
+    this.toplay = moves[0].toplay; //only one move
+    super.displayMessage(this.message, this.toplay);
+    super.playReceivedMove(moves, callback);
+  }
+
+};
diff --git a/variants/Dice/rules.html b/variants/Dice/rules.html
new file mode 100644
index 0000000..04e8682
--- /dev/null
+++ b/variants/Dice/rules.html
@@ -0,0 +1,3 @@
+<p>Play the piece type determined by a dice roll.</p>
+
+<p>There is no check or checkmate: the goal is to capture the king.</p>
diff --git a/variants/Dice/style.css b/variants/Dice/style.css
new file mode 100644
index 0000000..5881bc3
--- /dev/null
+++ b/variants/Dice/style.css
@@ -0,0 +1,12 @@
+@import url("/base_pieces.css");
+
+div.piece-text {
+  position: relative;
+  margin-top: 15px;
+  width: 100%;
+  text-align: center;
+  background-color: transparent;
+  color: darkred;
+  font-weight: bold;
+  font-size: 2em;
+}
-- 
2.44.0