diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 67 |
1 files changed, 0 insertions, 67 deletions
diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index 00eaf02..0000000 --- a/src/lib.rs +++ /dev/null @@ -1,67 +0,0 @@ -extern crate json; -extern crate rand; -extern crate serde; -extern crate serde_json; -#[macro_use] -extern crate serde_derive; - -mod actions; -mod math; -mod files; -mod ships; -mod placement; -mod shooting; -mod state; -mod knowledge; - -use actions::*; -use math::*; -use files::*; -use ships::*; -use placement::*; -use shooting::*; -use state::*; -use knowledge::*; - -use std::path::PathBuf; - -pub fn write_move(working_dir: PathBuf) -> Result<(), String> { - let state_json = read_file(&working_dir)?; - - let is_place_phase = state_json["Phase"] == 1; - let map_size = State::map_size_from_json(&state_json)?; - - let (action, knowledge) = if is_place_phase { - placement(map_size) - } - else { - let state = State::new(&state_json)?; - shoot(&state)? - }; - - write_knowledge(&knowledge) - .map_err(|e| format!("Failed to write knowledge to file. Error: {}", e))?; - - write_action(&working_dir, is_place_phase, action) - .map_err(|e| format!("Failed to write action to file. Error: {}", e))?; - - println!("Knowledge:\n{}\n\n", serde_json::to_string(&knowledge).unwrap_or(String::from(""))); - - Ok(()) -} - -fn placement(map_size: u16) -> (Action, Knowledge) { - let action = place_ships_randomly(map_size); - let knowledge = Knowledge::new(map_size, action.clone()); - - (action, knowledge) -} - -fn shoot(state: &State) -> Result<(Action, Knowledge), String> { - let old_knowledge = read_knowledge()?; - let knowledge = old_knowledge.resolve_last_action(&state); - let action = shoot_smartly(&knowledge); - - Ok((action.clone(), knowledge.with_action(action))) -} - |