Advertisement
Parasect

Untitled

Mar 8th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 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.      * Process a turn
  13.      */
  14.     public void doTurn(PirateGame game) {
  15.         this.game = game;
  16.         leaves = new int[THREADS_LENGTH];
  17.         Arrays.fill(leaves, 0);
  18.         // double lastTime;
  19.         // lastTime = System.currentTimeMillis();
  20.         Thread[] temp = new Thread[THREADS_LENGTH];
  21.         for (int i = 0; i < temp.length; i++) {
  22.             temp[i] = new Thread(new MinimaxTree(10, 3, 0, this.game, i));
  23.             temp[i].start();
  24.         }
  25.  
  26.         for (int i = 0; i < temp.length; i++) {
  27.             // check if thread is not dead
  28.             if (temp[i] != null) {
  29.                 try {
  30.                     // game.debug("Waiting for thread to die.");
  31.                     temp[i].join();
  32.                     // DEBUG
  33.                     //if (game.getTurn() < 3)
  34.                     //  game.debug("Thread " + i + " died.");
  35.                 } catch (Exception e) {
  36.                     String msg = e.getMessage();
  37.                     if (msg == null)
  38.                         game.debug("nullmsg");
  39.                     else
  40.                         game.debug(msg);
  41.                 }
  42.             }
  43.         }
  44.  
  45.         runTests(THREADS_LENGTH);
  46.         Arrays.fill(leaves, 0);
  47.         finishedThreads = 0;
  48.     }
  49.    
  50.     /**
  51.      * Debug
  52.      * @param howManyTurns first X turns to run
  53.      */
  54.     private void runTests(int howManyTurns)
  55.     {
  56.         if (game.getTurn() < howManyTurns) {
  57.             // game.debug("The tree(function) is done  : "
  58.             // + (System.currentTimeMillis() - lastTime));
  59.             game.debug("Finished threads: " + finishedThreads);
  60.             for (int i = 0; i < leaves.length; i++) {
  61.                 game.debug("Done " + leaves[i] + " leaves!");
  62.             }
  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.leaves[threadIndex]++;
  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