Added reduced time flag
[entelect-challenge-tower-defence.git] / src / main.rs
1 extern crate zombot;
2 extern crate time;
3 use time::{PreciseTime, Duration};
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     #[cfg(not(feature = "reduced-time"))] let max_time = Duration::milliseconds(1950);
20     #[cfg(feature = "reduced-time")] let max_time = Duration::milliseconds(950);
21     
22     strategy::monte_carlo::choose_move(settings, state, start_time, max_time)
23 }
24
25
26 fn write_command(filename: &str, command: Command) -> Result<(), Box<Error> > {
27     let mut file = File::create(filename)?;
28     write!(file, "{}", command)?;
29     Ok(())
30 }
31
32
33 fn main() {
34     let start_time = PreciseTime::now();
35     
36     println!("Reading in state.json file");
37     let (settings, state) = match input::json::read_state_from_file(STATE_PATH) {
38         Ok(ok) => ok,
39         Err(error) => {
40             println!("Error while parsing JSON file: {}", error);
41             process::exit(1);
42         }
43     };
44     let command = choose_move(&settings, &state, &start_time);
45
46     match write_command(COMMAND_PATH, command) {
47         Ok(()) => {}
48         Err(error) => {
49             println!("Error while writing command file: {}", error);
50             process::exit(1);
51         }
52     }
53
54     println!("Elapsed time: {}", start_time.to(PreciseTime::now()));
55 }