Split Refusal into Refusal1 + Refusal2, fix endgame bug, remove castling
[vchess.git] / client / src / variants / Refusal1.js
similarity index 79%
rename from client/src/variants/Refusal.js
rename to client/src/variants/Refusal1.js
index 76d737d..4f50d75 100644 (file)
@@ -1,7 +1,11 @@
 import { ChessRules } from "@/base_rules";
 import { randInt } from "@/utils/alea";
 
-export class RefusalRules extends ChessRules {
+export class Refusal1Rules extends ChessRules {
+
+  static get HasFlags() {
+    return false;
+  }
 
   static IsGoodFen(fen) {
     if (!ChessRules.IsGoodFen(fen)) return false;
@@ -11,7 +15,7 @@ export class RefusalRules extends ChessRules {
 
   static ParseFen(fen) {
     return Object.assign(
-      { lastMove: fen.split(" ")[5] },
+      { lastMove: fen.split(" ")[4] },
       ChessRules.ParseFen(fen)
     );
   }
@@ -26,7 +30,7 @@ export class RefusalRules extends ChessRules {
   // some extra repetitions could be detected... TODO (...)
 
   static GenRandInitFen(randomness) {
-    return ChessRules.GenRandInitFen(randomness) + " null";
+    return ChessRules.GenRandInitFen(randomness).slice(0, -6)  + "- null";
   }
 
   setOtherVariables(fen) {
@@ -66,26 +70,6 @@ export class RefusalRules extends ChessRules {
     return super.getPotentialMovesFrom([x, y]);
   }
 
-  // NOTE: do not take refusal move into account here (two own moves)
-  atLeastTwoMoves() {
-    let movesCounter = 0;
-    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]);
-          for (let m of moves) {
-            if (m.vanish[0].c == color && this.filterValid([m]).length > 0) {
-              movesCounter++;
-              if (movesCounter >= 2) return true;
-            }
-          }
-        }
-      }
-    }
-    return false;
-  }
-
   filterValid(moves) {
     if (moves.length == 0) return [];
     const color = this.turn;
@@ -115,16 +99,13 @@ export class RefusalRules extends ChessRules {
   prePlay(move) {
     const L = this.lastMove.length;
     const lm = this.lastMove[L-1];
-    if (
-      // My previous move was already refused?
-      (!!lm && this.getColor(lm.end.x, lm.end.y) == this.turn) ||
-      // I've only one move available?
-      !this.atLeastTwoMoves()
-    ) {
-      move.noRef = true;
-    }
     // NOTE: refusal could be recomputed, but, it's easier like this
     if (move.vanish[0].c != this.turn) move.refusal = true;
+    move.noRef = (
+      !!move.refusal ||
+      // My previous move was already refused?
+      !!lm && this.getColor(lm.end.x, lm.end.y) == this.turn
+    );
   }
 
   getEpSquare(move) {
@@ -137,7 +118,8 @@ export class RefusalRules extends ChessRules {
     else {
       const L = this.lastMove.length;
       const lm = this.lastMove[L-1];
-      this.disaggregateFlags(JSON.parse(lm.flags));
+      if (move.appear[0].p == V.KING)
+        this.kingPos[move.appear[0].c] = [move.end.x, move.end.y];
     }
     // NOTE: explicitely give fields, because some are assigned in BaseGame
     let mvInLm = {
@@ -145,7 +127,6 @@ export class RefusalRules extends ChessRules {
       end: move.end,
       appear: move.appear,
       vanish: move.vanish,
-      flags: move.flags
     };
     if (!!move.noRef) mvInLm.noRef = true;
     if (!!move.refusal) mvInLm.refusal = true;
@@ -154,6 +135,10 @@ export class RefusalRules extends ChessRules {
 
   postUndo(move) {
     if (!move.refusal) super.postUndo(move);
+    else {
+      if (move.appear[0].p == V.KING)
+        this.kingPos[move.appear[0].c] = [move.start.x, move.start.y];
+    }
     this.lastMove.pop();
   }
 
@@ -162,16 +147,12 @@ export class RefusalRules extends ChessRules {
     const L = this.lastMove.length;
     const lm = this.lastMove[L-1];
     let potentialMoves = [];
+    if (!!lm && !lm.noRef)
+      // Add refusal move:
+      potentialMoves = this.getPotentialMovesFrom([lm.end.x, lm.end.y]);
     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 ||
-            // Add move refusal:
-            (!!lm && lm.end.x == i && lm.end.y == j)
-          )
-        ) {
+        if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color) {
           Array.prototype.push.apply(
             potentialMoves,
             this.getPotentialMovesFrom([i, j])
@@ -182,13 +163,20 @@ export class RefusalRules extends ChessRules {
     return potentialMoves;
   }
 
+  atLeastOneMove() {
+    const L = this.lastMove.length;
+    const lm = this.lastMove[L-1];
+    if (!!lm && !lm.noRef) return true;
+    return super.atLeastOneMove();
+  }
+
   getComputerMove() {
     // Just play at random for now... (TODO?)
     // Refuse last move with odds 1/3.
     const moves = this.getAllValidMoves();
     const refusal = moves.find(m => m.vanish[0].c != this.turn);
     if (!!refusal) {
-      if (Math.random() <= 0.33) return refusal;
+      if (moves.length == 1 || Math.random() <= 0.33) return refusal;
       const others = moves.filter(m => m.vanish[0].c == this.turn);
       return others[randInt(others.length)];
     }