Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. private int minimax(State state, int depth, int min, int max) {
  2. ArrayList<Move> moves = generateMoves(state.board, state.colour);
  3. char opponent = (state.colour == DraughtBoard.WHITE) ? DraughtBoard.BLACK : DraughtBoard.WHITE;
  4.  
  5. if (moves.size() == 1)
  6. nextMove = moves.get(0);
  7.  
  8. int bestScore;
  9. Move bestMove = new Move();
  10. int score = 0;
  11.  
  12. if (depth == 0 || moves.size() == 0) {
  13. return evaluateBoard(state);
  14. }
  15.  
  16. if (colour == DraughtBoard.WHITE) {
  17. bestScore = min;
  18. for (Move move : moves) {
  19. char[][] temp = state.board.clone();
  20. boolean scored = simulateMove(move, temp);
  21.  
  22. State nextState = new State(temp, opponent, state.whiteScore, state.blackScore);
  23. if (scored) state.whiteScore++;
  24.  
  25. score = minimax(state, depth-1, bestScore, max);
  26.  
  27. if (score > bestScore) {
  28. bestScore = score;
  29. bestMove = move;
  30. }
  31. if (bestScore > max) return max;
  32. }
  33. nextMove = bestMove;
  34. return bestScore;
  35. } else {
  36. bestScore = max;
  37. for (Move move : moves) {
  38. char[][] temp = state.board.clone();
  39. boolean scored = simulateMove(move, temp);
  40.  
  41. State nextState = new State(temp, opponent, state.whiteScore, state.blackScore);
  42. if (scored) state.blackScore++;
  43.  
  44. score = minimax(state, depth-1, min, bestScore);
  45.  
  46. if (score < bestScore) {
  47. bestScore = score;
  48. bestMove = move;
  49. }
  50. if (bestScore < min) return min;
  51. }
  52. nextMove = bestMove;
  53. return bestScore;
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement