diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2022-04-19 21:28:54 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2022-04-19 21:28:54 +0200 |
commit | 59f8b6a8ef1abfd0556cc940ccb64305fcdf2968 (patch) | |
tree | af1e8424d7704cf7ccab5818c6d2adb915b183f3 /2017-battleships/src/placement.rs | |
parent | dc01a3e25e6c4ef056467e36f876ebd68f3ade22 (diff) | |
parent | a866bde485c7d8bc82820f2def70af7b6c70a066 (diff) |
Merge branch 'battleships-main'
Diffstat (limited to '2017-battleships/src/placement.rs')
-rw-r--r-- | 2017-battleships/src/placement.rs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/2017-battleships/src/placement.rs b/2017-battleships/src/placement.rs new file mode 100644 index 0000000..4740d76 --- /dev/null +++ b/2017-battleships/src/placement.rs @@ -0,0 +1,23 @@ +use actions::*; +use math::*; +use ships::*; + +pub fn place_ships_randomly(map_size: u16) -> Action { + let mut current_placement: Vec<ShipPlacement>; + + while { + current_placement = create_random_placement(map_size); + !ShipPlacement::valid_placements(¤t_placement, map_size) + } {} + Action::PlaceShips(current_placement) +} + +fn create_random_placement(map_size: u16) -> Vec<ShipPlacement> { + vec!( + ShipPlacement::new(Ship::Battleship, Point::random(map_size), Direction::random()), + ShipPlacement::new(Ship::Carrier, Point::random(map_size), Direction::random()), + ShipPlacement::new(Ship::Cruiser, Point::random(map_size), Direction::random()), + ShipPlacement::new(Ship::Destroyer, Point::random(map_size), Direction::random()), + ShipPlacement::new(Ship::Submarine, Point::random(map_size), Direction::random()) + ) +} |