Show invalid boats in red
This commit is contained in:
@ -276,6 +276,39 @@ impl BoatsLayout {
|
||||
true
|
||||
}
|
||||
|
||||
/// Check a boat already present in the layout has a valid position
|
||||
pub fn check_present_boat_position(&self, boat: usize, rules: &GameRules) -> Vec<&'static str> {
|
||||
let mut errors = vec![];
|
||||
|
||||
// Check boat coordinates
|
||||
let boat_i_coordinates = self.0[boat].all_coordinates();
|
||||
if boat_i_coordinates.iter().any(|c| !c.is_valid(rules)) {
|
||||
errors.push("A boat goes outside the game map!");
|
||||
}
|
||||
|
||||
for boat_j in 0..self.0.len() {
|
||||
if boat == boat_j {
|
||||
continue;
|
||||
}
|
||||
|
||||
let boat_j_coords = self.0[boat_j].all_coordinates();
|
||||
if boat_i_coordinates.iter().any(|c| boat_j_coords.contains(c)) {
|
||||
errors.push("A collision between two boats has been detected!");
|
||||
}
|
||||
|
||||
if !rules.boats_can_touch
|
||||
&& self.0[boat]
|
||||
.neighbor_coordinates(rules)
|
||||
.iter()
|
||||
.any(|c| boat_j_coords.contains(c))
|
||||
{
|
||||
errors.push("Two boats are touching!");
|
||||
}
|
||||
}
|
||||
|
||||
errors
|
||||
}
|
||||
|
||||
/// Check if this boats layout is valid or not
|
||||
pub fn errors(&self, rules: &GameRules) -> Vec<&'static str> {
|
||||
let mut errors = vec![];
|
||||
@ -296,31 +329,7 @@ impl BoatsLayout {
|
||||
}
|
||||
|
||||
for boat_i in 0..self.0.len() {
|
||||
// Check boat coordinates
|
||||
let boat_i_coordinates = self.0[boat_i].all_coordinates();
|
||||
if boat_i_coordinates.iter().any(|c| !c.is_valid(rules)) {
|
||||
errors.push("A boat goes outside the game map!");
|
||||
}
|
||||
|
||||
for boat_j in 0..self.0.len() {
|
||||
if boat_i == boat_j {
|
||||
continue;
|
||||
}
|
||||
|
||||
let boat_j_coords = self.0[boat_j].all_coordinates();
|
||||
if boat_i_coordinates.iter().any(|c| boat_j_coords.contains(c)) {
|
||||
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))
|
||||
{
|
||||
errors.push("Two boats are touching!");
|
||||
}
|
||||
}
|
||||
errors.append(&mut self.check_present_boat_position(boat_i, rules));
|
||||
}
|
||||
|
||||
errors
|
||||
@ -330,6 +339,11 @@ impl BoatsLayout {
|
||||
self.errors(rules).is_empty()
|
||||
}
|
||||
|
||||
/// Get the list of boats
|
||||
pub fn boats(&self) -> &[BoatPosition] {
|
||||
&self.0
|
||||
}
|
||||
|
||||
pub fn find_boat_at_position(&self, pos: Coordinates) -> Option<&BoatPosition> {
|
||||
self.0.iter().find(|f| f.all_coordinates().contains(&pos))
|
||||
}
|
||||
|
Reference in New Issue
Block a user