diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2022-04-19 21:29:11 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2022-04-19 21:29:11 +0200 |
commit | 01a849c6b26afd076989f645c6153d8ce1422281 (patch) | |
tree | a02fbbfbd958b8492dd3dc7bd58b4bb1d8566080 /2018-tower-defence/src/main.rs | |
parent | 59f8b6a8ef1abfd0556cc940ccb64305fcdf2968 (diff) | |
parent | 7ec48d0d454499177b63bc5bd512a3a2d6baa839 (diff) |
Merge branch 'tower-defence-main'
Diffstat (limited to '2018-tower-defence/src/main.rs')
-rw-r--r-- | 2018-tower-defence/src/main.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/2018-tower-defence/src/main.rs b/2018-tower-defence/src/main.rs new file mode 100644 index 0000000..4fa0366 --- /dev/null +++ b/2018-tower-defence/src/main.rs @@ -0,0 +1,55 @@ +extern crate zombot; +extern crate time; +use time::{PreciseTime, Duration}; + +use zombot::*; +use zombot::engine::constants::*; +use zombot::engine::command::Command; + +use std::error::Error; + +const STATE_PATH: &str = "state.json"; + +const COMMAND_PATH: &str = "command.txt"; + +use std::fs::File; +use std::io::prelude::*; +use std::process; + +fn write_command(filename: &str, command: Command) -> Result<(), Box<Error> > { + let mut file = File::create(filename)?; + write!(file, "{}", command)?; + Ok(()) +} + +fn main() { + let start_time = PreciseTime::now(); + let max_time = Duration::milliseconds(MAX_TIME_MILLIS); + + let state = match input::json::read_bitwise_state_from_file(STATE_PATH) { + Ok(ok) => ok, + Err(error) => { + println!("Error while parsing JSON file: {}", error); + process::exit(1); + } + }; + + let command = if cfg!(feature = "static-opening") && state.round < strategy::static_opening::STATIC_OPENING_LENGTH { + strategy::static_opening::choose_move(&state) + } else if cfg!(feature = "full-monte-carlo-tree") { + strategy::monte_carlo_tree::choose_move(&state, start_time, max_time) + } else { + strategy::monte_carlo::choose_move(&state, start_time, max_time) + }; + + match write_command(COMMAND_PATH, command) { + Ok(()) => {} + Err(error) => { + println!("Error while writing command file: {}", error); + process::exit(1); + } + } + + println!("Elapsed time: {}", start_time.to(PreciseTime::now())); +} + |