From f226635edd1f865065019de731f23840d93e76d9 Mon Sep 17 00:00:00 2001 From: mos Date: Wed, 13 Nov 2024 13:37:33 +0100 Subject: [PATCH] implement display trait for cell --- src/lib.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index efbaec1..868b772 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{} {}", self.0, self.1) @@ -111,17 +127,7 @@ impl std::fmt::Display for Level { if Pos(j, i) == self.player { write!(f, "@")?; } else { - write!( - f, - "{}", - match self[Pos(j, i)] { - Cell::Wall => 'X', - Cell::Box => '*', - Cell::Goal(true) => '!', - Cell::Goal(false) => '.', - Cell::None => ' ', - } - )?; + write!(f, "{}", self[Pos(j, i)])?; } } writeln!(f)?;