chessfriend/core/src/pieces.rs

111 lines
2.7 KiB
Rust
Raw Normal View History

// Eryn Wells <eryn@erynwells.me>
mod display;
pub use self::display::{PieceDisplay, PieceDisplayStyle};
2025-05-08 17:37:51 -07:00
2025-06-08 17:34:52 -07:00
use crate::{Color, Shape};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Piece {
2025-05-08 17:37:51 -07:00
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]
pub 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 std::fmt::Display for Piece {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
PieceDisplay {
piece: *self,
style: PieceDisplayStyle::default()
}
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_shape() {
assert_eq!("p".parse(), Ok(Shape::Pawn));
}
#[test]
fn shape_into_char() {
2025-05-08 17:37:51 -07:00
assert_eq!(<Shape as Into<char>>::into(Shape::Pawn), 'P');
}
}