Changed default implementation used by main to be bitwise
[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<GS:engine::GameState>(settings: &engine::settings::GameSettings, state: &GS, start_time: &PreciseTime) -> Command {
19     #[cfg(not(feature = "reduced-time"))]
20     #[cfg(not(feature = "extended-time"))]
21     let max_time = Duration::milliseconds(1950);
22     
23     #[cfg(feature = "reduced-time")]
24     let max_time = Duration::milliseconds(950);
25
26     #[cfg(feature = "extended-time")]
27     let max_time = Duration::milliseconds(19950);
28     
29     strategy::monte_carlo::choose_move(settings, state, start_time, max_time)
30 }
31
32
33 fn write_command(filename: &str, command: Command) -> Result<(), Box<Error> > {
34     let mut file = File::create(filename)?;
35     write!(file, "{}", command)?;
36     Ok(())
37 }
38
39
40 fn main() {
41     let start_time = PreciseTime::now();
42     
43     let (settings, state) = match input::json::read_bitwise_state_from_file(STATE_PATH) {
44         Ok(ok) => ok,
45         Err(error) => {
46             println!("Error while parsing JSON file: {}", error);
47             process::exit(1);
48         }
49     };
50     let command = choose_move(&settings, &state, &start_time);
51
52     match write_command(COMMAND_PATH, command) {
53         Ok(()) => {}
54         Err(error) => {
55             println!("Error while writing command file: {}", error);
56             process::exit(1);
57         }
58     }
59
60     println!("Elapsed time: {}", start_time.to(PreciseTime::now()));
61 }