Limited bot to run within the 2 second window
authorJustin Worthe <justin@worthe-it.co.za>
Sat, 12 May 2018 20:37:48 +0000 (22:37 +0200)
committerJustin Worthe <justin@worthe-it.co.za>
Sat, 12 May 2018 20:37:48 +0000 (22:37 +0200)
Cargo.toml
src/lib.rs
src/main.rs
src/strategy/monte_carlo.rs
src/strategy/sample.rs

index 12a5214..7703fc6 100644 (file)
@@ -8,3 +8,4 @@ serde = "1.0.43"
 serde_json = "1.0.16"
 
 rand = "0.4.2"
+time = "0.1.4"
\ No newline at end of file
index a09f999..728f747 100644 (file)
@@ -5,6 +5,7 @@ extern crate serde_json;
 extern crate serde_derive;
 
 extern crate rand;
+extern crate time;
 
 pub mod json;
 pub mod engine;
index e5dc3aa..3d3d980 100644 (file)
@@ -1,4 +1,7 @@
 extern crate zombot;
+extern crate time;
+use time::PreciseTime;
+
 use zombot::*;
 use zombot::engine::command::Command;
 
@@ -12,8 +15,8 @@ use std::fs::File;
 use std::io::prelude::*;
 use std::process;
 
-fn choose_move(settings: &engine::settings::GameSettings, state: &engine::GameState) -> Command {
-    strategy::monte_carlo::choose_move(settings, state)
+fn choose_move(settings: &engine::settings::GameSettings, state: &engine::GameState, start_time: &PreciseTime) -> Command {
+    strategy::monte_carlo::choose_move(settings, state, start_time)
 }
 
 
@@ -25,6 +28,8 @@ fn write_command(filename: &str, command: Command) -> Result<(), Box<Error> > {
 
 
 fn main() {
+    let start_time = PreciseTime::now();
+    
     println!("Reading in state.json file");
     let (settings, state) = match json::read_state_from_file(STATE_PATH) {
         Ok(ok) => ok,
@@ -33,7 +38,7 @@ fn main() {
             process::exit(1);
         }
     };
-    let command = choose_move(&settings, &state);
+    let command = choose_move(&settings, &state, &start_time);
 
     match write_command(COMMAND_PATH, command) {
         Ok(()) => {}
@@ -42,4 +47,6 @@ fn main() {
             process::exit(1);
         }
     }
+
+    println!("Elapsed time: {}", start_time.to(PreciseTime::now()));
 }
index ba66d48..86960eb 100644 (file)
@@ -6,21 +6,28 @@ use rand::{thread_rng, Rng};
 use std::process;
 const MAX_MOVES: u16 = 400;
 
+use time::{Duration, PreciseTime};
+
 // TODO Round start time here
-pub fn choose_move(settings: &GameSettings, state: &GameState) -> Command {
+pub fn choose_move(settings: &GameSettings, state: &GameState, start_time: &PreciseTime) -> Command {
     println!("Using MONTE_CARLO strategy");
+
+    //just under the max of 2 seconds, to avoid issues like overhead in the bot being run, and we still need to write the result of this to file
+    let max_time = Duration::milliseconds(1950);
     
     let mut rng = thread_rng();
     let mut command_scores = CommandScore::init_command_scores(settings, state);
 
-    // TODO Repeat this until time is out
-    for _ in 0..1000 {
+    loop {
         for mut score in &mut command_scores {
             simulate_to_endstate(score, settings, state, &mut rng);
         }
+        if start_time.to(PreciseTime::now()) > max_time {
+            break;
+        }
     }
 
-    println!("{:?}", command_scores);
+    println!("{:#?}", command_scores);
     let command = command_scores.iter().max_by_key(|&c| c.win_ratio());
     
     match command {
index 2dad924..72ab66c 100644 (file)
@@ -1,9 +1,11 @@
 use engine;
 use engine::command::*;
 
+use time::PreciseTime;
+
 use rand::{thread_rng, Rng};
 
-pub fn choose_move(settings: &engine::settings::GameSettings, state: &engine::GameState) -> Command {
+pub fn choose_move(settings: &engine::settings::GameSettings, state: &engine::GameState, _start_time: &PreciseTime) -> Command {
     let mut rng = thread_rng();
     
     if state.player.can_afford_defence_buildings(settings) {