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