Eightpieces almost ready
[vchess.git] / client / src / base_rules.js
index 2bfdc99..4c32fe6 100644 (file)
@@ -428,6 +428,7 @@ export const ChessRules = class ChessRules {
     this.INIT_COL_ROOK = { w: [-1, -1], b: [-1, -1] };
     this.kingPos = { w: [-1, -1], b: [-1, -1] }; //squares of white and black king
     const fenRows = V.ParseFen(fen).position.split("/");
+    const startRow = { 'w': V.size.x - 1, 'b': 0 };
     for (let i = 0; i < fenRows.length; i++) {
       let k = 0; //column index on board
       for (let j = 0; j < fenRows[i].length; j++) {
@@ -441,12 +442,16 @@ export const ChessRules = class ChessRules {
             this.INIT_COL_KING["w"] = k;
             break;
           case "r":
-            if (this.INIT_COL_ROOK["b"][0] < 0) this.INIT_COL_ROOK["b"][0] = k;
-            else this.INIT_COL_ROOK["b"][1] = k;
+            if (i == startRow['b']) {
+              if (this.INIT_COL_ROOK["b"][0] < 0) this.INIT_COL_ROOK["b"][0] = k;
+              else this.INIT_COL_ROOK["b"][1] = k;
+            }
             break;
           case "R":
-            if (this.INIT_COL_ROOK["w"][0] < 0) this.INIT_COL_ROOK["w"][0] = k;
-            else this.INIT_COL_ROOK["w"][1] = k;
+            if (i == startRow['w']) {
+              if (this.INIT_COL_ROOK["w"][0] < 0) this.INIT_COL_ROOK["w"][0] = k;
+              else this.INIT_COL_ROOK["w"][1] = k;
+            }
             break;
           default: {
             const num = parseInt(fenRows[i].charAt(j));
@@ -1029,13 +1034,13 @@ export const ChessRules = class ChessRules {
         move.start.x == firstRank && //our rook moves?
         this.INIT_COL_ROOK[c].includes(move.start.y)
       ) {
-        const flagIdx = move.start.y == this.INIT_COL_ROOK[c][0] ? 0 : 1;
+        const flagIdx = (move.start.y == this.INIT_COL_ROOK[c][0] ? 0 : 1);
         this.castleFlags[c][flagIdx] = false;
       } else if (
         move.end.x == oppFirstRank && //we took opponent rook?
         this.INIT_COL_ROOK[oppCol].includes(move.end.y)
       ) {
-        const flagIdx = move.end.y == this.INIT_COL_ROOK[oppCol][0] ? 0 : 1;
+        const flagIdx = (move.end.y == this.INIT_COL_ROOK[oppCol][0] ? 0 : 1);
         this.castleFlags[oppCol][flagIdx] = false;
       }
     }
@@ -1053,7 +1058,7 @@ export const ChessRules = class ChessRules {
   play(move) {
     // DEBUG:
 //    if (!this.states) this.states = [];
-//    const stateFen = this.getBaseFen() + this.getTurnFen() + this.getFlagsFen();
+//    const stateFen = this.getBaseFen() + this.getTurnFen();// + this.getFlagsFen();
 //    this.states.push(stateFen);
 
     if (V.HasFlags) move.flags = JSON.stringify(this.aggregateFlags()); //save flags (for undo)
@@ -1073,7 +1078,7 @@ export const ChessRules = class ChessRules {
     this.unupdateVariables(move);
 
     // DEBUG:
-//    const stateFen = this.getBaseFen() + this.getTurnFen() + this.getFlagsFen();
+//    const stateFen = this.getBaseFen() + this.getTurnFen();// + this.getFlagsFen();
 //    if (stateFen != this.states[this.states.length-1]) debugger;
 //    this.states.pop();
   }
@@ -1136,44 +1141,48 @@ export const ChessRules = class ChessRules {
 
     // Rank moves using a min-max at depth 2 (if search_depth >= 2!)
     for (let i = 0; i < moves1.length; i++) {
-      if (V.SEARCH_DEPTH == 1) {
-        moves1[i].eval = this.evalPosition();
+      this.play(moves1[i]);
+      const score1 = this.getCurrentScore();
+      if (score1 != "*") {
+        moves1[i].eval =
+          score1 == "1/2"
+            ? 0
+            : (score1 == "1-0" ? 1 : -1) * maxeval;
+      }
+      if (V.SEARCH_DEPTH == 1 || score1 != "*") {
+        if (!moves1[i].eval) moves1[i].eval = this.evalPosition();
+        this.undo(moves1[i]);
         continue;
       }
       // Initial self evaluation is very low: "I'm checkmated"
       moves1[i].eval = (color == "w" ? -1 : 1) * maxeval;
-      this.play(moves1[i]);
-      const score1 = this.getCurrentScore();
-      let eval2 = undefined;
-      if (score1 == "*") {
-        // Initial enemy evaluation is very low too, for him
-        eval2 = (color == "w" ? 1 : -1) * maxeval;
-        // Second half-move:
-        let moves2 = this.getAllValidMoves();
-        for (let j = 0; j < moves2.length; j++) {
-          this.play(moves2[j]);
-          const score2 = this.getCurrentScore();
-          let evalPos = 0; //1/2 value
-          switch (score2) {
-            case "*":
-              evalPos = this.evalPosition();
-              break;
-            case "1-0":
-              evalPos = maxeval;
-              break;
-            case "0-1":
-              evalPos = -maxeval;
-              break;
-          }
-          if (
-            (color == "w" && evalPos < eval2) ||
-            (color == "b" && evalPos > eval2)
-          ) {
-            eval2 = evalPos;
-          }
-          this.undo(moves2[j]);
+      // Initial enemy evaluation is very low too, for him
+      let eval2 = (color == "w" ? 1 : -1) * maxeval;
+      // Second half-move:
+      let moves2 = this.getAllValidMoves();
+      for (let j = 0; j < moves2.length; j++) {
+        this.play(moves2[j]);
+        const score2 = this.getCurrentScore();
+        let evalPos = 0; //1/2 value
+        switch (score2) {
+          case "*":
+            evalPos = this.evalPosition();
+            break;
+          case "1-0":
+            evalPos = maxeval;
+            break;
+          case "0-1":
+            evalPos = -maxeval;
+            break;
         }
-      } else eval2 = score1 == "1/2" ? 0 : (score1 == "1-0" ? 1 : -1) * maxeval;
+        if (
+          (color == "w" && evalPos < eval2) ||
+          (color == "b" && evalPos > eval2)
+        ) {
+          eval2 = evalPos;
+        }
+        this.undo(moves2[j]);
+      }
       if (
         (color == "w" && eval2 > moves1[i].eval) ||
         (color == "b" && eval2 < moves1[i].eval)