implement display trait for cell

This commit is contained in:
mos 2024-11-13 13:37:33 +01:00
parent 41af614d74
commit f226635edd
1 changed files with 17 additions and 11 deletions

View File

@ -57,6 +57,22 @@ impl Sub for Pos {
} }
} }
impl std::fmt::Display for Cell {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Cell::Wall => 'X',
Cell::Box => '*',
Cell::Goal(true) => '!',
Cell::Goal(false) => '.',
Cell::None => ' ',
}
)
}
}
impl std::fmt::Display for Pos { impl std::fmt::Display for Pos {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {}", self.0, self.1) write!(f, "{} {}", self.0, self.1)
@ -111,17 +127,7 @@ impl std::fmt::Display for Level {
if Pos(j, i) == self.player { if Pos(j, i) == self.player {
write!(f, "@")?; write!(f, "@")?;
} else { } else {
write!( write!(f, "{}", self[Pos(j, i)])?;
f,
"{}",
match self[Pos(j, i)] {
Cell::Wall => 'X',
Cell::Box => '*',
Cell::Goal(true) => '!',
Cell::Goal(false) => '.',
Cell::None => ' ',
}
)?;
} }
} }
writeln!(f)?; writeln!(f)?;