67 lines
1.4 KiB
Rust
67 lines
1.4 KiB
Rust
use std::fmt::Write;
|
|
use std::string::String;
|
|
|
|
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) {
|
|
print!("{}", self.get_map());
|
|
}
|
|
|
|
fn get_map(&self) -> String {
|
|
let mut out = String::with_capacity(100);
|
|
|
|
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;
|
|
}
|
|
|
|
write!(out, "{} ", content.letter()).unwrap();
|
|
x += 1;
|
|
}
|
|
|
|
out.push('\n');
|
|
|
|
// x == 0 <=> we reached the end of the map
|
|
if x == 0 {
|
|
break;
|
|
}
|
|
|
|
y += 1;
|
|
}
|
|
|
|
out
|
|
}
|
|
}
|