From b3eee18a16b9c2691d43d1cd53f3f91c775f7ef2 Mon Sep 17 00:00:00 2001 From: Paul-Nicolas Madelaine Date: Tue, 18 Nov 2025 06:49:33 +0100 Subject: [PATCH] make Setup::bitboards public --- src/moves.rs | 2 +- src/position.rs | 2 +- src/setup.rs | 71 +++++++++++++++++++++++++------------------------ 3 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/moves.rs b/src/moves.rs index f7815f4..fafb6e8 100644 --- a/src/moves.rs +++ b/src/moves.rs @@ -77,7 +77,7 @@ impl<'l> Move<'l> { pub fn captured(self) -> Option { match self.raw.kind { MoveType::EnPassant => Some(Role::Pawn), - _ => self.position.as_setup().get_role(self.raw.to), + _ => self.position.as_setup().role(self.raw.to), } } diff --git a/src/position.rs b/src/position.rs index 9e30677..bdfe247 100644 --- a/src/position.rs +++ b/src/position.rs @@ -327,7 +327,7 @@ impl Position { to, promotion, } = uci; - let role = self.0.get_role(from).ok_or(InvalidUciMove::Illegal)?; + let role = self.0.role(from).ok_or(InvalidUciMove::Illegal)?; #[inline] fn aux<'l, const ROLE: u8>( position: &'l Position, diff --git a/src/setup.rs b/src/setup.rs index 826ca9c..a2afaa7 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -231,7 +231,7 @@ impl Setup { #[inline] pub fn get(&self, square: Square) -> Option { Some(Piece { - role: self.get_role(square)?, + role: self.role(square)?, color: match (self.w & square.bitboard()).is_empty() { false => Color::White, true => Color::Black, @@ -318,6 +318,40 @@ impl Setup { self.en_passant = OptionSquare::new(square); } + /// Returns, for each color and each type of piece, the bitboard of all squares occupied. + #[inline] + pub fn bitboards(&self) -> ByColor> { + let Self { + w, + p_b_q, + n_b_k, + r_q_k, + .. + } = self.clone(); + let k = n_b_k & r_q_k; + let q = p_b_q & r_q_k; + let b = p_b_q & n_b_k; + let n = n_b_k ^ b ^ k; + let r = r_q_k ^ q ^ k; + let p = p_b_q ^ b ^ q; + ByColor::with(|color| { + let mask = match color { + Color::White => w, + Color::Black => !w, + }; + ByRole::with(|role| { + mask & match role { + Role::Pawn => p, + Role::Knight => n, + Role::Bishop => b, + Role::Rook => r, + Role::Queen => q, + Role::King => k, + } + }) + }) + } + /// Returns the mirror image of the position. /// /// The mirror of a position is the position obtained after reflecting the placement of pieces @@ -472,7 +506,7 @@ impl Setup { } #[inline] - pub(crate) fn get_role(&self, square: Square) -> Option { + pub(crate) fn role(&self, square: Square) -> Option { let mask = square.bitboard(); let bit0 = (self.p_b_q & mask).0 >> square as u8; let bit1 = (self.n_b_k & mask).0 >> square as u8; @@ -482,39 +516,6 @@ impl Setup { i => Some(unsafe { Role::transmute(i as u8) }), } } - - #[inline] - pub(crate) fn bitboards(&self) -> ByColor> { - let Self { - w, - p_b_q, - n_b_k, - r_q_k, - .. - } = self.clone(); - let k = n_b_k & r_q_k; - let q = p_b_q & r_q_k; - let b = p_b_q & n_b_k; - let n = n_b_k ^ b ^ k; - let r = r_q_k ^ q ^ k; - let p = p_b_q ^ b ^ q; - ByColor::with(|color| { - let mask = match color { - Color::White => w, - Color::Black => !w, - }; - ByRole::with(|role| { - mask & match role { - Role::Pawn => p, - Role::Knight => n, - Role::Bishop => b, - Role::Rook => r, - Role::Queen => q, - Role::King => k, - } - }) - }) - } } impl TryFrom for Position {