143 lines
3 KiB
Rust
143 lines
3 KiB
Rust
// Eryn Wells <eryn@erynwells.me>
|
|
|
|
use std::fmt;
|
|
|
|
use crate::Square;
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
|
pub enum Color {
|
|
White = 0,
|
|
Black = 1,
|
|
}
|
|
|
|
impl Color {
|
|
pub fn iter() -> impl Iterator<Item = Color> {
|
|
[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() -> impl Iterator<Item = Shape> {
|
|
[
|
|
Shape::Pawn,
|
|
Shape::Knight,
|
|
Shape::Bishop,
|
|
Shape::Rook,
|
|
Shape::Queen,
|
|
Shape::King,
|
|
]
|
|
.into_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',
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct TryFromCharError;
|
|
|
|
impl TryFrom<char> for Shape {
|
|
type Error = TryFromCharError;
|
|
|
|
fn try_from(value: char) -> Result<Self, Self::Error> {
|
|
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(TryFromCharError),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Into<char> for &Shape {
|
|
fn into(self) -> char {
|
|
self._ascii_representation()
|
|
}
|
|
}
|
|
|
|
impl Into<char> 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,
|
|
}
|
|
|
|
#[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) => {
|
|
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);
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
|
pub struct PlacedPiece {
|
|
pub piece: Piece,
|
|
pub square: Square,
|
|
}
|
|
|
|
impl PlacedPiece {
|
|
pub fn new(piece: Piece, square: Square) -> PlacedPiece {
|
|
PlacedPiece { piece, square }
|
|
}
|
|
}
|