summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: de2940d584516ccceb68fb6fa832b07329a1a5b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::io::prelude::*;
use std::io::stdin;
use std::path::Path;

use time::{Duration, PreciseTime};

use steam_powered_wyrm::command::{Command, Action};
use steam_powered_wyrm::strategy::choose_move;
use steam_powered_wyrm::json;
use steam_powered_wyrm::game;

fn main() {
    let max_time = Duration::milliseconds(950);
    let mut game_board = None;
    let mut strategy_cache = None;
    for line in stdin().lock().lines() {
        let start_time = PreciseTime::now();
        
        let round_number = line.expect("Failed to read line from stdin: {}");
        
        let command =
            match json::read_state_from_json_file(&Path::new(&format!("./rounds/{}/state.json", round_number))) {
                Ok(json_state) => {
                    match &mut game_board {
                        None => {
                            let new_board = game::GameBoard::new(json_state);
                            let (command, cache) = choose_move(&new_board, strategy_cache, &start_time, max_time);
                            strategy_cache = Some(cache);
                            game_board = Some(new_board);
                            command
                        },
                        Some(game_board) => {
                            game_board.update(json_state);
                            let (command, cache) = choose_move(&game_board, strategy_cache, &start_time, max_time);
                            strategy_cache = Some(cache);
                            command
                        }
                    }
                },
                Err(e) => {
                    eprintln!("WARN: State file could not be parsed: {}", e);
                    Command::new(Action::DoNothing)
                }
            };
        println!("C;{};{}", round_number, command);
    }
}