Clippy-suggested edits
[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::constants::*;
7 use zombot::engine::command::Command;
8
9 use std::error::Error;
10
11 const STATE_PATH: &str = "state.json";
12
13 const COMMAND_PATH: &str = "command.txt";
14
15 use std::fs::File;
16 use std::io::prelude::*;
17 use std::process;
18
19 fn write_command(filename: &str, command: Command) -> Result<(), Box<Error> > {
20     let mut file = File::create(filename)?;
21     write!(file, "{}", command)?;
22     Ok(())
23 }
24
25
26 fn main() {
27     let start_time = PreciseTime::now();
28     let max_time = Duration::milliseconds(MAX_TIME_MILLIS);
29     
30     let state = match input::json::read_bitwise_state_from_file(STATE_PATH) {
31         Ok(ok) => ok,
32         Err(error) => {
33             println!("Error while parsing JSON file: {}", error);
34             process::exit(1);
35         }
36     };
37     let command = strategy::monte_carlo::choose_move(&state, start_time, max_time);
38
39     match write_command(COMMAND_PATH, command) {
40         Ok(()) => {}
41         Err(error) => {
42             println!("Error while writing command file: {}", error);
43             process::exit(1);
44         }
45     }
46
47     println!("Elapsed time: {}", start_time.to(PreciseTime::now()));
48 }