Add 'CastleRights::unmoved_rooks'
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bruno BELANYI 2022-07-18 13:50:23 +02:00
parent c5949fb01e
commit aa3b464bb8

View file

@ -1,3 +1,5 @@
use super::{Bitboard, Color, File, Square};
/// Current castle rights for a player. /// Current castle rights for a player.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum CastleRights { pub enum CastleRights {
@ -62,6 +64,22 @@ impl CastleRights {
// SAFETY: we know the value is in-bounds // SAFETY: we know the value is in-bounds
unsafe { Self::from_index_unchecked(self.index() & !to_remove.index()) } unsafe { Self::from_index_unchecked(self.index() & !to_remove.index()) }
} }
/// Which rooks have not been moved for a given [CastleRights] and [Color].
#[inline(always)]
pub fn unmoved_rooks(self, color: Color) -> Bitboard {
let rank = color.first_rank();
let king_side_square = Square::new(File::H, rank);
let queen_side_square = Square::new(File::A, rank);
match self {
Self::NoSide => Bitboard::EMPTY,
Self::KingSide => king_side_square.into_bitboard(),
Self::QueenSide => queen_side_square.into_bitboard(),
Self::BothSides => king_side_square | queen_side_square,
}
}
} }
#[cfg(test)] #[cfg(test)]
@ -139,4 +157,40 @@ mod test {
CastleRights::KingSide CastleRights::KingSide
); );
} }
#[test]
fn unmoved_rooks() {
assert_eq!(
CastleRights::NoSide.unmoved_rooks(Color::White),
Bitboard::EMPTY
);
assert_eq!(
CastleRights::NoSide.unmoved_rooks(Color::Black),
Bitboard::EMPTY
);
assert_eq!(
CastleRights::KingSide.unmoved_rooks(Color::White),
Square::H1.into_bitboard()
);
assert_eq!(
CastleRights::KingSide.unmoved_rooks(Color::Black),
Square::H8.into_bitboard()
);
assert_eq!(
CastleRights::QueenSide.unmoved_rooks(Color::White),
Square::A1.into_bitboard()
);
assert_eq!(
CastleRights::QueenSide.unmoved_rooks(Color::Black),
Square::A8.into_bitboard()
);
assert_eq!(
CastleRights::BothSides.unmoved_rooks(Color::White),
Square::A1 | Square::H1
);
assert_eq!(
CastleRights::BothSides.unmoved_rooks(Color::Black),
Square::A8 | Square::H8
);
}
} }