diff options
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 |