+ let currentBest = moves1[_.sample(candidates, 1)];
+
+ // From here, depth >= 3: may take a while, so we control time
+ const timeStart = Date.now();
+
+ // Skip depth 3+ if we found a checkmate (or if we are checkmated in 1...)
+ if (VariantRules.SEARCH_DEPTH >= 3
+ && Math.abs(moves1[0].eval) < VariantRules.THRESHOLD_MATE)
+ {
+ for (let i=0; i<moves1.length; i++)
+ {
+ if (Date.now()-timeStart >= 5000) //more than 5 seconds
+ return currentBest; //depth 2 at least
+ this.play(moves1[i]);
+ // 0.1 * oldEval : heuristic to avoid some bad moves (not all...)
+ moves1[i].eval = 0.1*moves1[i].eval +
+ this.alphabeta(VariantRules.SEARCH_DEPTH-1, -maxeval, maxeval);
+ this.undo(moves1[i]);
+ }
+ moves1.sort( (a,b) => { return (color=="w" ? 1 : -1) * (b.eval - a.eval); });
+ }
+ else
+ return currentBest;
+ //console.log(moves1.map(m => { return [this.getNotation(m), m.eval]; }));