SeaBattle/sea_battle_backend/src/data/printable_map.rs

56 lines
1.2 KiB
Rust
Raw Normal View History

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) {
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;
}
print!("{} ", content.letter());
x += 1;
}
println!();
// x == 0 <=> we reached the end of the map
if x == 0 {
break;
}
y += 1;
}
}
}