- }
- // For black at depth 1, super method is fine:
- return super.getComputerMove();
+ };
+
+ const getBestBlackMove = () => {
+ let moves = this.getAllValidMoves();
+ moves.forEach(m => {
+ m.eval = V.INFINITY;
+ this.play(m);
+ const evalM = getBestWhiteMove("terminal").eval
+ this.undo(m);
+ if (evalM < m.eval) m.eval = evalM;
+ });
+ moves.sort((a, b) => a.eval - b.eval);
+ let candidates = [0];
+ for (
+ let i = 1;
+ i < moves.length && moves[i].eval == moves[0].eval;
+ i++
+ ) {
+ candidates.push(i);
+ }
+ const idx = candidates[randInt(candidates.length)];
+ return moves[idx];
+ };
+
+ const color = this.turn;
+ return (color == 'w' ? getBestWhiteMove() : getBestBlackMove());