From da39b2fc9cae7ec369a4d615dc84bfdec032ba7c Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 15 Jul 2022 15:41:42 -0400 Subject: [PATCH] Add more --- src/lib.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 68 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9bded2b..73e0257 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,7 +15,7 @@ const BLACK_KING: char = '♚'; #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)] #[rustfmt::skip] -enum BoardPosition { +pub enum BoardPosition { A1, A2, A3, A4, A5, A6, A7, A8, B1, B2, B3, B4, B5, B6, B7, B8, C1, C2, C3, C4, C5, C6, C7, C8, @@ -103,7 +103,7 @@ impl From for usize { pub enum MoveAmount { One, Two, - Many + Many, } #[derive(Debug, Clone, PartialEq, Hash, Eq)] @@ -118,8 +118,6 @@ pub enum MoveType { Promoted(PieceType), } - - #[derive(Debug, Copy, Clone, PartialEq)] pub enum PieceColor { Black, @@ -153,7 +151,7 @@ pub struct Piece { symbol: char, } -impl<'a> Piece { +impl Piece { pub fn new(color: PieceColor, kind: PieceType) -> Self { Self { kind: kind.clone(), @@ -191,16 +189,69 @@ impl<'a> Piece { match kind { 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]), Bishop(_) => HashSet::from([MoveType::Diagonal(Many)]), - Rook => HashSet::from([MoveType::Horizontal(Many), 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]) + Rook => HashSet::from([ + MoveType::Horizontal(Many), + 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; + 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 { use super::*; @@ -214,13 +265,19 @@ mod test { #[test] 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] fn test_get_promoted_white_pawn_queen() { 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 ); }