2022-09-19 17:29:11 +00:00
|
|
|
use std::fmt::Write;
|
|
|
|
use std::string::String;
|
|
|
|
|
2022-09-17 10:02:13 +00:00
|
|
|
use crate::data::Coordinates;
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
|
|
|
|
pub enum MapCellContent {
|
|
|
|
Invalid,
|
|
|
|
Nothing,
|
|
|
|
TouchedBoat,
|
|
|
|
SunkBoat,
|
|
|
|
Boat,
|
|
|
|
FailedStrike,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MapCellContent {
|
|
|
|
pub fn letter(&self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
MapCellContent::Invalid => "!",
|
|
|
|
MapCellContent::Nothing => ".",
|
|
|
|
MapCellContent::TouchedBoat => "T",
|
|
|
|
MapCellContent::SunkBoat => "S",
|
|
|
|
MapCellContent::Boat => "B",
|
|
|
|
MapCellContent::FailedStrike => "x",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait PrintableMap {
|
|
|
|
fn map_cell_content(&self, c: Coordinates) -> MapCellContent;
|
|
|
|
|
|
|
|
fn print_map(&self) {
|
2022-09-19 17:29:11 +00:00
|
|
|
print!("{}", self.get_map());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_map(&self) -> String {
|
|
|
|
let mut out = String::with_capacity(100);
|
|
|
|
|
2022-09-17 10:02:13 +00:00
|
|
|
let mut y = 0;
|
|
|
|
let mut x;
|
|
|
|
loop {
|
|
|
|
x = 0;
|
|
|
|
loop {
|
|
|
|
let content = self.map_cell_content(Coordinates::new(x, y));
|
|
|
|
|
|
|
|
if content == MapCellContent::Invalid {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-19 17:29:11 +00:00
|
|
|
write!(out, "{} ", content.letter()).unwrap();
|
2022-09-17 10:02:13 +00:00
|
|
|
x += 1;
|
|
|
|
}
|
|
|
|
|
2022-09-19 17:29:11 +00:00
|
|
|
out.push('\n');
|
2022-09-17 10:02:13 +00:00
|
|
|
|
|
|
|
// x == 0 <=> we reached the end of the map
|
|
|
|
if x == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
y += 1;
|
|
|
|
}
|
2022-09-19 17:29:11 +00:00
|
|
|
|
|
|
|
out
|
2022-09-17 10:02:13 +00:00
|
|
|
}
|
|
|
|
}
|