chessfriend/core/src/pieces.rs
2025-05-08 17:37:51 -07:00

297 lines
6.9 KiB
Rust

// Eryn Wells <eryn@erynwells.me>
use crate::{errors::TryFromCharError, try_from_string, Color, Square};
use std::{array, fmt, slice};
trait _Shape {
fn symbol(&self) -> char;
fn index(&self) -> usize;
}
macro_rules! shape {
($name:ident, $index:expr, $symbol:expr) => {
struct $name;
impl _Shape for $name {
fn symbol(&self) -> char {
$symbol
}
fn index(&self) -> usize {
$index
}
}
};
}
shape!(Pawn, 0, 'P');
shape!(Knight, 1, 'K');
shape!(Bishop, 2, 'B');
shape!(Rook, 3, 'R');
shape!(Queen, 4, 'Q');
shape!(King, 5, 'K');
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Shape {
Pawn = 0,
Knight = 1,
Bishop = 2,
Rook = 3,
Queen = 4,
King = 5,
}
impl Shape {
/// Number of piece shapes
pub const NUM: usize = 6;
/// A slice of all piece shapes
pub const ALL: [Shape; Self::NUM] = [
Shape::Pawn,
Shape::Knight,
Shape::Bishop,
Shape::Rook,
Shape::Queen,
Shape::King,
];
pub fn iter() -> slice::Iter<'static, Self> {
Shape::ALL.iter()
}
pub fn into_iter() -> array::IntoIter<Self, { Self::NUM }> {
Shape::ALL.into_iter()
}
/// An iterator over the shapes that a pawn can promote to
pub fn promotable() -> slice::Iter<'static, Shape> {
const PROMOTABLE_SHAPES: [Shape; 4] =
[Shape::Queen, Shape::Rook, Shape::Bishop, Shape::Knight];
PROMOTABLE_SHAPES.iter()
}
const fn to_ascii(self) -> char {
match self {
Shape::Pawn => 'P',
Shape::Knight => 'N',
Shape::Bishop => 'B',
Shape::Rook => 'R',
Shape::Queen => 'Q',
Shape::King => 'K',
}
}
}
impl TryFrom<char> for Shape {
type Error = TryFromCharError;
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
'P' | 'p' => Ok(Shape::Pawn),
'N' | 'n' => Ok(Shape::Knight),
'B' | 'b' => Ok(Shape::Bishop),
'R' | 'r' => Ok(Shape::Rook),
'Q' | 'q' => Ok(Shape::Queen),
'K' | 'k' => Ok(Shape::King),
_ => Err(TryFromCharError),
}
}
}
try_from_string!(Shape);
impl From<&Shape> for char {
fn from(shape: &Shape) -> char {
char::from(*shape)
}
}
impl From<Shape> for char {
fn from(shape: Shape) -> char {
shape.to_ascii()
}
}
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(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Piece {
pub color: Color,
pub shape: Shape,
}
macro_rules! piece_constructor {
($func_name:ident, $type:tt) => {
#[must_use]
pub fn $func_name(color: Color) -> Piece {
Piece {
color,
shape: Shape::$type,
}
}
};
}
macro_rules! is_shape {
($func_name:ident, $shape:ident) => {
#[must_use]
pub fn $func_name(&self) -> bool {
self.shape == Shape::$shape
}
};
}
impl Piece {
#[must_use]
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);
is_shape!(is_pawn, Pawn);
is_shape!(is_knight, Knight);
is_shape!(is_bishop, Bishop);
is_shape!(is_rook, Rook);
is_shape!(is_queen, Queen);
is_shape!(is_king, King);
#[must_use]
pub fn to_ascii(self) -> char {
let ch = self.shape.to_ascii();
match self.color {
Color::White => ch,
Color::Black => ch.to_ascii_lowercase(),
}
}
#[must_use]
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, "{}", self.to_unicode())
}
}
impl From<PlacedPiece> for Piece {
fn from(value: PlacedPiece) -> Self {
value.piece
}
}
impl From<&PlacedPiece> for Piece {
fn from(value: &PlacedPiece) -> Self {
value.piece
}
}
#[deprecated]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct PlacedPiece {
pub piece: Piece,
pub square: Square,
}
macro_rules! is_shape {
($func_name:ident, $shape:ident) => {
#[must_use]
pub fn $func_name(&self) -> bool {
self.piece().shape == Shape::$shape
}
};
}
impl PlacedPiece {
#[must_use]
pub const fn new(piece: Piece, square: Square) -> PlacedPiece {
PlacedPiece { piece, square }
}
/// The [Piece] itself
#[inline]
#[must_use]
pub fn piece(&self) -> Piece {
self.piece
}
is_shape!(is_pawn, Pawn);
is_shape!(is_knight, Knight);
is_shape!(is_bishop, Bishop);
is_shape!(is_rook, Rook);
is_shape!(is_queen, Queen);
is_shape!(is_king, King);
#[must_use]
pub fn is_kingside_rook(&self) -> bool {
self.is_rook()
&& match self.piece.color {
Color::White => self.square == Square::H1,
Color::Black => self.square == Square::H8,
}
}
#[must_use]
pub fn is_queenside_rook(&self) -> bool {
self.is_rook()
&& match self.piece.color {
Color::White => self.square == Square::A1,
Color::Black => self.square == Square::A8,
}
}
}
impl fmt::Display for PlacedPiece {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}{}", self.piece, self.square)
}
}
impl From<(&Square, &Piece)> for PlacedPiece {
fn from(value: (&Square, &Piece)) -> Self {
PlacedPiece::new(*value.1, *value.0)
}
}
#[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!(<Shape as Into<char>>::into(Shape::Pawn), 'P');
}
}