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 1.73 KB | None | 0 0
  1. private Move Search(State state)
  2. {
  3. timeNow = System.currentTimeMillis();
  4. Move best = null;
  5. try
  6. {
  7. for(int depth = 0;;depth++)
  8. {
  9. best = RootSearch(state, depth);
  10. }
  11. }
  12. catch (TimeIsUpException msg)
  13. {
  14. return best;
  15. }
  16. }
  17.  
  18. private Move RootSearch(State state, int depth) throws TimeIsUpException
  19. {
  20. if(timeNow + maxTime < System.currentTimeMillis())
  21. {
  22. throw new TimeIsUpException("mamma'in");
  23. }
  24. int alpha = -100;
  25. int beta = 100;
  26. Move best = null;
  27. ArrayList<Move> legalMoves;
  28. legalMoves = state.getLegalMoves(state.getCurrentPlayer());
  29. for(Move m : legalMoves)
  30. {
  31. int value = -ChildSearch(state.getStateByAction(m), -alpha, -beta, depth - 1);
  32. if(value > alpha) best = m;
  33. if(alpha >= beta) break;
  34. }
  35. return best;
  36. }
  37.  
  38. private int ChildSearch(State state, int alpha, int beta, int depth) throws TimeIsUpException
  39. {
  40. if(timeNow + maxTime < System.currentTimeMillis())
  41. {
  42. throw new TimeIsUpException("mamma'in");
  43. }
  44. if(depth <= 0 || state.isGameOver(state.getCurrentPlayer())) return state.evaluateScore(state.getCurrentPlayer());
  45. ArrayList<Move> legalMoves;
  46. legalMoves = state.getLegalMoves(state.getCurrentPlayer());
  47. for(Move m : legalMoves)
  48. {
  49. int value = -ChildSearch(state.getStateByAction(m), -alpha, -beta, depth - 1);
  50. if(value > alpha) alpha = value;
  51. if(alpha >= beta) break;
  52. }
  53. return alpha;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement