// Eryn Wells use crate::{ display::{ASCIIDisplay, FENDisplay, UnicodeDisplay}, Square, }; use std::fmt; use std::slice::Iter; #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub enum Color { White = 0, Black = 1, } impl Color { pub fn iter() -> impl Iterator { [Color::White, Color::Black].into_iter() } pub fn other(&self) -> Color { match self { Color::White => Color::Black, Color::Black => Color::White, } } } #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub enum Shape { Pawn = 0, Knight = 1, Bishop = 2, Rook = 3, Queen = 4, King = 5, } impl Shape { pub fn iter() -> Iter<'static, Shape> { const ALL_SHAPES: [Shape; 6] = [ Shape::Pawn, Shape::Knight, Shape::Bishop, Shape::Rook, Shape::Queen, Shape::King, ]; ALL_SHAPES.iter() } pub fn promotable() -> Iter<'static, Shape> { const PROMOTABLE_SHAPES: [Shape; 4] = [Shape::Queen, Shape::Rook, Shape::Bishop, Shape::Knight]; PROMOTABLE_SHAPES.iter() } fn _ascii_representation(&self) -> char { match self { Shape::Pawn => 'P', Shape::Knight => 'N', Shape::Bishop => 'B', Shape::Rook => 'R', Shape::Queen => 'Q', Shape::King => 'K', } } } #[derive(Debug, Eq, PartialEq)] pub struct TryFromError; impl TryFrom for Shape { type Error = TryFromError; fn try_from(value: char) -> Result { match value { 'p' => Ok(Shape::Pawn), 'N' => Ok(Shape::Knight), 'B' => Ok(Shape::Bishop), 'R' => Ok(Shape::Rook), 'Q' => Ok(Shape::Queen), 'K' => Ok(Shape::King), _ => Err(TryFromError), } } } impl TryFrom<&str> for Shape { type Error = TryFromError; fn try_from(value: &str) -> Result { let first_char = value.chars().nth(0).ok_or(TryFromError)?; Shape::try_from(first_char) } } impl Into for &Shape { fn into(self) -> char { self._ascii_representation() } } impl Into for Shape { fn into(self) -> char { self._ascii_representation() } } impl fmt::Display for Shape { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let self_char: char = self.into(); write!(f, "{}", self_char) } } #[derive(Debug, Eq, PartialEq)] pub enum PiecePlacementError { ExistsOnSquare, } #[macro_export] macro_rules! piece { ($color:ident $shape:ident) => { Piece::new(Color::$color, Shape::$shape) }; ($color:ident $shape:ident on $square:ident) => { PlacedPiece::new(piece!($color $shape), Square::$square) } } #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub struct Piece { color: Color, shape: Shape, } macro_rules! piece_constructor { ($func_name:ident, $type:tt) => { pub fn $func_name(color: Color) -> Piece { Piece { color, shape: Shape::$type, } } }; } impl Piece { pub fn new(color: Color, shape: Shape) -> Piece { Piece { color, shape } } piece_constructor!(pawn, Pawn); piece_constructor!(knight, Knight); piece_constructor!(bishop, Bishop); piece_constructor!(rook, Rook); piece_constructor!(queen, Queen); piece_constructor!(king, King); pub fn color(&self) -> Color { self.color } pub fn shape(&self) -> Shape { self.shape } } impl fmt::Display for Piece { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { UnicodeDisplay::fmt(self, f) } } 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(), } ) } } #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub struct PlacedPiece { piece: Piece, square: Square, } impl PlacedPiece { pub const fn new(piece: Piece, square: Square) -> PlacedPiece { PlacedPiece { piece, square } } #[inline] pub fn piece(&self) -> Piece { self.piece } #[inline] pub fn square(&self) -> Square { self.square } } #[cfg(test)] mod tests { use super::*; #[test] fn shape_try_from() { assert_eq!(Shape::try_from('p'), Ok(Shape::Pawn)); assert_eq!(Shape::try_from("p"), Ok(Shape::Pawn)); } #[test] fn shape_into_char() { assert_eq!(>::into(Shape::Pawn) as char, 'p'); } }