diff options
author | Justin Worthe <justin@worthe-it.co.za> | 2017-07-12 21:21:49 +0200 |
---|---|---|
committer | Justin Worthe <justin@worthe-it.co.za> | 2017-07-12 21:21:49 +0200 |
commit | e09f191ac88fadd02d2ecf4f7a0aea76a19e3d0a (patch) | |
tree | d3cdb09f300cfea7c2b1c5056703cdc7f78b0ee6 /src/actions.rs | |
parent | 7355491e14081c6dff749d6c536e876cfebc5d0d (diff) |
Avoided placing adjacent ships
Diffstat (limited to 'src/actions.rs')
-rw-r--r-- | src/actions.rs | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/src/actions.rs b/src/actions.rs index ae1f6ce..cf0059a 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -45,12 +45,11 @@ impl ShipPlacement { } pub fn valid_placements(placements: &Vec<ShipPlacement>, map_size: u16) -> bool { let mut occupied = HashSet::new(); - let mut individuals_valid = true; - let mut no_overlaps = true; - + + let individuals_valid = placements.iter().all(|p| p.valid(map_size)); + + let mut no_overlaps = true; for placement in placements { - individuals_valid = individuals_valid && placement.valid(map_size); - for i in 0..placement.ship_type.length() as i32 { match placement.point.move_point(placement.direction, i, map_size) { Some(block) => { @@ -62,6 +61,29 @@ impl ShipPlacement { } } } + + //block out the area around the current ship to prevent adjacent ships + for i in 0..placement.ship_type.length() as i32 { + match placement.point.move_point(placement.direction, i, map_size) { + Some(current_block) => { + if let Some(p) = current_block.move_point(Direction::North, 1, map_size) { + occupied.insert(p); + } + if let Some(p) = current_block.move_point(Direction::South, 1, map_size) { + occupied.insert(p); + } + if let Some(p) = current_block.move_point(Direction::East, 1, map_size) { + occupied.insert(p); + } + if let Some(p) = current_block.move_point(Direction::West, 1, map_size) { + occupied.insert(p); + } + }, + None => { + //invalid case here is handled above + } + } + } } individuals_valid && no_overlaps } |