diff --git a/board/src/piece.rs b/board/src/piece.rs index 6afae98..557f96a 100644 --- a/board/src/piece.rs +++ b/board/src/piece.rs @@ -139,6 +139,14 @@ macro_rules! piece_constructor { }; } +macro_rules! is_shape { + ($func_name:ident, $shape:ident) => { + pub fn $func_name(&self) -> bool { + self.shape == Shape::$shape + } + }; +} + impl Piece { pub fn new(color: Color, shape: Shape) -> Piece { Piece { color, shape } @@ -158,6 +166,13 @@ impl Piece { pub fn shape(&self) -> Shape { self.shape } + + 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); } impl fmt::Display for Piece { @@ -215,6 +230,14 @@ pub struct PlacedPiece { square: Square, } +macro_rules! is_shape { + ($func_name:ident, $shape:ident) => { + pub fn $func_name(&self) -> bool { + self.piece().shape == Shape::$shape + } + }; +} + impl PlacedPiece { pub const fn new(piece: Piece, square: Square) -> PlacedPiece { PlacedPiece { piece, square } @@ -239,6 +262,29 @@ impl PlacedPiece { pub fn shape(&self) -> Shape { self.piece.shape } + + 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); + + pub fn is_kingside_rook(&self) -> bool { + self.is_rook() + && match self.color() { + Color::White => self.square == Square::H1, + Color::Black => self.square == Square::H8, + } + } + + pub fn is_queenside_rook(&self) -> bool { + self.is_rook() + && match self.color() { + Color::White => self.square == Square::A1, + Color::Black => self.square == Square::A8, + } + } } impl fmt::Display for PlacedPiece {