diff options
author | Justin Worthe <justin.worthe@gmail.com> | 2017-05-13 21:43:27 +0200 |
---|---|---|
committer | Justin Worthe <justin.worthe@gmail.com> | 2017-05-13 21:43:27 +0200 |
commit | 05ccf5572b39fe254c7b0464cc78a1b623f732c7 (patch) | |
tree | 723d35b3eca744d5739174bfe965df12cd678fe1 | |
parent | 872529bc9a13b1923047a7f9308abaa40eb63d3c (diff) |
Added checking if state is in use before shooting
-rw-r--r-- | src/lib.rs | 15 | ||||
-rw-r--r-- | src/shooting.rs | 14 | ||||
-rw-r--r-- | src/state.rs | 78 |
3 files changed, 99 insertions, 8 deletions
@@ -7,28 +7,31 @@ mod files; mod ships; mod placement; mod shooting; +mod state; use math::*; use files::*; use ships::*; use placement::*; use shooting::*; +use state::*; use std::path::PathBuf; pub fn write_move(working_dir: PathBuf) -> Result<(), String> { - let state = read_file(&working_dir)?; + let state_json = read_file(&working_dir)?; - let is_place_phase = state["Phase"] == 1; - let map_size = state["MapDimension"] - .as_u16() - .ok_or("Did not find the map dimension in the state json file")?; + println!("{}", state_json); + + let is_place_phase = state_json["Phase"] == 1; + let map_size = State::map_size_from_json(&state_json)?; let action = if is_place_phase { place_ships_randomly(map_size) } else { - shoot_randomly(map_size) + let state = State::new(&state_json)?; + shoot_randomly(&state) }; write_action(&working_dir, is_place_phase, action) diff --git a/src/shooting.rs b/src/shooting.rs index 14f2b81..c2ca56e 100644 --- a/src/shooting.rs +++ b/src/shooting.rs @@ -1,6 +1,16 @@ use actions::*; use math::*; +use state::*; -pub fn shoot_randomly(map_size: u16) -> Action { - Action::Shoot(Point::random(map_size)) +pub fn shoot_randomly(state: &State) -> Action { + let mut shot: Point; + + while { + shot = Point::random(state.map_size); + let ref target = state.opponent_map.cells[shot.x as usize][shot.y as usize]; + target.damaged || target.missed + } {} + + + Action::Shoot(shot) } diff --git a/src/state.rs b/src/state.rs new file mode 100644 index 0000000..df66ec9 --- /dev/null +++ b/src/state.rs @@ -0,0 +1,78 @@ +use json; + +pub struct State { + pub map_size: u16, + pub opponent_map: OpponentMap +} + +impl State { + pub fn new(json: &json::JsonValue) -> Result<State, String> { + let map_size = State::map_size_from_json(&json)?; + let ref opponent_map_json = json["OpponentMap"]; + let opponent_map = OpponentMap::new(&opponent_map_json, map_size)?; + + Ok(State { + map_size: map_size, + opponent_map: opponent_map + }) + } + + pub fn map_size_from_json(json: &json::JsonValue) -> Result<u16, String> { + json["MapDimension"] + .as_u16() + .ok_or(String::from("Did not find the map dimension in the state json file")) + } +} + +pub struct OpponentMap { + pub cells: Vec<Vec<Cell>> +} + +impl OpponentMap { + fn new(json: &json::JsonValue, map_size: u16) -> Result<OpponentMap, String> { + let mut cells = Vec::with_capacity(map_size as usize); + for _ in 0..map_size { + let mut row = Vec::with_capacity(map_size as usize); + for _ in 0..map_size { + row.push(Cell::new()); + } + cells.push(row); + } + + for json_cell in json["Cells"].members() { + let x = json_cell["X"] + .as_u16() + .ok_or(String::from("Failed to read X value of opponent map cell in json file"))?; + let y = json_cell["Y"] + .as_u16() + .ok_or(String::from("Failed to read Y value of opponent map cell in json file"))?; + let damaged = json_cell["Damaged"] + .as_bool() + .ok_or(String::from("Failed to read Damaged value of opponent map cell in json file"))?; + let missed = json_cell["Missed"] + .as_bool() + .ok_or(String::from("Failed to read Missed value of opponent map cell in json file"))?; + + cells[x as usize][y as usize].damaged = damaged; + cells[x as usize][y as usize].missed = missed; + } + + Ok(OpponentMap { + cells: cells + }) + } +} + +pub struct Cell { + pub damaged: bool, + pub missed: bool +} + +impl Cell { + fn new() -> Cell { + Cell { + damaged: false, + missed: false + } + } +} |