This commit is contained in:
Eryn Wells 2025-05-08 17:37:51 -07:00
parent d5cdf273c8
commit 091cc99cb3
42 changed files with 805 additions and 1662 deletions

View file

@ -3,6 +3,34 @@
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,
@ -94,8 +122,8 @@ impl fmt::Display for Shape {
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Piece {
color: Color,
shape: Shape,
pub color: Color,
pub shape: Shape,
}
macro_rules! piece_constructor {
@ -132,16 +160,6 @@ impl Piece {
piece_constructor!(queen, Queen);
piece_constructor!(king, King);
#[must_use]
pub fn color(&self) -> Color {
self.color
}
#[must_use]
pub fn shape(&self) -> Shape {
self.shape
}
is_shape!(is_pawn, Pawn);
is_shape!(is_knight, Knight);
is_shape!(is_bishop, Bishop);
@ -195,10 +213,11 @@ impl From<&PlacedPiece> for Piece {
}
}
#[deprecated]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct PlacedPiece {
piece: Piece,
square: Square,
pub piece: Piece,
pub square: Square,
}
macro_rules! is_shape {
@ -223,27 +242,6 @@ impl PlacedPiece {
self.piece
}
/// The square the piece is on
#[inline]
#[must_use]
pub fn square(&self) -> Square {
self.square
}
/// The piece's [Color]
#[inline]
#[must_use]
pub fn color(&self) -> Color {
self.piece.color
}
/// The piece's [Shape]
#[inline]
#[must_use]
pub fn shape(&self) -> Shape {
self.piece.shape
}
is_shape!(is_pawn, Pawn);
is_shape!(is_knight, Knight);
is_shape!(is_bishop, Bishop);
@ -254,7 +252,7 @@ impl PlacedPiece {
#[must_use]
pub fn is_kingside_rook(&self) -> bool {
self.is_rook()
&& match self.color() {
&& match self.piece.color {
Color::White => self.square == Square::H1,
Color::Black => self.square == Square::H8,
}
@ -263,7 +261,7 @@ impl PlacedPiece {
#[must_use]
pub fn is_queenside_rook(&self) -> bool {
self.is_rook()
&& match self.color() {
&& match self.piece.color {
Color::White => self.square == Square::A1,
Color::Black => self.square == Square::A8,
}
@ -294,6 +292,6 @@ mod tests {
#[test]
fn shape_into_char() {
assert_eq!(<Shape as Into<char>>::into(Shape::Pawn) as char, 'P');
assert_eq!(<Shape as Into<char>>::into(Shape::Pawn), 'P');
}
}