Add 'Bitboard::any_square'

This commit is contained in:
Bruno BELANYI 2024-04-14 16:09:58 +01:00
parent 8be7105ac5
commit ed38c6c12d

View file

@ -75,6 +75,12 @@ impl Bitboard {
(self.0 & (self.0.wrapping_sub(1))) != 0 (self.0 & (self.0.wrapping_sub(1))) != 0
} }
/// Return a [Square] from the board, or `None` if it is empty.
#[inline(always)]
pub fn any_square(self) -> Option<Square> {
Square::try_from_index(self.0.trailing_zeros() as usize)
}
/// Iterate over the power-set of a given [Bitboard], yielding each possible sub-set of /// Iterate over the power-set of a given [Bitboard], yielding each possible sub-set of
/// [Square] that belong to the [Bitboard]. In other words, generate all set of [Square] that /// [Square] that belong to the [Bitboard]. In other words, generate all set of [Square] that
/// contain all, some, or none of the [Square] that are in the given [Bitboard]. /// contain all, some, or none of the [Square] that are in the given [Bitboard].
@ -479,6 +485,23 @@ mod test {
); );
} }
#[test]
fn any_square() {
for square in Square::iter() {
assert_eq!(square.into_bitboard().any_square(), Some(square));
}
}
#[test]
fn any_square_empty() {
assert!(Bitboard::EMPTY.any_square().is_none());
}
#[test]
fn any_square_full_board() {
assert!(Bitboard::ALL.any_square().is_some());
}
#[test] #[test]
fn into_square() { fn into_square() {
for square in Square::iter() { for square in Square::iter() {