From 1af00c56319683edd9215457b2c84840215399ab Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Sat, 22 Feb 2020 11:48:08 +0100
Subject: [PATCH] Fix Benedict rules

---
 client/src/base_rules.js                      |  1 -
 client/src/translations/rules/Benedict/en.pug |  4 +-
 client/src/translations/rules/Benedict/es.pug |  4 +-
 client/src/translations/rules/Benedict/fr.pug |  4 +-
 client/src/variants/Benedict.js               | 62 +++++++++++++------
 5 files changed, 50 insertions(+), 25 deletions(-)

diff --git a/client/src/base_rules.js b/client/src/base_rules.js
index 656f7019..5bf47f76 100644
--- a/client/src/base_rules.js
+++ b/client/src/base_rules.js
@@ -1055,7 +1055,6 @@ export const ChessRules = class ChessRules {
   // What is the score ? (Interesting if game is over)
   getCurrentScore() {
     if (this.atLeastOneMove())
-      // game not over
       return "*";
 
     // Game over
diff --git a/client/src/translations/rules/Benedict/en.pug b/client/src/translations/rules/Benedict/en.pug
index 805dbf4c..9f6da6fc 100644
--- a/client/src/translations/rules/Benedict/en.pug
+++ b/client/src/translations/rules/Benedict/en.pug
@@ -14,8 +14,8 @@ figure.diagram-container
 h3 End of the game
 
 p.
-  The game ends when a king changes color.
-  There can be no stalemate since all pieces remain on the board.
+  The game ends when a king changes color,
+  or when a side cannot make a move (stalemate, draw).
 
 p.
   In the diagram position, 2...g6?? for example would allow 3.Nf6 which
diff --git a/client/src/translations/rules/Benedict/es.pug b/client/src/translations/rules/Benedict/es.pug
index 30a7b743..439d5da1 100644
--- a/client/src/translations/rules/Benedict/es.pug
+++ b/client/src/translations/rules/Benedict/es.pug
@@ -10,8 +10,8 @@ figure.diagram-container
 h3 Fin de la partida
 
 p.
-  El juego termina cuando un rey cambia de color.
-  No puede hay empate ya que todas las piezas permanecen en el tablero.
+  El juego termina cuando un rey cambia de color,
+  o cuando un lado ya no tiene un movimiento legal (pat, tablas).
 
 p.
   En la posición del diagrama, 2...g6?? por ejemplo permitiría 3.Nf6 que
diff --git a/client/src/translations/rules/Benedict/fr.pug b/client/src/translations/rules/Benedict/fr.pug
index 6625912b..542a1898 100644
--- a/client/src/translations/rules/Benedict/fr.pug
+++ b/client/src/translations/rules/Benedict/fr.pug
@@ -10,8 +10,8 @@ figure.diagram-container
 h3 Fin de la partie
 
 p.
-  La partie s'achève quand un roi change de couleur.
-  Il ne peut pas y avoir de pat puisque toutes les pièces restent sur l'échiquier.
+  La partie s'achève quand un roi change de couleur,
+  ou qu'un camp ne dispose plus de coups légaux (pat, match nul).
 
 p.
   Dans la position du diagrame, 2...g6?? par exemple permettrait 3.Nf6 qui
diff --git a/client/src/variants/Benedict.js b/client/src/variants/Benedict.js
index e3e55eb6..fbeef895 100644
--- a/client/src/variants/Benedict.js
+++ b/client/src/variants/Benedict.js
@@ -140,27 +140,34 @@ export const VariantRules = class BenedictRules extends ChessRules {
     // Get all moves from x,y without captures:
     let moves = super.getPotentialMovesFrom([x, y]);
     // Add flips:
+    let newAppear = [];
+    let newVanish = [];
     moves.forEach(m => {
       V.PlayOnBoard(this.board, m);
-      const flipped = this.findCaptures([m.end.x, m.end.y]);
-      V.UndoOnBoard(this.board, m);
-      flipped.forEach(sq => {
-        const piece = this.getPiece(sq[0],sq[1]);
-        const pipoA = new PiPo({
-          x:sq[0],
-          y:sq[1],
-          c:color,
-          p:piece
-        });
-        const pipoV = new PiPo({
-          x:sq[0],
-          y:sq[1],
-          c:oppCol,
-          p:piece
+      // If castling, m.appear has 2 elements:
+      m.appear.forEach(a => {
+        const flipped = this.findCaptures([a.x, a.y]);
+        flipped.forEach(sq => {
+          const piece = this.getPiece(sq[0],sq[1]);
+          const pipoA = new PiPo({
+            x:sq[0],
+            y:sq[1],
+            c:color,
+            p:piece
+          });
+          const pipoV = new PiPo({
+            x:sq[0],
+            y:sq[1],
+            c:oppCol,
+            p:piece
+          });
+          newAppear.push(pipoA);
+          newVanish.push(pipoV);
         });
-        m.appear.push(pipoA);
-        m.vanish.push(pipoV);
+        Array.prototype.push.apply(m.appear, newAppear);
+        Array.prototype.push.apply(m.vanish, newVanish);
       });
+      V.UndoOnBoard(this.board, m);
     });
     return moves;
   }
@@ -175,12 +182,31 @@ export const VariantRules = class BenedictRules extends ChessRules {
     return [];
   }
 
+  // Stop at the first move found
+  atLeastOneMove() {
+    const color = this.turn;
+    const oppCol = V.GetOppCol(color);
+    for (let i = 0; i < V.size.x; i++) {
+      for (let j = 0; j < V.size.y; j++) {
+        if (this.board[i][j] != V.EMPTY && this.getColor(i, j) != oppCol) {
+          const moves = this.getPotentialMovesFrom([i, j]);
+          if (moves.length > 0)
+            return true;
+        }
+      }
+    }
+    return false;
+  }
+
   getCurrentScore() {
     const color = this.turn;
     // Did a king change color?
     const kp = this.kingPos[color];
     if (this.getColor(kp[0], kp[1]) != color)
       return color == "w" ? "0-1" : "1-0";
-    return "*";
+    if (this.atLeastOneMove())
+      return "*";
+    // Stalemate:
+    return "1/2";
   }
 };
-- 
2.44.0