Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private int minimax(State state, int depth, int min, int max) {
- ArrayList<Move> moves = generateMoves(state.board, state.colour);
- char opponent = (state.colour == DraughtBoard.WHITE) ? DraughtBoard.BLACK : DraughtBoard.WHITE;
- if (moves.size() == 1)
- nextMove = moves.get(0);
- int bestScore;
- Move bestMove = new Move();
- int score = 0;
- if (depth == 0 || moves.size() == 0) {
- return evaluateBoard(state);
- }
- if (colour == DraughtBoard.WHITE) {
- bestScore = min;
- for (Move move : moves) {
- char[][] temp = state.board.clone();
- boolean scored = simulateMove(move, temp);
- State nextState = new State(temp, opponent, state.whiteScore, state.blackScore);
- if (scored) state.whiteScore++;
- score = minimax(state, depth-1, bestScore, max);
- if (score > bestScore) {
- bestScore = score;
- bestMove = move;
- }
- if (bestScore > max) return max;
- }
- nextMove = bestMove;
- return bestScore;
- } else {
- bestScore = max;
- for (Move move : moves) {
- char[][] temp = state.board.clone();
- boolean scored = simulateMove(move, temp);
- State nextState = new State(temp, opponent, state.whiteScore, state.blackScore);
- if (scored) state.blackScore++;
- score = minimax(state, depth-1, min, bestScore);
- if (score < bestScore) {
- bestScore = score;
- bestMove = move;
- }
- if (bestScore < min) return min;
- }
- nextMove = bestMove;
- return bestScore;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement