Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Player {
  4. static int mainPlayer;
  5.  
  6. /**
  7. * Performs a move
  8. *
  9. * @param pState
  10. * the current state of the board
  11. * @param pDue
  12. * time before which we must have returned
  13. * @return the next state the board is in after our move
  14. */
  15. public GameState play(final GameState pState, final Deadline pDue) {
  16.  
  17. Vector<GameState> lNextStates = new Vector<GameState>();
  18. pState.findPossibleMoves(lNextStates);
  19. mainPlayer = pState.getNextPlayer();
  20. System.err.println("Jag är:" + pState.getNextPlayer());
  21.  
  22. if (lNextStates.size() == 0) {
  23. // Must play "pass" move if there are no other moves possible.
  24. return new GameState(pState, new Move());
  25. }
  26.  
  27. /**
  28. * Here you should write your algorithms to get the best next move, i.e.
  29. * the best next state. This skeleton returns a random move instead.
  30. */
  31.  
  32. int score = Integer.MIN_VALUE;
  33. int tempScore = 0;
  34. int index = 0;
  35. for (int i = 0; i < lNextStates.size(); i++) {
  36. tempScore = alphaBeta(lNextStates.elementAt(i), 9, Integer.MIN_VALUE, Integer.MAX_VALUE, 2);
  37. System.err.println("Potentiella drag: " + tempScore);
  38. if (tempScore > score){
  39. score = tempScore;
  40. index = i;
  41. }
  42. }
  43. System.err.println("Det bästa draget har value: " + score);
  44. return lNextStates.elementAt(index);
  45. }
  46.  
  47. /*
  48. * and to check if it is a king, you would check if
  49. *
  50. * (lBoard.At(23)&CELL_KING)
  51. */
  52.  
  53. /**
  54. * This function counts the normal pieces as 1 point and the kings as 2 points.
  55. *
  56. */
  57.  
  58. public static int evalFunction(GameState gameState, int player) {
  59.  
  60. int redValue = 0;
  61. int whiteValue = 0;
  62.  
  63. for (int cell = 0; cell < gameState.NUMBER_OF_SQUARES; cell++) {
  64. int row = gameState.cellToRow(cell);
  65. int col = gameState.cellToCol(cell);
  66. int cellValue = gameState.get(row, col);
  67. //System.err.println(cellValue);
  68. if (cellValue == Constants.CELL_RED){
  69. redValue += 1;
  70. }
  71. else if (cellValue == Constants.CELL_RED + Constants.CELL_KING){
  72. redValue += 1.5;
  73. }
  74. else if (cellValue == Constants.CELL_WHITE){
  75. whiteValue += 1;
  76. }
  77. else if (cellValue == Constants.CELL_WHITE + Constants.CELL_KING){
  78. whiteValue += 1.5;
  79. }
  80. }
  81.  
  82. if (redValue == 0) {
  83. whiteValue += 10;
  84. } else if (whiteValue == 0) {
  85. redValue += 10;
  86. }
  87.  
  88. int value;
  89.  
  90. if (mainPlayer == 1) {
  91. value = redValue - whiteValue;
  92. } else{
  93. value = whiteValue - redValue;
  94. }
  95. return value;
  96. }
  97.  
  98. public static int alphaBeta (GameState gameState, int depth, int alpha, int beta, int player){
  99. Vector<GameState> nextStates = new Vector<GameState>();
  100. gameState.findPossibleMoves(nextStates);
  101.  
  102. int value;
  103. if (depth == 0 || nextStates.size() == 0){
  104. value = evalFunction(gameState, player);
  105. return value;
  106. }
  107. else if (player == 1){
  108. value = Integer.MIN_VALUE;
  109. for (GameState nextState: nextStates) {
  110. value = Math.max(value, alphaBeta(nextState, depth-1, alpha, beta, 2));
  111. alpha = Math.max(alpha, value);
  112. if (beta <= alpha){
  113. break;
  114. }
  115. }
  116. }
  117. else{
  118. value = Integer.MAX_VALUE;
  119. for (GameState nextState:nextStates) {
  120. value = Math.min(value, alphaBeta(nextState, depth-1, alpha, beta, 1));
  121. beta = Math.min(beta, value);
  122. if (beta <= alpha){
  123. break;
  124. }
  125. }
  126. }
  127.  
  128. return value;
  129. }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement