Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bots;
- import java.util.*;
- import pirates.game.PirateBot;
- import pirates.game.PirateGame;
- public class MyBot implements PirateBot {
- public static int finishedThreads = 0;
- public static int[] leaves;
- private PirateGame game;
- public static final int THREADS_LENGTH = 20;
- /**
- * Process a turn
- */
- public void doTurn(PirateGame game) {
- this.game = game;
- leaves = new int[THREADS_LENGTH];
- Arrays.fill(leaves, 0);
- // double lastTime;
- // lastTime = System.currentTimeMillis();
- Thread[] temp = new Thread[THREADS_LENGTH];
- for (int i = 0; i < temp.length; i++) {
- temp[i] = new Thread(new MinimaxTree(10, 3, 0, this.game, i));
- temp[i].start();
- }
- for (int i = 0; i < temp.length; i++) {
- // check if thread is not dead
- if (temp[i] != null) {
- try {
- // game.debug("Waiting for thread to die.");
- temp[i].join();
- // DEBUG
- //if (game.getTurn() < 3)
- // game.debug("Thread " + i + " died.");
- } catch (Exception e) {
- String msg = e.getMessage();
- if (msg == null)
- game.debug("nullmsg");
- else
- game.debug(msg);
- }
- }
- }
- runTests(THREADS_LENGTH);
- Arrays.fill(leaves, 0);
- finishedThreads = 0;
- }
- /**
- * Debug
- * @param howManyTurns first X turns to run
- */
- private void runTests(int howManyTurns)
- {
- if (game.getTurn() < howManyTurns) {
- // game.debug("The tree(function) is done : "
- // + (System.currentTimeMillis() - lastTime));
- game.debug("Finished threads: " + finishedThreads);
- for (int i = 0; i < leaves.length; i++) {
- game.debug("Done " + leaves[i] + " leaves!");
- }
- }
- }
- }
- class MinimaxTree implements Runnable {
- private static Random rnd = new Random();
- private int nodes;
- private int maxDepth;
- private int depth;
- private PirateGame game;
- private int threadIndex;
- public MinimaxTree(int nodes, int maxDepth, int depth, PirateGame game,
- int threadIndex) {
- this.nodes = nodes;
- this.maxDepth = maxDepth;
- this.depth = depth;
- this.game = game;
- this.threadIndex = threadIndex;
- rnd = new Random();
- }
- /**
- * Does math. Count leaves.
- * @return
- */
- public double doSomeMath() {
- double random = rnd.nextDouble();
- MyBot.leaves[threadIndex]++;
- return Math.pow(Math.E, random) * random;
- }
- /**
- * The minimax function.
- *
- * @param nodes
- * @param maxDepth
- * @param depth
- * @return
- */
- private double minimax(int nodes, int maxDepth, int depth) {
- if (depth >= maxDepth) {
- return doSomeMath();
- }
- for (int i = 0; i < nodes; i++) {
- minimax(nodes, maxDepth, depth + 1);
- }
- return -1;
- }
- /**
- * run method
- */
- public void run() {
- minimax(nodes, maxDepth, depth);
- MyBot.finishedThreads++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement