TODO: debug Knightrelay, use generators + yield for moves generation
[vchess.git] / client / src / base_rules.js
index 7949d7f..ef07742 100644 (file)
@@ -46,12 +46,27 @@ export const ChessRules = class ChessRules {
   static get CanAnalyze() {
     return true;
   }
+  // Patch: issues with javascript OOP, objects can't access static fields.
+  get canAnalyze() {
+    return V.CanAnalyze;
+  }
 
   // Some variants show incomplete information,
   // and thus show only a partial moves list or no list at all.
   static get ShowMoves() {
     return "all";
   }
+  get showMoves() {
+    return V.ShowMoves;
+  }
+
+  // Some variants always show the same orientation
+  static get CanFlip() {
+    return true;
+  }
+  get canFlip() {
+    return V.CanFlip;
+  }
 
   // Turn "wb" into "B" (for FEN)
   static board2fen(b) {
@@ -267,12 +282,13 @@ export const ChessRules = class ChessRules {
       pieces[c][knight2Pos] = "n";
       pieces[c][rook2Pos] = "r";
     }
+    // Add turn + flags + enpassant
     return (
       pieces["b"].join("") +
       "/pppppppp/8/8/8/8/PPPPPPPP/" +
       pieces["w"].join("").toUpperCase() +
       " w 0 1111 -"
-    ); //add turn + flags + enpassant
+    );
   }
 
   // "Parse" FEN: just return untransformed string data
@@ -357,9 +373,9 @@ export const ChessRules = class ChessRules {
       for (let indexInRow = 0; indexInRow < rows[i].length; indexInRow++) {
         const character = rows[i][indexInRow];
         const num = parseInt(character);
+        // If num is a number, just shift j:
         if (!isNaN(num)) j += num;
-        //just shift j
-        //something at position i,j
+        // Else: something at position i,j
         else board[i][j++] = V.fen2board(character);
       }
     }
@@ -370,7 +386,6 @@ export const ChessRules = class ChessRules {
   setFlags(fenflags) {
     // white a-castle, h-castle, black a-castle, h-castle
     this.castleFlags = { w: [true, true], b: [true, true] };
-    if (!fenflags) return;
     for (let i = 0; i < 4; i++)
       this.castleFlags[i < 2 ? "w" : "b"][i % 2] = fenflags.charAt(i) == "1";
   }
@@ -452,7 +467,7 @@ export const ChessRules = class ChessRules {
     return { x: 8, y: 8 };
   }
 
-  // Color of thing on suqare (i,j). 'undefined' if square is empty
+  // Color of thing on square (i,j). 'undefined' if square is empty
   getColor(i, j) {
     return this.board[i][j].charAt(0);
   }
@@ -528,7 +543,7 @@ export const ChessRules = class ChessRules {
   ////////////////////
   // MOVES GENERATION
 
-  // All possible moves from selected square (assumption: color is OK)
+  // All possible moves from selected square
   getPotentialMovesFrom([x, y]) {
     switch (this.getPiece(x, y)) {
       case V.PAWN:
@@ -619,8 +634,8 @@ export const ChessRules = class ChessRules {
         x + shiftX == lastRank
           ? [V.ROOK, V.KNIGHT, V.BISHOP, V.QUEEN]
           : [V.PAWN];
-      // One square forward
       if (this.board[x + shiftX][y] == V.EMPTY) {
+        // One square forward
         for (let piece of finalPieces) {
           moves.push(
             this.getBasicMove([x, y], [x + shiftX, y], {
@@ -1106,6 +1121,7 @@ export const ChessRules = class ChessRules {
     // Some variants may show a bigger moves list to the human (Switching),
     // thus the argument "computer" below (which is generally ignored)
     let moves1 = this.getAllValidMoves("computer");
+
     if (moves1.length == 0)
       //TODO: this situation should not happen
       return null;
@@ -1194,7 +1210,7 @@ export const ChessRules = class ChessRules {
         return (color == "w" ? 1 : -1) * (b.eval - a.eval);
       });
     } else return currentBest;
-    //    console.log(moves1.map(m => { return [this.getNotation(m), m.eval]; }));
+    console.log(moves1.map(m => { return [this.getNotation(m), m.eval]; }));
 
     candidates = [0];
     for (let j = 1; j < moves1.length && moves1[j].eval == moves1[0].eval; j++)
@@ -1239,6 +1255,18 @@ export const ChessRules = class ChessRules {
       for (let j = 0; j < V.size.y; j++) {
         if (this.board[i][j] != V.EMPTY) {
           const sign = this.getColor(i, j) == "w" ? 1 : -1;
+
+
+//TODO: debug in KnightRelay
+if (isNaN(V.VALUES[this.getPiece(i, j)])) {
+  console.log(i + " " + j);
+  console.log(this.getPiece(i, j));
+  console.log(this.board);
+  console.log("ajout " + sign + " * "+  V.VALUES[this.getPiece(i, j)]);
+  debugger;
+}
+
+
           evaluation += sign * V.VALUES[this.getPiece(i, j)];
         }
       }
@@ -1254,7 +1282,7 @@ export const ChessRules = class ChessRules {
   // TODO: un-ambiguous notation (switch on piece type, check directions...)
   getNotation(move) {
     if (move.appear.length == 2 && move.appear[0].p == V.KING)
-      //castle
+      // Castle
       return move.end.y < move.start.y ? "0-0-0" : "0-0";
 
     // Translate final square