[bitboard] Make a few tweaks to some definitions in BitBoard

Use u64::MIN and u64::MAX to define the empty and full bitboards
Write From<Square> as `1u64 << sq as u32`
Write a doc test for BitBoard::is_single_square()
Make library::RANKS and library::FILES pub(crate) instead of pub(super)
This commit is contained in:
Eryn Wells 2024-02-11 08:44:11 -07:00
parent 891b3ddbb9
commit 997621eea7
2 changed files with 16 additions and 5 deletions

View file

@ -18,8 +18,8 @@ macro_rules! moves_getter {
}
impl BitBoard {
pub const EMPTY: BitBoard = BitBoard(0);
pub const FULL: BitBoard = BitBoard(0xFFFFFFFFFFFFFFFF);
pub const EMPTY: BitBoard = BitBoard(u64::MIN);
pub const FULL: BitBoard = BitBoard(u64::MAX);
pub const fn empty() -> BitBoard {
BitBoard(0)
@ -104,6 +104,17 @@ impl BitBoard {
*self &= !sq_bb
}
/// Returns `true` if this BitBoard represents a single square.
///
/// ## Examples
///
/// ```
/// use chessfriend_bitboard::BitBoard;
/// assert!(!BitBoard::EMPTY.is_single_square(), "Empty bitboards represent no squares");
/// assert!(!BitBoard::FULL.is_single_square(), "Full bitboards represent all the squares");
/// assert!(!BitBoard::new(0b010011110101101100).is_single_square(), "This bitboard represents a bunch of squares");
/// assert!(BitBoard::new(0b10000000000000).is_single_square());
/// ```
pub fn is_single_square(&self) -> bool {
self.0.is_power_of_two()
}
@ -149,7 +160,7 @@ impl Default for BitBoard {
impl From<Square> for BitBoard {
fn from(value: Square) -> Self {
BitBoard(1 << value as u64)
BitBoard(1u64 << value as u32)
}
}

View file

@ -4,7 +4,7 @@ use crate::BitBoard;
use chessfriend_core::{Color, Direction, Square};
use std::sync::OnceLock;
pub(super) const RANKS: [BitBoard; 8] = [
pub(crate) const RANKS: [BitBoard; 8] = [
BitBoard(0xFF << 0 * 8),
BitBoard(0xFF << 1 * 8),
BitBoard(0xFF << 2 * 8),
@ -15,7 +15,7 @@ pub(super) const RANKS: [BitBoard; 8] = [
BitBoard(0xFF << 7 * 8),
];
pub(super) const FILES: [BitBoard; 8] = [
pub(crate) const FILES: [BitBoard; 8] = [
BitBoard(0x0101010101010101 << 0),
BitBoard(0x0101010101010101 << 1),
BitBoard(0x0101010101010101 << 2),