Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. import Helpers.Environment;
  2. import Helpers.Move;
  3. import Helpers.PawnPosition;
  4. import Helpers.TimeIsUpException;
  5.  
  6. import java.sql.Time;
  7. import java.util.ArrayList;
  8. import java.util.HashSet;
  9.  
  10. public class MyAgent implements Agent
  11. {
  12. private String role; // the name of this agent's role (white or black)
  13. private int playclock; // this is how much time (in seconds) we have before nextAction needs to return a move
  14. private boolean myTurn; // whether it is this agent's turn or not
  15. private State initialState;
  16. private State currentState;
  17. HashSet<State> possibleMoves;
  18. private long timeNow;
  19. private long maxTime;
  20.  
  21. public void init(String role, int width, int height, int playclock) {
  22. this.role = role;
  23. this.playclock = playclock;
  24. maxTime = (playclock * 1000) - 50;
  25. myTurn = !role.equals("white");
  26. Environment env = new Environment(height, width);
  27. initialState = new State(env);
  28. initialState.addToLists(width,height);
  29. }
  30.  
  31. // lastMove is null the first time nextAction gets called (in the initial state)
  32. // otherwise it contains the coordinates x1,y1,x2,y2 of the move that the last player did
  33. public String nextAction(int[] lastMove) {
  34. if (lastMove != null) {
  35. int x1 = lastMove[0], y1 = lastMove[1], x2 = lastMove[2], y2 = lastMove[3];
  36. String roleOfLastPlayer;
  37. if (myTurn && role.equals("white") || !myTurn && role.equals("black")) {
  38. roleOfLastPlayer = "white";
  39. } else {
  40. roleOfLastPlayer = "black";
  41. }
  42. System.out.println(roleOfLastPlayer + " moved from " + x1 + "," + y1 + " to " + x2 + "," + y2);
  43.  
  44. // TODO: 1. update your internal world model according to the action that was just executed
  45. if(x2 == x1 && y2 < y1){
  46. //initialState.moveForward(x1,y1,false);
  47. }
  48. else if(x2 < x1 && y2 == y1){
  49. //initialState.moveDiagonally(x1,y1, true);
  50. }
  51. else{
  52. //initialState.moveDiagonally(x1,y1, false);
  53. }
  54. }
  55. else {
  56. currentState = initialState;
  57. }
  58. // update turn (above that line it myTurn is still for the previous state)
  59. myTurn = !myTurn;
  60. if (myTurn) {
  61. // TODO: 2. run alpha-beta search to determine the best move
  62.  
  63. Move bestMove = Search(currentState);
  64. System.out.println("M");
  65. System.out.println(bestMove.getFrom().getX() + ", " + bestMove.getFrom().getY());
  66. System.out.println(bestMove.getTo().getX() + ", " + bestMove.getTo().getY());
  67. int x1,y1,x2,y2;
  68. return "(move " + bestMove.getFrom().getX() + " " + bestMove.getFrom().getY() + " " + bestMove.getTo().getX() + " " + bestMove.getTo().getY() + ")";
  69. } else {
  70. return "noop";
  71. }
  72. }
  73.  
  74. // is called when the game is over or the match is aborted
  75. @Override
  76. public void cleanup() {
  77. // TODO: cleanup so that the agent is ready for the next match
  78. }
  79.  
  80. private Move Search(State state)
  81. {
  82. timeNow = System.currentTimeMillis();
  83. Move best = null;
  84. try
  85. {
  86. for(int depth = 2;;depth++)
  87. {
  88. best = RootSearch(state, depth);
  89. }
  90. }
  91. catch (TimeIsUpException msg)
  92. {
  93. return best;
  94. }
  95. }
  96.  
  97. private Move RootSearch(State state, int depth) throws TimeIsUpException
  98. {
  99. if(timeNow + maxTime < System.currentTimeMillis())
  100. {
  101. throw new TimeIsUpException("mamma'in");
  102. }
  103. int alpha = -100;
  104. int beta = 100;
  105. Move best = null;
  106. ArrayList<Move> legalMoves;
  107. legalMoves = state.getLegalMoves(state.getCurrentPlayer());
  108. for(Move m : legalMoves)
  109. {
  110. int value = -ChildSearch(state.getStateByAction(m), -alpha, -beta, depth - 1);
  111. if(value > alpha) best = m;
  112. if(alpha >= beta) break;
  113. }
  114. return best;
  115. }
  116.  
  117. private int ChildSearch(State state, int alpha, int beta, int depth) throws TimeIsUpException
  118. {
  119. if(timeNow + maxTime < System.currentTimeMillis())
  120. {
  121. throw new TimeIsUpException("mamma'in");
  122. }
  123. if(depth <= 0 || state.isGameOver(state.getCurrentPlayer())) return state.evaluateScore(state.getCurrentPlayer());
  124. ArrayList<Move> legalMoves;
  125. legalMoves = state.getLegalMoves(state.getCurrentPlayer());
  126. for(Move m : legalMoves)
  127. {
  128. int value = -ChildSearch(state.getStateByAction(m), -alpha, -beta, depth - 1);
  129. if(value > alpha) alpha = value;
  130. if(alpha >= beta) break;
  131. }
  132. return alpha;
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement