This commit is contained in:
Timothy Warren 2022-07-15 15:41:42 -04:00
parent 04c8452520
commit da39b2fc9c
1 changed files with 68 additions and 11 deletions

View File

@ -15,7 +15,7 @@ const BLACK_KING: char = '♚';
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)] #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
#[rustfmt::skip] #[rustfmt::skip]
enum BoardPosition { pub enum BoardPosition {
A1, A2, A3, A4, A5, A6, A7, A8, A1, A2, A3, A4, A5, A6, A7, A8,
B1, B2, B3, B4, B5, B6, B7, B8, B1, B2, B3, B4, B5, B6, B7, B8,
C1, C2, C3, C4, C5, C6, C7, C8, C1, C2, C3, C4, C5, C6, C7, C8,
@ -103,7 +103,7 @@ impl From<BoardPosition> for usize {
pub enum MoveAmount { pub enum MoveAmount {
One, One,
Two, Two,
Many Many,
} }
#[derive(Debug, Clone, PartialEq, Hash, Eq)] #[derive(Debug, Clone, PartialEq, Hash, Eq)]
@ -118,8 +118,6 @@ pub enum MoveType {
Promoted(PieceType), Promoted(PieceType),
} }
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, PartialEq)]
pub enum PieceColor { pub enum PieceColor {
Black, Black,
@ -153,7 +151,7 @@ pub struct Piece {
symbol: char, symbol: char,
} }
impl<'a> Piece { impl Piece {
pub fn new(color: PieceColor, kind: PieceType) -> Self { pub fn new(color: PieceColor, kind: PieceType) -> Self {
Self { Self {
kind: kind.clone(), kind: kind.clone(),
@ -191,16 +189,69 @@ impl<'a> Piece {
match kind { match kind {
Pawn(Some(k)) => Self::valid_move_types(*k), Pawn(Some(k)) => Self::valid_move_types(*k),
Pawn(None) => HashSet::from([MoveType::Diagonal(One), MoveType::Vertical(One), MoveType::Vertical(Two), MoveType::EnPassant]), Pawn(None) => HashSet::from([
MoveType::Diagonal(One),
MoveType::Vertical(One),
MoveType::Vertical(Two),
MoveType::EnPassant,
]),
Knight => HashSet::from([MoveType::Knight]), Knight => HashSet::from([MoveType::Knight]),
Bishop(_) => HashSet::from([MoveType::Diagonal(Many)]), Bishop(_) => HashSet::from([MoveType::Diagonal(Many)]),
Rook => HashSet::from([MoveType::Horizontal(Many), MoveType::Vertical(Many), MoveType::KingSideCastle, MoveType::QueenSideCastle]), Rook => HashSet::from([
Queen => HashSet::from([MoveType::Horizontal(Many), MoveType::Vertical(Many), MoveType::Diagonal(Many)]), MoveType::Horizontal(Many),
King => HashSet::from([MoveType::Horizontal(One), MoveType::Vertical(One), MoveType::Diagonal(One), MoveType::KingSideCastle, MoveType::QueenSideCastle]) MoveType::Vertical(Many),
MoveType::KingSideCastle,
MoveType::QueenSideCastle,
]),
Queen => HashSet::from([
MoveType::Horizontal(Many),
MoveType::Vertical(Many),
MoveType::Diagonal(Many),
]),
King => HashSet::from([
MoveType::Horizontal(One),
MoveType::Vertical(One),
MoveType::Diagonal(One),
MoveType::KingSideCastle,
MoveType::QueenSideCastle,
]),
} }
} }
} }
trait PieceBehavior {
fn get_valid_move_types() -> HashSet<MoveType>;
fn get_symbol(&self) -> char;
}
pub struct Pawn {
color: PieceColor,
has_moved: bool,
}
pub struct Knight {
color: PieceColor,
}
pub struct Bishop {
color: PieceColor,
}
pub struct Rook {
color: PieceColor,
has_moved: bool,
}
pub struct Queen {
color: PieceColor,
}
pub struct King {
color: PieceColor,
has_moved: bool,
}
#[cfg(test)]
mod test { mod test {
use super::*; use super::*;
@ -214,13 +265,19 @@ mod test {
#[test] #[test]
fn test_get_white_knight_char() { fn test_get_white_knight_char() {
assert_eq!(Piece::get_symbol(PieceColor::White, PieceType::Knight), WHITE_KNIGHT); assert_eq!(
Piece::get_symbol(PieceColor::White, PieceType::Knight),
WHITE_KNIGHT
);
} }
#[test] #[test]
fn test_get_promoted_white_pawn_queen() { fn test_get_promoted_white_pawn_queen() {
assert_eq!( assert_eq!(
Piece::get_symbol(PieceColor::White, PieceType::Pawn(Some(Box::new(PieceType::Queen)))), Piece::get_symbol(
PieceColor::White,
PieceType::Pawn(Some(Box::new(PieceType::Queen)))
),
WHITE_QUEEN WHITE_QUEEN
); );
} }