Add unambiguous section in the PGN + some fixes + code formatting and fix typos
[vchess.git] / client / src / variants / Apocalypse.js
index b84fb5b..abcfd70 100644 (file)
@@ -22,7 +22,7 @@ export class ApocalypseRules extends ChessRules {
   }
 
   static get CanAnalyze() {
-    return true; //false;
+    return false;
   }
 
   static get ShowMoves() {
@@ -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;
     }
@@ -146,7 +146,8 @@ export class ApocalypseRules extends ChessRules {
       start: this.whiteMove.start,
       end: this.whiteMove.end,
       appear: this.whiteMove.appear,
-      vanish: this.whiteMove.vanish
+      vanish: this.whiteMove.vanish,
+      illegal: this.whiteMove.illegal
     });
   }
 
@@ -321,7 +322,7 @@ export class ApocalypseRules extends ChessRules {
       vanish: []
     };
     if (!m1 && !m2) return smove;
-    // Both move are now legal:
+    // Both moves are now legal or at least possible:
     smove.vanish.push(m1.vanish[0]);
     smove.vanish.push(m2.vanish[0]);
     if ((m1.end.x != m2.end.x) || (m1.end.y != m2.end.y)) {
@@ -376,10 +377,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);
@@ -409,10 +406,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) {
@@ -461,30 +454,42 @@ 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:
-    // try to capture something (not re-capturing)
+    let validMoves = [];
+    let illegalMoves = [];
     moves.forEach(m => {
-      V.PlayOnBoard(this.board, m);
-      m.eval = this.evalPosition();
-      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) {
     // Basic system: piece + init + dest square
     return (
-      move.vanish[0].p.toUpperCase() +
+      (move.vanish[0].p == V.KNIGHT ? "N" : "") +
       V.CoordsToSquare(move.start) +
       V.CoordsToSquare(move.end)
     );