cargo fmt

This commit is contained in:
Pierre HUBERT 2022-09-13 17:26:40 +02:00
parent e17b64a416
commit b36f2d4f20
2 changed files with 24 additions and 16 deletions

View File

@ -71,7 +71,7 @@ impl BoatDirection {
} }
#[derive( #[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 { pub struct Coordinates {
y: i32, y: i32,
@ -80,8 +80,8 @@ pub struct Coordinates {
impl Coordinates { impl Coordinates {
pub fn new<E>(x: E, y: E) -> Self pub fn new<E>(x: E, y: E) -> Self
where where
E: Into<i32>, E: Into<i32>,
{ {
Self { Self {
x: x.into(), x: x.into(),
@ -164,8 +164,11 @@ impl BoatsLayout {
break; break;
} }
if attempt >= rules.map_width * rules.map_height { if attempt >= rules.map_width * rules.map_height * 4 {
return Err(std::io::Error::new(ErrorKind::Other, "Un-usable game rules!")); 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 // Check if the boat touch another boat in a configuration where is it forbidden
if !rules.boats_can_touch if !rules.boats_can_touch
&& pos && pos
.neighbor_coordinates(&rules) .neighbor_coordinates(&rules)
.iter() .iter()
.any(|c| self.find_boat_at_position(*c).is_some()) .any(|c| self.find_boat_at_position(*c).is_some())
{ {
return false; return false;
} }
@ -210,9 +213,7 @@ impl BoatsLayout {
} }
// Check the length of the boats // Check the length of the boats
let mut len = self.0.iter() let mut len = self.0.iter().map(|l| l.len).collect::<Vec<_>>();
.map(|l| l.len)
.collect::<Vec<_>>();
len.sort(); len.sort();
let mut boats = rules.boats_list(); let mut boats = rules.boats_list();
boats.sort(); boats.sort();
@ -238,14 +239,17 @@ impl BoatsLayout {
errors.push("A collision between two boats has been detected!"); errors.push("A collision between two boats has been detected!");
} }
if !rules.boats_can_touch && self.0[boat_i].neighbor_coordinates(&rules).iter() if !rules.boats_can_touch
.any(|c| boat_j_coords.contains(c)) { && 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.push("A collision between two boats has been detected!");
} }
} }
} }
errors errors
} }
@ -256,9 +260,9 @@ impl BoatsLayout {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::data::{BotType, GameRules, PlayConfiguration};
use crate::data::boats_layout::{BoatDirection, BoatPosition, BoatsLayout, Coordinates}; use crate::data::boats_layout::{BoatDirection, BoatPosition, BoatsLayout, Coordinates};
use crate::data::game_map::GameMap; use crate::data::game_map::GameMap;
use crate::data::{BotType, GameRules, PlayConfiguration};
use crate::game::Game; use crate::game::Game;
#[test] #[test]

View File

@ -29,7 +29,11 @@ impl GameRules {
/// Set the list of boats for this configuration /// Set the list of boats for this configuration
pub fn set_boats_list(&mut self, boats: &[usize]) { pub fn set_boats_list(&mut self, boats: &[usize]) {
self.boats_str = boats.iter().map(usize::to_string).collect::<Vec<_>>().join(","); self.boats_str = boats
.iter()
.map(usize::to_string)
.collect::<Vec<_>>()
.join(",");
} }
/// Get the list of boats for this configuration /// Get the list of boats for this configuration