Add Konane, (very) early drafts of Emergo/Fanorona/Yote/Gomoku, fix repetitions detec...
[vchess.git] / client / src / variants / Otage.js
index 98b340e..742c6ee 100644 (file)
@@ -167,6 +167,7 @@ export class OtageRules extends ChessRules {
     super.setOtherVariables(fen);
     // Stack of "last move" only for intermediate chaining
     this.lastMoveEnd = [null];
+    this.repetitions = [];
   }
 
   static IsGoodFlags(flags) {
@@ -638,8 +639,32 @@ export class OtageRules extends ChessRules {
   getCheckSquares() {
     return [];
   }
+
   filterValid(moves) {
-    return moves;
+    if (moves.length == 0) return [];
+    return moves.filter(m => {
+      if (!m.end.released) return true;
+      // Check for repetitions:
+      V.PlayOnBoard(this.board, m);
+      const newState = {
+        piece: m.end.released,
+        square: { x: m.end.x, y: m.end.y },
+        position: this.getBaseFen()
+      };
+      const repet =
+        this.repetitions.some(r => {
+          return (
+            r.piece == newState.piece &&
+            (
+              r.square.x == newState.square.x &&
+              r.square.y == newState.square.y &&
+            ) &&
+            r.position == newState.position
+          );
+        });
+      V.UndoOnBoard(this.board, m);
+      return !repet;
+    });
   }
 
   updateCastleFlags(move, piece) {
@@ -702,6 +727,16 @@ export class OtageRules extends ChessRules {
     else
       this.lastMoveEnd.push(Object.assign({ p: move.end.released }, move.end));
     V.PlayOnBoard(this.board, move);
+    if (!move.end.released) this.repetitions = [];
+    else {
+      this.repetitions.push(
+        {
+          piece: move.end.released,
+          square: { x: move.end.x, y: move.end.y },
+          position: this.getBaseFen()
+        }
+      );
+    }
   }
 
   undo(move) {
@@ -713,6 +748,7 @@ export class OtageRules extends ChessRules {
       this.turn = V.GetOppCol(this.turn);
       this.movesCount--;
     }
+    if (!!move.end.released) this.repetitions.pop();
     this.postUndo(move);
   }
 
@@ -761,6 +797,7 @@ export class OtageRules extends ChessRules {
       for (let i = mvArray.length - 1; i >= 0; i--) this.undo(mvArray[i]);
       if (!mv.end.released) return (mvArray.length > 1 ? mvArray : mvArray[0]);
     }
+    return null; //never reached
   }
 
   // NOTE: evalPosition() is wrong, but unused since bot plays at random
@@ -798,8 +835,13 @@ export class OtageRules extends ChessRules {
     // Add potential promotion indications:
     const firstLastRank = (c == 'w' ? [7, 0] : [0, 7]);
     if (move.end.x == firstLastRank[1] && piece == V.PAWN) {
-      const up = this.getUnionPieces(move.appear[0].c, move.appear[0].p);
-      notation += "=" + up[c].toUpperCase();
+      notation += "=";
+      if (ChessRules.PIECES.includes(move.appear[0].p))
+        notation += move.appear[0].p.toUpperCase();
+      else {
+        const up = this.getUnionPieces(move.appear[0].c, move.appear[0].p);
+        notation += up[c].toUpperCase();
+      }
     }
     else if (
       move.end.x == firstLastRank[0] &&