diff options
author | Justin Worthe <justin@worthe-it.co.za> | 2019-04-22 21:50:00 +0200 |
---|---|---|
committer | Justin Worthe <justin@worthe-it.co.za> | 2019-04-22 21:50:00 +0200 |
commit | 88430f31c73f469086b68f2b77d1e1ba5f9178e7 (patch) | |
tree | 292a0aceba92e4d0c38679ed919b9b463c82152b /src/json.rs | |
parent | 3e54b01003aa9d27de8f4ca13c9240fe785ec0e1 (diff) |
More minimal game state
I'd prefer to start with just the state that I need, and progressively
readd the bits that I've skipped as I find I need them, or as the
competition evolves.
Diffstat (limited to 'src/json.rs')
-rw-r--r-- | src/json.rs | 35 |
1 files changed, 15 insertions, 20 deletions
diff --git a/src/json.rs b/src/json.rs index 3046b7c..5a8267f 100644 --- a/src/json.rs +++ b/src/json.rs @@ -19,7 +19,7 @@ pub fn read_state_from_json_file(filename: &str) -> Result<State, Box<Error>> { pub struct State { pub current_round: u32, pub max_rounds: u32, - pub map_size: u32, + pub map_size: u8, pub current_worm_id: i32, pub consecutive_do_nothing_count: u32, pub my_player: Player, @@ -68,8 +68,8 @@ pub struct OpponentWorm { #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] #[serde(rename_all = "camelCase")] pub struct Cell { - pub x: u32, - pub y: u32, + pub x: i8, + pub y: i8, #[serde(rename = "type")] pub cell_type: CellType, pub occupier: Option<CellWorm>, @@ -126,51 +126,46 @@ pub enum PowerupType { #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] #[serde(rename_all = "camelCase")] pub struct Position { - pub x: u32, - pub y: u32, + pub x: i8, + pub y: i8, } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] #[serde(rename_all = "camelCase")] pub struct Weapon { - pub damage: u32, - pub range: u32, + pub damage: i32, + pub range: u8, } impl State { - pub fn active_worm(&self) -> Option<&PlayerWorm> { + pub fn active_worm_index(&self) -> Option<usize> { self.my_player .worms .iter() - .find(|w| w.id == self.current_worm_id) - } - - pub fn cell_at(&self, pos: &Position) -> Option<&Cell> { - self.map - .iter() - .flatten() - .find(|c| c.x == pos.x && c.y == pos.y) + .position(|w| w.id == self.current_worm_id) } } impl Position { - pub fn west(&self, distance: u32) -> Option<Position> { + pub fn west(&self, distance: i8) -> Option<Position> { self.x .checked_sub(distance) + .filter(|&x| x >= 0) .map(|x| Position { x, y: self.y }) } - pub fn east(&self, distance: u32, max: u32) -> Option<Position> { + pub fn east(&self, distance: i8, max: i8) -> Option<Position> { self.x .checked_add(distance) .filter(|&x| x < max) .map(|x| Position { x, y: self.y }) } - pub fn north(&self, distance: u32) -> Option<Position> { + pub fn north(&self, distance: i8) -> Option<Position> { self.y .checked_sub(distance) + .filter(|&y| y >= 0) .map(|y| Position { x: self.x, y }) } - pub fn south(&self, distance: u32, max: u32) -> Option<Position> { + pub fn south(&self, distance: i8, max: i8) -> Option<Position> { self.y .checked_add(distance) .filter(|&y| y < max) |