From 3b98a861b893f0dc8e125c6f4a68faeb075ed56e Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Wed, 24 Mar 2021 11:36:28 +0100
Subject: [PATCH] Pandemonium: promotion on last 2 ranks

---
 client/src/base_rules.js                      |  3 +--
 .../src/translations/rules/Pandemonium/en.pug |  2 +-
 .../src/translations/rules/Pandemonium/es.pug |  2 +-
 .../src/translations/rules/Pandemonium/fr.pug |  2 +-
 client/src/variants/Pandemonium.js            | 27 ++++++++++++-------
 5 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/client/src/base_rules.js b/client/src/base_rules.js
index 5cf1d97e..f7a2fee1 100644
--- a/client/src/base_rules.js
+++ b/client/src/base_rules.js
@@ -769,9 +769,8 @@ export const ChessRules = class ChessRules {
       if (!!promotions) finalPieces = promotions;
       else if (!!V.PawnSpecs.promotions) finalPieces = V.PawnSpecs.promotions;
     }
-    let tr = null;
     for (let piece of finalPieces) {
-      tr = (piece != V.PAWN ? { c: color, p: piece } : null);
+      const tr = (piece != V.PAWN ? { c: color, p: piece } : null);
       moves.push(this.getBasicMove([x1, y1], [x2, y2], tr));
     }
   }
diff --git a/client/src/translations/rules/Pandemonium/en.pug b/client/src/translations/rules/Pandemonium/en.pug
index 1c315cb3..1a0ebdfe 100644
--- a/client/src/translations/rules/Pandemonium/en.pug
+++ b/client/src/translations/rules/Pandemonium/en.pug
@@ -29,7 +29,7 @@ ul
   li Marshal promotes into Queen (called "Apricot" here).
 p.
   All these promotions are optional. They are available after a move ending
-  at or starting from the last rank.
+  at or starting from the last two ranks.
 
 p.
   Each captured piece is first returned to its unpromoted version (if
diff --git a/client/src/translations/rules/Pandemonium/es.pug b/client/src/translations/rules/Pandemonium/es.pug
index 56ab473e..c66055f6 100644
--- a/client/src/translations/rules/Pandemonium/es.pug
+++ b/client/src/translations/rules/Pandemonium/es.pug
@@ -30,7 +30,7 @@ ul
   li Mariscal ascendió a Dama (aquí se llama "Apricot").
 p.
   Todas estas promociones son opcionales. Están disponibles después de un
-  movimiento que termina o comienza en la última fila.
+  movimiento que termina o comienza en las dos últimas filas.
 
 p.
   Cada pieza capturada se devuelve primero a su forma no promocionada (el caso
diff --git a/client/src/translations/rules/Pandemonium/fr.pug b/client/src/translations/rules/Pandemonium/fr.pug
index 63de846c..6772a224 100644
--- a/client/src/translations/rules/Pandemonium/fr.pug
+++ b/client/src/translations/rules/Pandemonium/fr.pug
@@ -30,7 +30,7 @@ ul
   li Maréchal promu en Dame (appelée "Apricot" ici).
 p.
   Toutes ces promotions sont optionnelles. Elles sont disponibles après un
-  coup terminant ou commençant sur la dernière rangée.
+  coup terminant ou commençant sur les deux dernières rangées.
 
 p.
   Chaque pièce capturée est d'abord ramenée à sa forme non promue (le cas
diff --git a/client/src/variants/Pandemonium.js b/client/src/variants/Pandemonium.js
index e3b8c191..4894ec16 100644
--- a/client/src/variants/Pandemonium.js
+++ b/client/src/variants/Pandemonium.js
@@ -4,14 +4,6 @@ import { ArrayFun } from "@/utils/array";
 
 export class PandemoniumRules extends ChessRules {
 
-  static get PawnSpecs() {
-    return Object.assign(
-      {},
-      ChessRules.PawnSpecs,
-      { promotions: [V.GILDING] }
-    );
-  }
-
   loseOnRepetition() {
     // If current side is under check: lost
     return this.underCheck(this.turn);
@@ -414,10 +406,10 @@ export class PandemoniumRules extends ChessRules {
     // Maybe apply promotions:
     if (Object.keys(V.PromoteMap).includes(p)) {
       const promoted = V.PromoteMap[p];
-      const lastRank = (c == 'w' ? 0 : 9);
+      const lastRanks = (c == 'w' ? [0, 1] : [9, 8]);
       let promotions = [];
       moves.forEach(m => {
-        if (m.start.x == lastRank || m.end.x == lastRank) {
+        if (lastRanks.includes(m.start.x) || lastRanks.includes(m.end.x)) {
           let pMove = JSON.parse(JSON.stringify(m));
           pMove.appear[0].p = promoted;
           promotions.push(pMove);
@@ -428,6 +420,21 @@ export class PandemoniumRules extends ChessRules {
     return moves;
   }
 
+  addPawnMoves([x1, y1], [x2, y2], moves) {
+    const color = this.turn;
+    const lastRanks = (color == "w" ? [0, 1] : [9, 8]);
+    if (!lastRanks.includes(x2)) {
+      moves.push(this.getBasicMove([x1, y1], [x2, y2]));
+      return;
+    }
+    let finalPieces = [V.GILDING];
+    if (x2 == lastRanks[1]) finalPieces.push(V.PAWN);
+    for (let piece of finalPieces) {
+      const tr = (piece != V.PAWN ? { c: color, p: piece } : null);
+      moves.push(this.getBasicMove([x1, y1], [x2, y2], tr));
+    }
+  }
+
   getPotentialPawnMoves([x, y]) {
     const color = this.turn;
     const shiftX = (color == 'w' ? -1 : 1);
-- 
2.44.0