From cb1165b46266a73b2a1f211a9c98470b1ceaacd3 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Sat, 9 Jan 2021 10:40:44 +0100
Subject: [PATCH] Fix Synchrone2

---
 client/src/base_rules.js             |  2 +-
 client/src/translations/about/en.pug |  2 ++
 client/src/translations/about/es.pug |  2 ++
 client/src/translations/about/fr.pug |  2 ++
 client/src/variants/Atomic1.js       |  3 ++-
 client/src/variants/Synchrone1.js    | 20 ++++++++++++--------
 client/src/variants/Synchrone2.js    | 28 ++++++++++++++++++++++------
 7 files changed, 43 insertions(+), 16 deletions(-)

diff --git a/client/src/base_rules.js b/client/src/base_rules.js
index e9ea7bc1..6502679a 100644
--- a/client/src/base_rules.js
+++ b/client/src/base_rules.js
@@ -163,6 +163,7 @@ export const ChessRules = class ChessRules {
 
   // Check if FEN describes a board situation correctly
   static IsGoodFen(fen) {
+console.log("ddd");
     const fenParsed = V.ParseFen(fen);
     // 1) Check position
     if (!V.IsGoodPosition(fenParsed.position)) return false;
@@ -533,7 +534,6 @@ export const ChessRules = class ChessRules {
     // Squares of white and black king:
     this.kingPos = { w: [-1, -1], b: [-1, -1] };
     const fenRows = V.ParseFen(fen).position.split("/");
-    const startRow = { 'w': V.size.x - 1, 'b': 0 };
     for (let i = 0; i < fenRows.length; i++) {
       let k = 0; //column index on board
       for (let j = 0; j < fenRows[i].length; j++) {
diff --git a/client/src/translations/about/en.pug b/client/src/translations/about/en.pug
index a64d163e..cfd3ca9f 100644
--- a/client/src/translations/about/en.pug
+++ b/client/src/translations/about/en.pug
@@ -56,5 +56,7 @@ h3 Related links
   a(href="http://www.zillions-of-games.com/") zillions-of-games.com
   a(href="https://en.wikipedia.org/wiki/Fairy_chess_piece") Fairy chess pieces
   a(href="http://www.pion.ch/echecs/liste_variantes.php") pion.ch
+  a(href="https://www.jatektan.hu/_2018_vissza/2011_ig/uj2001/isakk1.html")
+    | List of variants
   a(href="http://abrobecker.free.fr/chess/fairyblitz.htm") fairyblitz.htm
   a(href="https://chessvariants.training/") chessvariants.training
diff --git a/client/src/translations/about/es.pug b/client/src/translations/about/es.pug
index 553d4a4d..3805605f 100644
--- a/client/src/translations/about/es.pug
+++ b/client/src/translations/about/es.pug
@@ -56,5 +56,7 @@ h3 Enlaces relacionados
   a(href="https://en.wikipedia.org/wiki/Fairy_chess_piece")
     | Piezas de ajedrez magicas
   a(href="http://www.pion.ch/echecs/liste_variantes.php") pion.ch
+  a(href="https://www.jatektan.hu/_2018_vissza/2011_ig/uj2001/isakk1.html")
+    | Listado de variantes
   a(href="http://abrobecker.free.fr/chess/fairyblitz.htm") fairyblitz.htm
   a(href="https://chessvariants.training/") chessvariants.training
diff --git a/client/src/translations/about/fr.pug b/client/src/translations/about/fr.pug
index 51d34f70..39e22b44 100644
--- a/client/src/translations/about/fr.pug
+++ b/client/src/translations/about/fr.pug
@@ -57,5 +57,7 @@ h3 Liens connexes
   a(href="https://en.wikipedia.org/wiki/Fairy_chess_piece")
     | Pièces d'échecs féériques
   a(href="http://www.pion.ch/echecs/liste_variantes.php") pion.ch
+  a(href="https://www.jatektan.hu/_2018_vissza/2011_ig/uj2001/isakk1.html")
+    | Liste de variantes
   a(href="http://abrobecker.free.fr/chess/fairyblitz.htm") fairyblitz.htm
   a(href="https://chessvariants.training/") chessvariants.training
diff --git a/client/src/variants/Atomic1.js b/client/src/variants/Atomic1.js
index 27769a7e..c44c2cac 100644
--- a/client/src/variants/Atomic1.js
+++ b/client/src/variants/Atomic1.js
@@ -91,7 +91,8 @@ export class Atomic1Rules extends ChessRules {
         ) {
           this.kingPos[c] = [-1, -1];
           this.castleFlags[c] = [8, 8];
-        } else {
+        }
+        else {
           // Now check if init rook(s) exploded
           if (Math.abs(move.end.x - firstRank[c]) <= 1) {
             if (Math.abs(move.end.y - this.castleFlags[c][0]) <= 1)
diff --git a/client/src/variants/Synchrone1.js b/client/src/variants/Synchrone1.js
index 2c59bc6c..7410d278 100644
--- a/client/src/variants/Synchrone1.js
+++ b/client/src/variants/Synchrone1.js
@@ -363,9 +363,11 @@ export class Synchrone1Rules extends ChessRules {
     return smove;
   }
 
-  play(move) {
-    move.flags = JSON.stringify(this.aggregateFlags()); //save flags (for undo)
-    this.epSquares.push(this.getEpSquare(move));
+  play(move, noFlag) {
+    if (!noFlag) {
+      move.flags = JSON.stringify(this.aggregateFlags());
+      this.epSquares.push(this.getEpSquare(move));
+    }
     // Do not play on board (would reveal the move...)
     this.turn = V.GetOppCol(this.turn);
     this.movesCount++;
@@ -421,9 +423,11 @@ export class Synchrone1Rules extends ChessRules {
     move.smove = smove;
   }
 
-  undo(move) {
-    this.epSquares.pop();
-    this.disaggregateFlags(JSON.parse(move.flags));
+  undo(move, noFlag) {
+    if (!noFlag) {
+      this.epSquares.pop();
+      this.disaggregateFlags(JSON.parse(move.flags));
+    }
     if (this.turn == 'w')
       // Back to the middle of the move
       V.UndoOnBoard(this.board, move.smove);
@@ -447,14 +451,14 @@ export class Synchrone1Rules extends ChessRules {
     if (color == 'b') {
       // kingPos must be reset for appropriate highlighting:
       var lastMove = JSON.parse(JSON.stringify(this.whiteMove));
-      this.undo(lastMove); //will erase whiteMove, thus saved above
+      this.undo(lastMove, "noFlag"); //will erase whiteMove, thus saved above
     }
     let res = [];
     if (this.kingPos['w'][0] >= 0 && this.underCheck('w'))
       res.push(JSON.parse(JSON.stringify(this.kingPos['w'])));
     if (this.kingPos['b'][0] >= 0 && this.underCheck('b'))
       res.push(JSON.parse(JSON.stringify(this.kingPos['b'])));
-    if (color == 'b') this.play(lastMove);
+    if (color == 'b') this.play(lastMove, "noFlag");
     return res;
   }
 
diff --git a/client/src/variants/Synchrone2.js b/client/src/variants/Synchrone2.js
index 5539c9e0..af77a8c0 100644
--- a/client/src/variants/Synchrone2.js
+++ b/client/src/variants/Synchrone2.js
@@ -93,7 +93,7 @@ export class Synchrone2Rules extends Synchrone1Rules {
     this.board = initBoard;
     const movesInit = super.getPotentialMovesFrom([x, y]);
     this.board = saveBoard;
-    const target = appeared.find(a => a.c == oppCol);
+    const target = appeared.find(a => a.c == oppCol) || { x: -1, y: -1 };
     let movesNow = super.getPotentialMovesFrom([x, y]).filter(m => {
       return (
         m.end.x == target.x &&
@@ -102,7 +102,7 @@ export class Synchrone2Rules extends Synchrone1Rules {
       );
     });
     const passTarget =
-      (x != this.kingPos[c][0] || y != this.kingPos[c][1]) ? c : oppCol;
+      (x != this.kingPos[c][0] || y != this.kingPos[c][0]) ? c : oppCol;
     movesNow.push(
       new Move({
         start: { x: x, y: y },
@@ -141,9 +141,19 @@ export class Synchrone2Rules extends Synchrone1Rules {
     return this.filterValid(this.getPotentialMovesFrom([x, y]));
   }
 
-  play(move) {
+  getAllValidMoves() {
+    const moves = this.filterValid(super.getAllPotentialMoves());
+    if (this.movesCount % 4 <= 1) return moves;
+    const emptyIdx = moves.findIndex(m => m.vanish.length == 0);
+    if (emptyIdx >= 0)
+      // Keep only one pass move (for computer play)
+      return moves.filter((m, i) => m.vanish.length > 0 || i == emptyIdx);
+    return moves;
+  }
+
+  play(move, noFlag) {
     if (this.movesCount % 4 == 0) this.initfenStack.push(this.getBaseFen());
-    move.flags = JSON.stringify(this.aggregateFlags());
+    if (!noFlag) move.flags = JSON.stringify(this.aggregateFlags());
     // Do not play on board (would reveal the move...)
     this.turn = V.GetOppCol(this.turn);
     this.movesCount++;
@@ -191,8 +201,8 @@ export class Synchrone2Rules extends Synchrone1Rules {
     move.smove = smove;
   }
 
-  undo(move) {
-    this.disaggregateFlags(JSON.parse(move.flags));
+  undo(move, noFlag) {
+    if (!noFlag) this.disaggregateFlags(JSON.parse(move.flags));
     if (this.turn == 'w')
       // Back to the middle of the move
       V.UndoOnBoard(this.board, move.smove);
@@ -236,6 +246,12 @@ export class Synchrone2Rules extends Synchrone1Rules {
     return (whiteCanMove ? "1-0" : "0-1");
   }
 
+  getComputerMove() {
+    if (this.movesCount % 4 <= 1) return super.getComputerMove();
+    const moves = this.getAllValidMoves();
+    return moves[randInt(moves.length)];
+  }
+
   getNotation(move) {
     if (move.vanish.length == 0) return "pass";
     return super.getNotation(move);
-- 
2.44.0