Can get the size of the smallest unsunk boat

This commit is contained in:
Pierre HUBERT 2022-09-19 18:58:32 +02:00
parent dd0ca5941a
commit 7cfd7a4899

View File

@ -194,6 +194,19 @@ impl CurrentGameStatus {
None
}
/// Get the size of the smallest un-sunk boat
pub fn get_size_of_smallest_un_sunk_boat(&self) -> Option<usize> {
let mut boats_size = self.rules.boats_list();
boats_size.sort();
for boat in &self.opponent_map.sunk_boats {
let index = boats_size.iter().position(|b| *b == boat.len)?;
boats_size.remove(index);
}
boats_size.first().cloned()
}
pub fn print_your_map(&self) {
PrintableCurrentGameMapStatus(self.rules.clone(), self.your_map.clone()).print_map()
}
@ -292,4 +305,40 @@ mod test {
assert!(next_fire.is_some());
assert!(possible_next_strikes.contains(&next_fire.unwrap()))
}
#[test]
fn get_size_of_smallest_unsunk_boat_start_of_game() {
let status = CurrentGameStatus::default();
let min_val = *status.rules.boats_list().iter().min().unwrap();
assert_eq!(min_val, status.get_size_of_smallest_un_sunk_boat().unwrap());
}
#[test]
fn get_size_of_smallest_unsunk_boat_bigger_boat_sunk() {
let mut status = CurrentGameStatus::default();
let min_val = *status.rules.boats_list().iter().min().unwrap();
status.opponent_map.sunk_boats.push(BoatPosition {
start: Coordinates::new(0, 0),
len: min_val + 1,
direction: BoatDirection::Left,
});
assert_eq!(min_val, status.get_size_of_smallest_un_sunk_boat().unwrap());
}
#[test]
fn get_size_of_smallest_unsunk_boat_smallest_boat_sunk() {
let mut status = CurrentGameStatus::default();
let mut boats_size = status.rules.boats_list();
boats_size.sort();
status.opponent_map.sunk_boats.push(BoatPosition {
start: Coordinates::new(0, 0),
len: boats_size[0],
direction: BoatDirection::Left,
});
assert_eq!(
boats_size[1],
status.get_size_of_smallest_un_sunk_boat().unwrap()
);
}
}