Advertisement
Parasect

Untitled

Mar 8th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. package bots;
  2. import java.util.*;
  3. import pirates.game.PirateBot;
  4. import pirates.game.PirateGame;
  5.  
  6. public class MyBot implements PirateBot {
  7. public static int finishedThreads = 0;
  8. public static int leaves;
  9. private PirateGame game;
  10. public static final int THREADS_LENGTH = 20;
  11.  
  12. public static synchronized void incrementLeaves(){
  13. leaves++;
  14. }
  15. /**
  16. * Process a turn
  17. */
  18. public void doTurn(PirateGame game) {
  19. this.game = game;
  20. // double lastTime;
  21. // lastTime = System.currentTimeMillis();
  22. Thread[] temp = new Thread[THREADS_LENGTH];
  23. for (int i = 0; i < temp.length; i++) {
  24. temp[i] = new Thread(new MinimaxTree(10, 3, 0, this.game, i));
  25. temp[i].start();
  26. }
  27.  
  28. for (int i = 0; i < temp.length; i++) {
  29. // check if thread is not dead
  30. if (temp[i] != null) {
  31. try {
  32. // game.debug("Waiting for thread to die.");
  33. temp[i].join();
  34. // DEBUG
  35. //if (game.getTurn() < 3)
  36. // game.debug("Thread " + i + " died.");
  37. } catch (Exception e) {
  38. String msg = e.getMessage();
  39. if (msg == null)
  40. game.debug("nullmsg");
  41. else
  42. game.debug(msg);
  43. }
  44. }
  45. }
  46.  
  47. runTests(THREADS_LENGTH);
  48. leaves = 0;
  49. finishedThreads = 0;
  50. }
  51.  
  52. /**
  53. * Debug
  54. * @param howManyTurns first X turns to run
  55. */
  56. private void runTests(int howManyTurns)
  57. {
  58. if (game.getTurn() < howManyTurns) {
  59. // game.debug("The tree(function) is done : "
  60. // + (System.currentTimeMillis() - lastTime));
  61. game.debug("Finished threads: " + finishedThreads);
  62. game.debug("Done " + leaves + " leaves!");
  63. }
  64. }
  65. }
  66.  
  67. class MinimaxTree implements Runnable {
  68. private static Random rnd = new Random();
  69. private int nodes;
  70. private int maxDepth;
  71. private int depth;
  72. private PirateGame game;
  73. private int threadIndex;
  74.  
  75. public MinimaxTree(int nodes, int maxDepth, int depth, PirateGame game,
  76. int threadIndex) {
  77. this.nodes = nodes;
  78. this.maxDepth = maxDepth;
  79. this.depth = depth;
  80. this.game = game;
  81. this.threadIndex = threadIndex;
  82. rnd = new Random();
  83. }
  84. /**
  85. * Does math. Count leaves.
  86. * @return
  87. */
  88. public double doSomeMath() {
  89. double random = rnd.nextDouble();
  90. MyBot.incrementLeaves();
  91. return Math.pow(Math.E, random) * random;
  92. }
  93.  
  94. /**
  95. * The minimax function.
  96. *
  97. * @param nodes
  98. * @param maxDepth
  99. * @param depth
  100. * @return
  101. */
  102. private double minimax(int nodes, int maxDepth, int depth) {
  103. if (depth >= maxDepth) {
  104. return doSomeMath();
  105. }
  106. for (int i = 0; i < nodes; i++) {
  107. minimax(nodes, maxDepth, depth + 1);
  108. }
  109. return -1;
  110. }
  111.  
  112. /**
  113. * run method
  114. */
  115. public void run() {
  116. minimax(nodes, maxDepth, depth);
  117. MyBot.finishedThreads++;
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement