Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Player {
  4. int mainPlayer;
  5. int maxDepth = -1;
  6.  
  7. public GameState play(final GameState pState, final Deadline pDue) {
  8. Vector<GameState> lNextStates = new Vector<GameState>();
  9. pState.findPossibleMoves(lNextStates);
  10.  
  11. mainPlayer = pState.getNextPlayer();
  12.  
  13. if (lNextStates.size() == 0)
  14. return new GameState(pState, new Move());
  15.  
  16. if (lNextStates.size() == 1)
  17. return lNextStates.elementAt(0);
  18.  
  19. int bestIndex = 0;
  20. for (int depth = 1; depth < 11; depth++) {
  21. this.maxDepth = depth;
  22. int score = Integer.MIN_VALUE;
  23. int index = 0;
  24.  
  25. for (int i = 0; i < lNextStates.size(); i++) {
  26. int tempScore = alphaBeta(lNextStates.elementAt(i), 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 2, pDue);
  27. if (tempScore == -9999) {
  28. return lNextStates.elementAt(bestIndex);
  29. }
  30.  
  31. if (tempScore > score) {
  32. score = tempScore;
  33. index = i;
  34. }
  35. }
  36. bestIndex = index;
  37. }
  38.  
  39. return lNextStates.elementAt(bestIndex);
  40. }
  41.  
  42. public int eval(GameState gameState, int depth) {
  43. int redValue = 0;
  44. int whiteValue = 0;
  45.  
  46. for (int cell = 0; cell < GameState.NUMBER_OF_SQUARES; cell++) {
  47. int row = GameState.cellToRow(cell);
  48. int col = GameState.cellToCol(cell);
  49. int cellValue = gameState.get(row, col);
  50.  
  51. if (cellValue == Constants.CELL_RED) {
  52. redValue += 1;
  53. } else if (cellValue == Constants.CELL_RED + Constants.CELL_KING) {
  54. redValue += 4;
  55. } else if (cellValue == Constants.CELL_WHITE) {
  56. whiteValue += 1;
  57. } else if (cellValue == Constants.CELL_WHITE + Constants.CELL_KING) {
  58. whiteValue += 4;
  59. }
  60. }
  61.  
  62. if (gameState.isRedWin()) {
  63. redValue += 40 - depth;
  64. }
  65. if (gameState.isWhiteWin()) {
  66. whiteValue += 40 - depth;
  67. }
  68.  
  69. int extra = 0;
  70. if (gameState.isDraw()) {
  71. extra = 15 - depth;
  72. }
  73.  
  74. int value;
  75.  
  76. if (mainPlayer == 1) {
  77. value = redValue - whiteValue;
  78. } else {
  79. value = whiteValue - redValue;
  80. }
  81. return value + extra;
  82. }
  83.  
  84. public int alphaBeta(GameState gameState, int depth, int alpha, int beta, int player, Deadline pDue) {
  85. if (pDue.timeUntil() < 100000000 / 10) {
  86. return -9999;
  87. }
  88.  
  89. Vector<GameState> nextStates = new Vector<GameState>();
  90. gameState.findPossibleMoves(nextStates);
  91.  
  92. int value;
  93. if (depth == maxDepth || nextStates.size() == 0) {
  94. value = eval(gameState, depth);
  95. return value;
  96. }
  97.  
  98. else if (player == 1) {
  99. value = Integer.MIN_VALUE;
  100. for (GameState nextState : nextStates) {
  101. value = Math.max(value, alphaBeta(nextState, depth + 1, alpha, beta, 2, pDue));
  102. alpha = Math.max(alpha, value);
  103. if (beta <= alpha) {
  104. break;
  105. }
  106. }
  107. }
  108.  
  109. else {
  110. value = Integer.MAX_VALUE;
  111. for (GameState nextState : nextStates) {
  112. value = Math.min(value, alphaBeta(nextState, depth + 1, alpha, beta, 1, pDue));
  113. beta = Math.min(beta, value);
  114. if (beta <= alpha) {
  115. break;
  116. }
  117. }
  118. }
  119.  
  120. if (pDue.timeUntil() < 100000000 / 10) {
  121. return -9999;
  122. }
  123.  
  124. return value;
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement