Add Checkered1 + fix last move highlights
[vchess.git] / client / src / variants / Apocalypse.js
index 1eb482e..4587be3 100644 (file)
@@ -69,12 +69,12 @@ export class ApocalypseRules extends ChessRules {
     // 4) Check whiteMove
     if (
       (
-        fenParsed.turn == "w" &&
+        fenParsed.turn == "b" &&
         // NOTE: do not check really JSON stringified move...
         (!fenParsed.whiteMove || fenParsed.whiteMove == "-")
       )
       ||
-      (fenParsed.turn == "b" && fenParsed.whiteMove != "-")
+      (fenParsed.turn == "w" && fenParsed.whiteMove != "-")
     ) {
       return false;
     }
@@ -274,29 +274,44 @@ export class ApocalypseRules extends ChessRules {
       return (
         (
           m.vanish[0].p == V.KNIGHT &&
-          (m.vanish.length == 1 || m.vanish[1].c != m.vanish[0].c)
-        )
-        ||
-        (
-          // Promotion attempt
-          m.end.x == (m.vanish[0].c == "w" ? 0 : V.size.x - 1) &&
-          other.vanish.length == 2 &&
-          other.vanish[1].p == V.KNIGHT &&
-          other.vanish[1].c == m.vanish[0].c
-        )
-        ||
-        (
-          // Moving attempt
-          !movingLikeCapture(m) &&
-          other.start.x == m.end.x &&
-          other.start.y == m.end.y
+          (
+            m.vanish.length == 1 ||
+            m.vanish[1].c != m.vanish[0].c ||
+            // Self-capture attempt
+            (
+              !other.illegal &&
+              other.end.x == m.end.x &&
+              other.end.y == m.end.y
+            )
+          )
         )
         ||
         (
-          // Capture attempt
-          movingLikeCapture(m) &&
-          other.end.x == m.end.x &&
-          other.end.y == m.end.y
+          m.vanish[0].p == V.PAWN &&
+          !other.illegal &&
+          (
+            (
+              // Promotion attempt
+              m.end.x == (m.vanish[0].c == "w" ? 0 : V.size.x - 1) &&
+              other.vanish.length == 2 &&
+              other.vanish[1].p == V.KNIGHT &&
+              other.vanish[1].c == m.vanish[0].c
+            )
+            ||
+            (
+              // Moving attempt
+              !movingLikeCapture(m) &&
+              other.start.x == m.end.x &&
+              other.start.y == m.end.y
+            )
+            ||
+            (
+              // Capture attempt
+              movingLikeCapture(m) &&
+              other.end.x == m.end.x &&
+              other.end.y == m.end.y
+            )
+          )
         )
       );
     };
@@ -377,10 +392,6 @@ export class ApocalypseRules extends ChessRules {
   }
 
   play(move) {
-    if (!this.states) this.states = [];
-    const stateFen = this.getFen();
-    this.states.push(stateFen);
-
     // Do not play on board (would reveal the move...)
     move.flags = JSON.stringify(this.aggregateFlags());
     this.turn = V.GetOppCol(this.turn);
@@ -410,10 +421,6 @@ export class ApocalypseRules extends ChessRules {
     this.turn = V.GetOppCol(this.turn);
     this.movesCount--;
     this.postUndo(move);
-
-    const stateFen = this.getFen();
-    if (stateFen != this.states[this.states.length-1]) debugger;
-    this.states.pop();
   }
 
   postUndo(move) {
@@ -421,7 +428,7 @@ export class ApocalypseRules extends ChessRules {
     else this.whiteMove = move.whiteMove;
   }
 
-  getCheckSquares(color) {
+  getCheckSquares() {
     return [];
   }
 
@@ -462,31 +469,36 @@ export class ApocalypseRules extends ChessRules {
       // TODO: this situation should not happen
       return null;
 
-    if (Math.random() < 0.5)
-      // Return a random move
-      return moves[randInt(moves.length)];
-
     // Rank moves at depth 1:
+    let validMoves = [];
+    let illegalMoves = [];
     moves.forEach(m => {
-      // Warning: m.vanish[0] might refer to an empty square! Or self
-      const skipPlayUndo = (
-        m.vanish.length == 2 &&
-        (
-          m.vanish[1].c == m.vanish[0].c ||
-          this.board[m.vanish[1].x][m.vanish[1].y] == V.EMPTY
-        )
-      );
-      if (!skipPlayUndo) V.PlayOnBoard(this.board, m);
-      m.eval = this.evalPosition();
-      if (!skipPlayUndo) V.UndoOnBoard(this.board, m);
+      // Warning: m might be illegal!
+      if (!m.illegal) {
+        V.PlayOnBoard(this.board, m);
+        m.eval = this.evalPosition();
+        V.UndoOnBoard(this.board, m);
+        validMoves.push(m);
+      } else illegalMoves.push(m);
     });
-    moves.sort((a, b) => {
+
+    const illegalRatio = illegalMoves.length / moves.length;
+    if (Math.random() < illegalRatio)
+      // Return a random illegal move
+      return illegalMoves[randInt(illegalMoves.length)];
+
+    validMoves.sort((a, b) => {
       return (color == "w" ? 1 : -1) * (b.eval - a.eval);
     });
     let candidates = [0];
-    for (let i = 1; i < moves.length && moves[i].eval == moves[0].eval; i++)
+    for (
+      let i = 1;
+      i < validMoves.length && validMoves[i].eval == moves[0].eval;
+      i++
+    ) {
       candidates.push(i);
-    return moves[candidates[randInt(candidates.length)]];
+    }
+    return validMoves[candidates[randInt(candidates.length)]];
   }
 
   getNotation(move) {