Skip to content
Snippets Groups Projects
Commit 9ab8ea10 authored by Piers Williams's avatar Piers Williams
Browse files

Modified the timer to work better

parent 4a0b1839
No related branches found
No related tags found
No related merge requests found
...@@ -5,13 +5,20 @@ package battle.controllers.Piers; ...@@ -5,13 +5,20 @@ package battle.controllers.Piers;
*/ */
public class GameTimer { public class GameTimer {
long startTime; private long startTime;
long timeBudget; private long timeBudget;
private boolean timeSet = false;
public GameTimer() { public GameTimer() {
startTime = System.nanoTime(); startTime = System.nanoTime();
} }
public GameTimer(long budget) {
this();
setTimeBudgetMilliseconds(budget);
}
public long elapsed() { public long elapsed() {
return System.nanoTime() - startTime; return System.nanoTime() - startTime;
} }
...@@ -21,7 +28,9 @@ public class GameTimer { ...@@ -21,7 +28,9 @@ public class GameTimer {
} }
public void setTimeBudgetMilliseconds(long budget) { public void setTimeBudgetMilliseconds(long budget) {
if(timeSet) throw new IllegalAccessError("You shouldn't try to set the time budget now");
timeBudget = (long) (budget * 1.0E6); timeBudget = (long) (budget * 1.0E6);
timeSet = true;
} }
public long remainingTimeMilliseconds() { public long remainingTimeMilliseconds() {
......
...@@ -9,11 +9,15 @@ import battle.SimpleBattle; ...@@ -9,11 +9,15 @@ import battle.SimpleBattle;
*/ */
public class PiersMCTS implements BattleController { public class PiersMCTS implements BattleController {
public PiersMCTS() {
MCTSNode.setAllActions();
}
@Override @Override
public Action getAction(SimpleBattle gameStateCopy, int playerId) { public Action getAction(SimpleBattle gameStateCopy, int playerId) {
MCTSNode root = new MCTSNode(2.0, playerId); MCTSNode root = new MCTSNode(2.0, playerId);
GameTimer timer = new GameTimer(); GameTimer timer = new GameTimer();
timer.setTimeBudgetMilliseconds(100); timer.setTimeBudgetMilliseconds(40);
while (timer.remainingTimePercent() > 10) { while (timer.remainingTimePercent() > 10) {
MCTSNode travel = root.select(gameStateCopy, 3); MCTSNode travel = root.select(gameStateCopy, 3);
double[] results = travel.rollout(gameStateCopy); double[] results = travel.rollout(gameStateCopy);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment