Speed up computer thinking, fix Zen ep
[vchess.git] / public / javascripts / base_rules.js
index 9a4ef91..d8ec34a 100644 (file)
@@ -98,6 +98,7 @@ class ChessRules
                                j++;
                        }
                }
+               // TODO: since we keep moves stack, next 2 are redundant
                let epSq = undefined;
                if (fenParts[2] != "-")
                {
@@ -629,6 +630,7 @@ class ChessRules
                return false;
        }
 
+       // Is color c under check after move ?
        underCheck(move, c)
        {
                this.play(move);
@@ -637,6 +639,17 @@ class ChessRules
                return res;
        }
 
+       // On which squares is color c under check (after move) ?
+       getCheckSquares(move, c)
+       {
+               this.play(move);
+               let res = this.isAttacked(this.kingPos[c], this.getOppCol(c))
+                       ? [ JSON.parse(JSON.stringify(this.kingPos[c])) ] //need to duplicate!
+                       : [ ];
+               this.undo(move);
+               return res;
+       }
+
        // Apply a move on board
        static PlayOnBoard(board, move)
        {
@@ -826,8 +839,7 @@ class ChessRules
 
        alphabeta(color, oppCol, depth, alpha, beta)
   {
-               const moves = this.getAllValidMoves(color);
-               if (moves.length == 0)
+               if (!this.atLeastOneMove(color))
                {
                        switch (this.checkGameEnd(color))
                        {
@@ -837,6 +849,7 @@ class ChessRules
                }
                if (depth == 0)
       return this.evalPosition();
+               const moves = this.getAllValidMoves(color);
     let v = color=="w" ? -1000 : 1000;
                if (color == "w")
                {