From 4f93a095b579a2538d2996a4e44ac557c1b31a07 Mon Sep 17 00:00:00 2001 From: Paul-Nicolas Madelaine Date: Wed, 10 Sep 2025 17:48:34 +0200 Subject: [PATCH] wip --- src/bitboard.rs | 42 +++++++++++++++++++++++++++++------------- src/board.rs | 18 +++++++++--------- src/lib.rs | 2 +- src/position.rs | 6 ++++++ src/setup.rs | 3 ++- 5 files changed, 47 insertions(+), 24 deletions(-) diff --git a/src/bitboard.rs b/src/bitboard.rs index c74e435..5690696 100644 --- a/src/bitboard.rs +++ b/src/bitboard.rs @@ -1,22 +1,34 @@ +//! Sets of squares. + use crate::board::*; use std::iter::ExactSizeIterator; use std::iter::FusedIterator; +/// A set of squares. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Bitboard(pub(crate) u64); +pub struct Bitboard(pub u64); impl Bitboard { + /// Returns an empty bitboard. #[inline] pub fn new() -> Self { Self(0) } + /// Returns `true` if the bitboard is empty. #[inline] pub fn is_empty(&self) -> bool { self.0 == 0 } + /// Returns `true` if the bitboard contains the given square. + #[inline] + pub fn contains(&self, square: Square) -> bool { + self.0 & (1 << square as u8) != 0 + } + + /// Returns the first square in the bitboard, if it is not empty. #[inline] pub fn first(&self) -> Option { let mask = self.0; @@ -26,6 +38,7 @@ impl Bitboard { } } + /// Removes the first square in the bitboard and returns it, if the bitboard is not empty. #[inline] pub fn pop(&mut self) -> Option { let Self(ref mut mask) = self; @@ -37,13 +50,27 @@ impl Bitboard { square } + ///Inserts a square in the bitboard. #[inline] pub fn insert(&mut self, square: Square) { self.0 |= 1 << square as u8; } + /// Removes a square from the bitboard. #[inline] - pub fn trans(&self, direction: Direction) -> Self { + pub fn remove(&mut self, square: Square) { + self.0 &= !(1 << square as u8) + } + + /// Returns the square that is horizontaly symmetric to the current square. + #[inline] + pub fn mirror(self) -> Self { + let [a, b, c, d, e, f, g, h] = self.0.to_le_bytes(); + Self(u64::from_le_bytes([h, g, f, e, d, c, b, a])) + } + + #[inline] + pub(crate) fn trans(&self, direction: Direction) -> Self { match direction { Direction::North => Self(self.0 << 8), Direction::NorthEast => Self(self.0 << 9) & !File::A.bitboard(), @@ -55,17 +82,6 @@ impl Bitboard { Direction::NorthWest => Self(self.0 << 7) & !File::H.bitboard(), } } - - #[inline] - pub fn contains(&self, square: Square) -> bool { - self.0 & (1 << square as u8) != 0 - } - - #[inline] - pub fn mirror(self) -> Bitboard { - let [a, b, c, d, e, f, g, h] = self.0.to_le_bytes(); - Self(u64::from_le_bytes([h, g, f, e, d, c, b, a])) - } } impl std::ops::BitOr for Bitboard { diff --git a/src/board.rs b/src/board.rs index e90eda3..8b69468 100644 --- a/src/board.rs +++ b/src/board.rs @@ -3,9 +3,9 @@ use crate::bitboard::*; macro_rules! container { - ($a:ident, $b:ident, $n:literal) => { + ($vis:vis, $a:ident, $b:ident, $n:literal) => { #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] - pub(crate) struct $b(pub(crate) [T; $n]); + $vis struct $b(pub(crate) [T; $n]); #[allow(unused)] impl $b { #[inline] @@ -35,7 +35,7 @@ pub enum Color { Black, } -container!(Color, ByColor, 2); +container!(pub, Color, ByColor, 2); impl Color { #[inline] @@ -93,7 +93,7 @@ pub enum File { H, } -container!(File, ByFile, 8); +container!(pub(crate), File, ByFile, 8); impl File { #[inline] @@ -165,7 +165,7 @@ pub enum Rank { Eighth, } -container!(Rank, ByRank, 8); +container!(pub(crate), Rank, ByRank, 8); impl Rank { #[inline] @@ -243,7 +243,7 @@ pub enum Square{ A8, B8, C8, D8, E8, F8, G8, H8, } -container!(Square, BySquare, 64); +container!(pub(crate), Square, BySquare, 64); impl Square { #[inline] @@ -500,7 +500,7 @@ impl Role { } #[derive(Clone, Copy, PartialEq, Eq, Hash)] -pub(crate) struct ByRole(pub(crate) [T; 6]); +pub struct ByRole(pub(crate) [T; 6]); #[allow(unused)] impl ByRole { #[inline] @@ -572,7 +572,7 @@ pub(crate) enum Direction { West, } -container!(Direction, ByDirection, 8); +container!(pub(crate), Direction, ByDirection, 8); impl Direction { #[inline] @@ -614,7 +614,7 @@ pub enum CastlingSide { Long, } -container!(CastlingSide, ByCastlingSide, 2); +container!(pub(crate), CastlingSide, ByCastlingSide, 2); impl CastlingSide { #[inline] diff --git a/src/lib.rs b/src/lib.rs index 6c36c79..df4f342 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,10 +73,10 @@ //! - etc. pub(crate) mod array_vec; -pub(crate) mod bitboard; pub(crate) mod magics; pub(crate) mod rays; +pub mod bitboard; pub mod board; pub mod lookup; pub mod position; diff --git a/src/position.rs b/src/position.rs index 38ea97f..d8ba0fd 100644 --- a/src/position.rs +++ b/src/position.rs @@ -281,6 +281,12 @@ impl Position { } } + /// Returns the bitboards for every piece type and color. + #[inline] + pub fn bitboards(&self) -> ByColor> { + self.setup.bitboards() + } + pub(crate) fn move_from_uci<'l>(&'l self, uci: UciMove) -> Result, InvalidUciMove> { struct VisitorImpl { role: Role, diff --git a/src/setup.rs b/src/setup.rs index c3b774b..244a206 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -383,8 +383,9 @@ impl Setup { } } + /// Returns the bitboards for every piece type and color. #[inline] - pub(crate) fn bitboards(&self) -> ByColor> { + pub fn bitboards(&self) -> ByColor> { let Self { w, p_b_q,