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