Limited bot to run within the 2 second window
[entelect-challenge-tower-defence.git] / src / main.rs
1 extern crate zombot;
2 extern crate time;
3 use time::PreciseTime;
4
5 use zombot::*;
6 use zombot::engine::command::Command;
7
8 use std::error::Error;
9
10 const STATE_PATH: &str = "state.json";
11
12 const COMMAND_PATH: &str = "command.txt";
13
14 use std::fs::File;
15 use std::io::prelude::*;
16 use std::process;
17
18 fn choose_move(settings: &engine::settings::GameSettings, state: &engine::GameState, start_time: &PreciseTime) -> Command {
19     strategy::monte_carlo::choose_move(settings, state, start_time)
20 }
21
22
23 fn write_command(filename: &str, command: Command) -> Result<(), Box<Error> > {
24     let mut file = File::create(filename)?;
25     write!(file, "{}", command)?;
26     Ok(())
27 }
28
29
30 fn main() {
31     let start_time = PreciseTime::now();
32     
33     println!("Reading in state.json file");
34     let (settings, state) = match json::read_state_from_file(STATE_PATH) {
35         Ok(ok) => ok,
36         Err(error) => {
37             println!("Error while parsing JSON file: {}", error);
38             process::exit(1);
39         }
40     };
41     let command = choose_move(&settings, &state, &start_time);
42
43     match write_command(COMMAND_PATH, command) {
44         Ok(()) => {}
45         Err(error) => {
46             println!("Error while writing command file: {}", error);
47             process::exit(1);
48         }
49     }
50
51     println!("Elapsed time: {}", start_time.to(PreciseTime::now()));
52 }