public int min(final Board board, final int depth) { if (depth == 0 || isEndGameScenario(board)) { return this.evaluator.evaluate(board, depth); } int lowestSeenValue = Integer.MAX_VALUE; for (final Move move : board.currentPlayer().getLegalMoves()) { final MoveTransition moveTransition = board.currentPlayer().makeMove(move); if (moveTransition.getMoveStatus().isDone()) { final int currentValue = max(moveTransition.getTransitionBoard(), depth - 1); if (currentValue <= lowestSeenValue) { lowestSeenValue = currentValue; } } } return lowestSeenValue; }
private List<Move> orderImpl(final Board board, final int depth) { final List<MoveOrderEntry> moveOrderEntries = new ArrayList<>(); final boolean SORT_DESCENDING = board.currentPlayer().getColor().isWhite(); for (final Move move : board.currentPlayer().getLegalMoves()) { final MoveTransition moveTransition = board.currentPlayer().makeMove(move); if (moveTransition.getMoveStatus().isDone()) { final int currentValue = board.currentPlayer().getColor().isWhite() ? min(moveTransition.getTransitionBoard(), depth - 1) : max(moveTransition.getTransitionBoard(), depth - 1); moveOrderEntries.add(new MoveOrderEntry(move, currentValue)); } } if (SORT_DESCENDING) { Collections.sort( moveOrderEntries, new Comparator<MoveOrderEntry>() { @Override public int compare(final MoveOrderEntry o1, final MoveOrderEntry o2) { return Ints.compare(o2.getScore(), o1.getScore()); } }); } else { Collections.sort( moveOrderEntries, new Comparator<MoveOrderEntry>() { @Override public int compare(final MoveOrderEntry o1, MoveOrderEntry o2) { return Ints.compare(o1.getScore(), o2.getScore()); } }); } final List<Move> orderedMoves = new ArrayList<>(); for (final MoveOrderEntry entry : moveOrderEntries) { orderedMoves.add(entry.getMove()); } return ImmutableList.copyOf(orderedMoves); }
private static boolean isEndGameScenario(final Board board) { return board.currentPlayer().isInCheckMate() || board.currentPlayer().isInStaleMate() || board.currentPlayer().getOpponent().isInCheckMate() || board.currentPlayer().getOpponent().isInStaleMate(); }