diff --git a/src/data/boats_layout.rs b/src/data/boats_layout.rs index 181f8c3..75e8eb1 100644 --- a/src/data/boats_layout.rs +++ b/src/data/boats_layout.rs @@ -71,7 +71,7 @@ impl BoatDirection { } #[derive( -serde::Serialize, serde::Deserialize, Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, + serde::Serialize, serde::Deserialize, Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, )] pub struct Coordinates { y: i32, @@ -80,8 +80,8 @@ pub struct Coordinates { impl Coordinates { pub fn new(x: E, y: E) -> Self - where - E: Into, + where + E: Into, { Self { x: x.into(), @@ -164,8 +164,11 @@ impl BoatsLayout { break; } - if attempt >= rules.map_width * rules.map_height { - return Err(std::io::Error::new(ErrorKind::Other, "Un-usable game rules!")); + if attempt >= rules.map_width * rules.map_height * 4 { + return Err(std::io::Error::new( + ErrorKind::Other, + "Un-usable game rules!", + )); } } } @@ -191,9 +194,9 @@ impl BoatsLayout { // Check if the boat touch another boat in a configuration where is it forbidden if !rules.boats_can_touch && pos - .neighbor_coordinates(&rules) - .iter() - .any(|c| self.find_boat_at_position(*c).is_some()) + .neighbor_coordinates(&rules) + .iter() + .any(|c| self.find_boat_at_position(*c).is_some()) { return false; } @@ -210,9 +213,7 @@ impl BoatsLayout { } // Check the length of the boats - let mut len = self.0.iter() - .map(|l| l.len) - .collect::>(); + let mut len = self.0.iter().map(|l| l.len).collect::>(); len.sort(); let mut boats = rules.boats_list(); boats.sort(); @@ -238,14 +239,17 @@ impl BoatsLayout { errors.push("A collision between two boats has been detected!"); } - if !rules.boats_can_touch && self.0[boat_i].neighbor_coordinates(&rules).iter() - .any(|c| boat_j_coords.contains(c)) { + if !rules.boats_can_touch + && self.0[boat_i] + .neighbor_coordinates(&rules) + .iter() + .any(|c| boat_j_coords.contains(c)) + { errors.push("A collision between two boats has been detected!"); } } } - errors } @@ -256,9 +260,9 @@ impl BoatsLayout { #[cfg(test)] mod test { - use crate::data::{BotType, GameRules, PlayConfiguration}; use crate::data::boats_layout::{BoatDirection, BoatPosition, BoatsLayout, Coordinates}; use crate::data::game_map::GameMap; + use crate::data::{BotType, GameRules, PlayConfiguration}; use crate::game::Game; #[test] diff --git a/src/data/game_rules.rs b/src/data/game_rules.rs index e15608d..64d9a1e 100644 --- a/src/data/game_rules.rs +++ b/src/data/game_rules.rs @@ -29,7 +29,11 @@ impl GameRules { /// Set the list of boats for this configuration pub fn set_boats_list(&mut self, boats: &[usize]) { - self.boats_str = boats.iter().map(usize::to_string).collect::>().join(","); + self.boats_str = boats + .iter() + .map(usize::to_string) + .collect::>() + .join(","); } /// Get the list of boats for this configuration