From 0ada7ac59ce418a2f03c559ad4d495407bdc47ce Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Mon, 4 May 2020 00:32:53 +0200
Subject: [PATCH] Fix Dice chess

---
 client/src/translations/rules/Dice/en.pug |  4 +++
 client/src/translations/rules/Dice/es.pug |  4 +++
 client/src/translations/rules/Dice/fr.pug |  4 +++
 client/src/variants/Dice.js               | 37 +++++++++++++++++++----
 4 files changed, 43 insertions(+), 6 deletions(-)

diff --git a/client/src/translations/rules/Dice/en.pug b/client/src/translations/rules/Dice/en.pug
index 3fc2354a..91be2d1a 100644
--- a/client/src/translations/rules/Dice/en.pug
+++ b/client/src/translations/rules/Dice/en.pug
@@ -13,6 +13,10 @@ figure.diagram-container
     | fen:r1b2b1r/pp2p3/2p2q2/3p1kpp/1P2Qp2/2K2P1P/P1PP2P1/RNB2BNR:
   figcaption Both kings could be captured if the dice indicated a queen.
 
+p.
+  Note: a pawn promotion into a piece type 'T' is a valid move
+  if the dice shows 'T'.
+
 p.
   Games are likely to be very random with this constraint.
   Play at your own risk :)
diff --git a/client/src/translations/rules/Dice/es.pug b/client/src/translations/rules/Dice/es.pug
index 4a08a928..1799c531 100644
--- a/client/src/translations/rules/Dice/es.pug
+++ b/client/src/translations/rules/Dice/es.pug
@@ -14,6 +14,10 @@ figure.diagram-container
   figcaption.
     Los dos reyes podrían ser capturados si el dado indica una dama.
 
+p.
+  Nota: una promoción de peón en una pieza de tipo 'T' es un movimiento
+  válido si el dado indica 'T'.
+
 p.
   Es probable que los juegos sean muy aleatorios con esta restricción.
   Juega bajo tu propio riesgo :)
diff --git a/client/src/translations/rules/Dice/fr.pug b/client/src/translations/rules/Dice/fr.pug
index 42ee7ca1..a5d852f5 100644
--- a/client/src/translations/rules/Dice/fr.pug
+++ b/client/src/translations/rules/Dice/fr.pug
@@ -14,6 +14,10 @@ figure.diagram-container
   figcaption.
     Les deux rois pourraient être capturés si le dé indiquait une dame.
 
+p.
+  Note : une promotion de pion en une pièce de type 'T' est un coup valide
+  si le dé indique 'T'.
+
 p.
   Les parties sont susceptibles d'être très aléatoires avec cette contrainte.
   Jouez à vos risques et périls :)
diff --git a/client/src/variants/Dice.js b/client/src/variants/Dice.js
index a8ecf1b9..68d80bb4 100644
--- a/client/src/variants/Dice.js
+++ b/client/src/variants/Dice.js
@@ -3,7 +3,7 @@ import { randInt } from "@/utils/alea";
 
 export class DiceRules extends ChessRules {
   static get CanAnalyze() {
-    return true;//false;
+    return false;
   }
 
   doClick(square) {
@@ -19,14 +19,39 @@ export class DiceRules extends ChessRules {
   }
 
   getPotentialMovesFrom([x, y]) {
+    if (this.subTurn == 1) return [];
     const L = this.p2play.length;
-    if (
-      this.subTurn == 1 ||
+    const piece = this.getPiece(x, y);
+    if (piece == V.PAWN && this.p2play[L-1] != V.PAWN) {
+      // The piece must be a pawn about to promote.
+      const color = this.turn;
+      const beforeLastRank = (color == 'w' ? 1 : 0);
+      const forward = (color == 'w' ? -1 : 1);
+      let moves = [];
+      if (this.board[x + forward][y] == V.EMPTY) {
+        moves.push(
+          this.getBasicMove(
+            [x, y], [x + forward], { c: color, p: this.p2play[L-1] })
+        );
+      }
+      for (let shift of [-1, 1]) {
+        const [i, j] = [x + forward, y + shift];
+        if (
+          V.OnBoard(i, j) &&
+          this.board[i][j] != V.EMPTY &&
+          this.getColor(i, j) != color
+        ) {
+          moves.push(
+            this.getBasicMove(
+              [x, y], [i, j], { c: color, p: this.p2play[L-1] })
+          );
+        }
+      }
+      return moves;
+    }
+    if (piece != this.p2play[L-1])
       // The piece type must match last p2play
-      this.getPiece(x, y) != this.p2play[L-1]
-    ) {
       return [];
-    }
     return super.getPotentialMovesFrom([x, y]);
   }
 
-- 
2.44.0