[core] Move the Unicode piece table to a helper to_unicode() method on Piece

This commit is contained in:
Eryn Wells 2024-02-11 08:45:19 -07:00
parent 742b00119a
commit 0201668563

View file

@ -136,30 +136,34 @@ impl Piece {
is_shape!(is_king, King);
pub fn to_ascii(&self) -> char {
self.shape.to_ascii()
let ch = self.shape.to_ascii();
match self.color {
Color::White => ch,
Color::Black => ch.to_ascii_lowercase(),
}
}
fn to_unicode(&self) -> char {
match (self.color, self.shape) {
(Color::Black, Shape::Pawn) => '♟',
(Color::Black, Shape::Knight) => '♞',
(Color::Black, Shape::Bishop) => '♝',
(Color::Black, Shape::Rook) => '♜',
(Color::Black, Shape::Queen) => '♛',
(Color::Black, Shape::King) => '♚',
(Color::White, Shape::Pawn) => '♙',
(Color::White, Shape::Knight) => '♘',
(Color::White, Shape::Bishop) => '♗',
(Color::White, Shape::Rook) => '♖',
(Color::White, Shape::Queen) => '♕',
(Color::White, Shape::King) => '♔',
}
}
}
impl fmt::Display for Piece {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match (self.color, self.shape) {
(Color::Black, Shape::Pawn) => '♟',
(Color::Black, Shape::Knight) => '♞',
(Color::Black, Shape::Bishop) => '♝',
(Color::Black, Shape::Rook) => '♜',
(Color::Black, Shape::Queen) => '♛',
(Color::Black, Shape::King) => '♚',
(Color::White, Shape::Pawn) => '♙',
(Color::White, Shape::Knight) => '♘',
(Color::White, Shape::Bishop) => '♗',
(Color::White, Shape::Rook) => '♖',
(Color::White, Shape::Queen) => '♕',
(Color::White, Shape::King) => '♔',
}
)
write!(f, "{}", self.to_unicode())
}
}