Experimental in-place reorientation for Eightpieces + small fixes in BaseGame
[vchess.git] / client / src / variants / Doublemove1.js
index 7d1ee4d..f51dd7a 100644 (file)
@@ -79,7 +79,7 @@ export class Doublemove1Rules extends ChessRules {
 
   play(move) {
     move.flags = JSON.stringify(this.aggregateFlags());
-    move.turn = this.turn + this.subTurn;
+    move.turn = [this.turn, this.subTurn];
     V.PlayOnBoard(this.board, move);
     const epSq = this.getEpSquare(move);
     if (this.movesCount == 0) {
@@ -94,12 +94,14 @@ export class Doublemove1Rules extends ChessRules {
       this.epSquares.push([epSq]);
       move.checkOnSubturn1 = true;
       this.movesCount++;
-    } else {
+    }
+    else {
       if (this.subTurn == 2) {
         this.turn = V.GetOppCol(this.turn);
         let lastEpsq = this.epSquares[this.epSquares.length - 1];
         lastEpsq.push(epSq);
-      } else {
+      }
+      else {
         this.epSquares.push([epSq]);
         this.movesCount++;
       }
@@ -109,7 +111,7 @@ export class Doublemove1Rules extends ChessRules {
   }
 
   postPlay(move) {
-    const c = move.turn.charAt(0);
+    const c = move.turn[0];
     const piece = move.vanish[0].p;
     const firstRank = c == "w" ? V.size.x - 1 : 0;
 
@@ -127,7 +129,8 @@ export class Doublemove1Rules extends ChessRules {
     ) {
       const flagIdx = (move.start.y == this.castleFlags[c][0] ? 0 : 1);
       this.castleFlags[c][flagIdx] = V.size.y;
-    } else if (
+    }
+    else if (
       move.end.x == oppFirstRank && //we took opponent rook?
       this.castleFlags[oppCol].includes(move.end.y)
     ) {
@@ -144,13 +147,14 @@ export class Doublemove1Rules extends ChessRules {
       this.epSquares.pop();
       // Moves counter was just incremented:
       this.movesCount--;
-    } else {
+    }
+    else {
       // Undo the second half of a move
       let lastEpsq = this.epSquares[this.epSquares.length - 1];
       lastEpsq.pop();
     }
     this.turn = move.turn[0];
-    this.subTurn = parseInt(move.turn[1]);
+    this.subTurn = move.turn[1];
     super.postUndo(move);
   }
 
@@ -201,7 +205,7 @@ export class Doublemove1Rules extends ChessRules {
       return res;
     };
 
-    let moves11 = this.getAllValidMoves();
+    const moves11 = this.getAllValidMoves();
     let doubleMoves = [];
     // Rank moves using a min-max at depth 2
     for (let i = 0; i < moves11.length; i++) {
@@ -209,13 +213,14 @@ export class Doublemove1Rules extends ChessRules {
       if (this.turn != color) {
         // We gave check with last move: search the best opponent move
         doubleMoves.push({ moves: [moves11[i]], eval: getBestMoveEval() });
-      } else {
+      }
+      else {
         let moves12 = this.getAllValidMoves();
         for (let j = 0; j < moves12.length; j++) {
           this.play(moves12[j]);
           doubleMoves.push({
             moves: [moves11[i], moves12[j]],
-            eval: getBestMoveEval()
+            eval: getBestMoveEval() + 0.05 - Math.random() / 10
           });
           this.undo(moves12[j]);
         }
@@ -223,6 +228,8 @@ export class Doublemove1Rules extends ChessRules {
       this.undo(moves11[i]);
     }
 
+    // TODO: array + sort + candidates logic not required when adding small
+    // fluctuations to the eval function (could also be generalized).
     doubleMoves.sort((a, b) => {
       return (color == "w" ? 1 : -1) * (b.eval - a.eval);
     });
@@ -234,7 +241,6 @@ export class Doublemove1Rules extends ChessRules {
     ) {
       candidates.push(i);
     }
-
     const selected = doubleMoves[randInt(candidates.length)].moves;
     if (selected.length == 1) return selected[0];
     return selected;