Fix Progressive2. Fixing attempt on Doublemove1
authorBenjamin Auder <benjamin.auder@somewhere>
Mon, 20 Jul 2020 00:07:42 +0000 (02:07 +0200)
committerBenjamin Auder <benjamin.auder@somewhere>
Mon, 20 Jul 2020 00:07:42 +0000 (02:07 +0200)
client/src/variants/Doublemove1.js
client/src/variants/Progressive1.js
client/src/variants/Progressive2.js

index 6999eb4..95d94d7 100644 (file)
@@ -76,11 +76,38 @@ export class Doublemove1Rules extends ChessRules {
     return moves;
   }
 
     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);
   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";
     if (this.movesCount == 0) {
       // First move in game
       this.turn = "b";
@@ -96,14 +123,14 @@ export class Doublemove1Rules extends ChessRules {
         !this.atLeastOneMove()
       )
     ) {
         !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.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);
       }
         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) {
     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;
     }
       this.castleFlags[c] = [V.size.y, V.size.y];
       return;
     }
index 4f69ee9..3023b72 100644 (file)
@@ -28,7 +28,7 @@ export class Progressive1Rules extends ChessRules {
   }
 
   play(move) {
   }
 
   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];
     const color = this.turn;
     const oppCol = V.GetOppCol(color);
     move.turn = [color, this.subTurn];
index ffc22bc..cb9911d 100644 (file)
@@ -26,7 +26,6 @@ export class Progressive2Rules extends Progressive1Rules {
   }
 
   undo(move) {
   }
 
   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];
     V.UndoOnBoard(this.board, move);
     if (this.turn != move.turn[0]) this.movesCount--;
     this.turn = move.turn[0];