diff --git a/board/src/display.rs b/board/src/display.rs new file mode 100644 index 0000000..37621a8 --- /dev/null +++ b/board/src/display.rs @@ -0,0 +1,15 @@ +// Eryn Wells + +use std::fmt; + +pub(crate) trait ASCIIDisplay { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result; +} + +pub(crate) trait UnicodeDisplay { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result; +} + +pub(crate) trait FENDisplay { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result; +} diff --git a/board/src/lib.rs b/board/src/lib.rs index 34f38df..76b0de3 100644 --- a/board/src/lib.rs +++ b/board/src/lib.rs @@ -1,6 +1,7 @@ // Eryn Wells mod bitboard; +mod display; mod moves; #[macro_use] pub mod piece; diff --git a/board/src/piece.rs b/board/src/piece.rs index 1732864..7a20d19 100644 --- a/board/src/piece.rs +++ b/board/src/piece.rs @@ -1,6 +1,9 @@ // Eryn Wells -use crate::{bitboard::BitBoard, Square}; +use crate::{ + display::{ASCIIDisplay, FENDisplay, UnicodeDisplay}, + Square, +}; use std::fmt; use std::slice::Iter; @@ -56,7 +59,7 @@ impl Shape { fn _ascii_representation(&self) -> char { match self { - Shape::Pawn => 'p', + Shape::Pawn => 'P', Shape::Knight => 'N', Shape::Bishop => 'B', Shape::Rook => 'R', @@ -168,22 +171,50 @@ impl Piece { impl fmt::Display for Piece { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let char = match (self.color, self.shape) { - (Color::White, Shape::Pawn) => '♟', - (Color::White, Shape::Knight) => '♞', - (Color::White, Shape::Bishop) => '♝', - (Color::White, Shape::Rook) => '♜', - (Color::White, Shape::Queen) => '♛', - (Color::White, Shape::King) => '♚', - (Color::Black, Shape::Pawn) => '♙', - (Color::Black, Shape::Knight) => '♘', - (Color::Black, Shape::Bishop) => '♗', - (Color::Black, Shape::Rook) => '♖', - (Color::Black, Shape::Queen) => '♕', - (Color::Black, Shape::King) => '♔', - }; + UnicodeDisplay::fmt(self, f) + } +} - write!(f, "{}", char) +impl ASCIIDisplay for Piece { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", Into::::into(self.shape())) + } +} + +impl UnicodeDisplay for Piece { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "{}", + match (self.color, self.shape) { + (Color::White, Shape::Pawn) => '♟', + (Color::White, Shape::Knight) => '♞', + (Color::White, Shape::Bishop) => '♝', + (Color::White, Shape::Rook) => '♜', + (Color::White, Shape::Queen) => '♛', + (Color::White, Shape::King) => '♚', + (Color::Black, Shape::Pawn) => '♙', + (Color::Black, Shape::Knight) => '♘', + (Color::Black, Shape::Bishop) => '♗', + (Color::Black, Shape::Rook) => '♖', + (Color::Black, Shape::Queen) => '♕', + (Color::Black, Shape::King) => '♔', + } + ) + } +} + +impl FENDisplay for Piece { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let ascii = self.shape()._ascii_representation(); + write!( + f, + "{}", + match self.color { + Color::White => ascii.to_ascii_uppercase(), + Color::Black => ascii.to_ascii_lowercase(), + } + ) } }