diff options
author | Justin Worthe <justin.worthe@gmail.com> | 2017-05-14 21:49:48 +0200 |
---|---|---|
committer | Justin Worthe <justin.worthe@gmail.com> | 2017-05-14 21:49:48 +0200 |
commit | 10c8ceb168e86a58e38086691ddd519bac63ff03 (patch) | |
tree | a40b433e7cfad492a60b37c5a337758ffe7d1786 /src/state.rs | |
parent | 25a551316e27f4cc52c160d099db9cc3673b3421 (diff) |
Added model for knowledge of the game's state
Will be useful to track deductions that have already been made.
Diffstat (limited to 'src/state.rs')
-rw-r--r-- | src/state.rs | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/src/state.rs b/src/state.rs index df66ec9..8c176e1 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,4 +1,6 @@ use json; +use std::collections::HashMap; +use ships::*; pub struct State { pub map_size: u16, @@ -25,7 +27,8 @@ impl State { } pub struct OpponentMap { - pub cells: Vec<Vec<Cell>> + pub cells: Vec<Vec<Cell>>, + pub ships: HashMap<Ship, OpponentShip>, } impl OpponentMap { @@ -38,7 +41,7 @@ impl OpponentMap { } cells.push(row); } - + for json_cell in json["Cells"].members() { let x = json_cell["X"] .as_u16() @@ -56,13 +59,34 @@ impl OpponentMap { cells[x as usize][y as usize].damaged = damaged; cells[x as usize][y as usize].missed = missed; } + + let mut ships = HashMap::new(); + for json_ship in json["Ships"].members() { + let ship_type_string = json_ship["ShipType"] + .as_str() + .ok_or(String::from("Failed to read ShipType value of opponent map ship in json file"))?; + let ship_type = ship_type_string.parse::<Ship>()?; + + let destroyed = json_ship["Destroyed"] + .as_bool() + .ok_or(String::from("Failed to read Destroyed value of opponent map ship in json file"))?; + ships.insert(ship_type, OpponentShip { + destroyed: destroyed + }); + } + Ok(OpponentMap { - cells: cells + cells: cells, + ships: ships }) } } +pub struct OpponentShip { + pub destroyed: bool +} + pub struct Cell { pub damaged: bool, pub missed: bool |