| 41 | } |
| 42 | |
| 43 | public Evaluation minMax(final Board board, final int depth, final int printAt) { |
| 44 | final List<Move> legalMoves = board.getLegalMoves(); |
| 45 | nodesEvaluated++; |
| 46 | if (legalMoves.isEmpty() || depth == 0) { |
| 47 | return new Evaluation(null, -board.evaluation(legalMoves.size())); |
| 48 | } |
| 49 | Evaluation bestMove = null; |
| 50 | for (Move move : legalMoves) { |
| 51 | final Board copy = board.copy(); |
| 52 | copy.makeMove(move); |
| 53 | final Evaluation eval = minMax(copy, depth - 1, printAt); |
| 54 | if (depth == printAt) { |
| 55 | System.out.println(getString(move) + ": " + eval.getScore() + " " + move + |
| 56 | " " + copy.fenRepresentation()); |
| 57 | } |
| 58 | if (bestMove == null || bestMove.getScore() > -eval.getScore()) { |
| 59 | bestMove = new Evaluation(move, -eval.getScore()); |
| 60 | if (bestMove.getScore() < 0 && bestMove.getScore() + Integer.MAX_VALUE < 0.0001) { |
| 61 | return bestMove; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | return bestMove; |
| 66 | } |
| 67 | |
| 68 | public OutCome alphaBeta(final Board board, final int depth, double alpha, double beta, final int printAt) { |
| 69 | final List<Move> legalMoves = board.getLegalMoves(); |