[board] Implement some is_shape getters on Piece and PlacedPiece
This commit is contained in:
parent
620701057d
commit
f7951d6110
1 changed files with 46 additions and 0 deletions
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue