From 3b8b6b36e37b5b042ea5f4507c44ddcd2b236694 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sat, 10 Feb 2024 18:33:29 -0700 Subject: [PATCH] Implement a basic Display for chessfriend_moves::Move --- moves/src/moves.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/moves/src/moves.rs b/moves/src/moves.rs index ab0c0da..f4f9b21 100644 --- a/moves/src/moves.rs +++ b/moves/src/moves.rs @@ -89,6 +89,41 @@ impl Move { fn special(&self) -> u16 { self.0 & 0b11 } + + fn _transfer_char(&self) -> char { + if self.is_capture() || self.is_en_passant() { + 'x' + } else { + '-' + } + } +} + +impl fmt::Display for Move { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if let Some(castle) = self.castle() { + match castle { + Castle::KingSide => return write!(f, "0-0"), + Castle::QueenSide => return write!(f, "0-0-0"), + } + } + + write!( + f, + "{}{}{}", + self.origin_square(), + self._transfer_char(), + self.target_square() + )?; + + if let Some(promotion) = self.promotion() { + write!(f, "={}", promotion)?; + } else if self.is_en_passant() { + write!(f, " e.p.")?; + } + + Ok(()) + } } impl fmt::Debug for Move {