From c3ff3a0c807d97c0311a06491318fe02440266db Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Mon, 20 Jul 2020 02:07:42 +0200
Subject: [PATCH] Fix Progressive2. Fixing attempt on Doublemove1

---
 client/src/variants/Doublemove1.js  | 34 +++++++++++++++++++++++++----
 client/src/variants/Progressive1.js |  2 +-
 client/src/variants/Progressive2.js |  1 -
 3 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/client/src/variants/Doublemove1.js b/client/src/variants/Doublemove1.js
index 6999eb40..95d94d7a 100644
--- a/client/src/variants/Doublemove1.js
+++ b/client/src/variants/Doublemove1.js
@@ -76,11 +76,38 @@ export class Doublemove1Rules extends ChessRules {
     return moves;
   }
 
+  atLeastOneMove() {
+    const color = this.turn;
+    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) == color) {
+          const moves = this.getPotentialMovesFrom([i, j]);
+          if (moves.length > 0) {
+            for (let k = 0; k < moves.length; k++) {
+              const m = moves[k];
+              // NOTE: not using play() here (=> infinite loop)
+              V.PlayOnBoard(this.board, m);
+              if (m.vanish[0].p == V.KING)
+                this.kingPos[color] = [m.appear[0].x, m.appear[0].y];
+              const res = !this.underCheck(color);
+              V.UndoOnBoard(this.board, m);
+              if (m.vanish[0].p == V.KING)
+                this.kingPos[color] = [m.start.x, m.start.y];
+              if (res) return true;
+            }
+          }
+        }
+      }
+    }
+    return false;
+  }
+
   play(move) {
     move.flags = JSON.stringify(this.aggregateFlags());
     move.turn = [this.turn, this.subTurn];
     V.PlayOnBoard(this.board, move);
     const epSq = this.getEpSquare(move);
+    const oppCol = V.GetOppCol(this.turn);
     if (this.movesCount == 0) {
       // First move in game
       this.turn = "b";
@@ -96,14 +123,14 @@ export class Doublemove1Rules extends ChessRules {
         !this.atLeastOneMove()
       )
     ) {
-      this.turn = V.GetOppCol(this.turn);
+      this.turn = oppCol;
       this.epSquares.push([epSq]);
       move.checkOnSubturn1 = true;
       this.movesCount++;
     }
     else {
       if (this.subTurn == 2) {
-        this.turn = V.GetOppCol(this.turn);
+        this.turn = oppCol;
         let lastEpsq = this.epSquares[this.epSquares.length - 1];
         lastEpsq.push(epSq);
       }
@@ -122,8 +149,7 @@ export class Doublemove1Rules extends ChessRules {
     const firstRank = c == "w" ? V.size.x - 1 : 0;
 
     if (piece == V.KING) {
-      this.kingPos[c][0] = move.appear[0].x;
-      this.kingPos[c][1] = move.appear[0].y;
+      this.kingPos[c] = [move.appear[0].x, move.appear[0].y];
       this.castleFlags[c] = [V.size.y, V.size.y];
       return;
     }
diff --git a/client/src/variants/Progressive1.js b/client/src/variants/Progressive1.js
index 4f69ee90..3023b729 100644
--- a/client/src/variants/Progressive1.js
+++ b/client/src/variants/Progressive1.js
@@ -28,7 +28,7 @@ export class Progressive1Rules extends ChessRules {
   }
 
   play(move) {
-    move.flags = JSON.stringify(this.aggregateFlags());
+    if (V.HasFlags) move.flags = JSON.stringify(this.aggregateFlags());
     const color = this.turn;
     const oppCol = V.GetOppCol(color);
     move.turn = [color, this.subTurn];
diff --git a/client/src/variants/Progressive2.js b/client/src/variants/Progressive2.js
index ffc22bc9..cb9911d3 100644
--- a/client/src/variants/Progressive2.js
+++ b/client/src/variants/Progressive2.js
@@ -26,7 +26,6 @@ export class Progressive2Rules extends Progressive1Rules {
   }
 
   undo(move) {
-    this.disaggregateFlags(JSON.parse(move.flags));
     V.UndoOnBoard(this.board, move);
     if (this.turn != move.turn[0]) this.movesCount--;
     this.turn = move.turn[0];
-- 
2.44.0