1
0
Fork 0

make Setup::bitboards public

This commit is contained in:
Paul-Nicolas Madelaine 2025-11-18 06:49:33 +01:00
parent b45952b6b7
commit b3eee18a16
3 changed files with 38 additions and 37 deletions

View file

@ -77,7 +77,7 @@ impl<'l> Move<'l> {
pub fn captured(self) -> Option<Role> {
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),
}
}

View file

@ -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,

View file

@ -231,7 +231,7 @@ impl Setup {
#[inline]
pub fn get(&self, square: Square) -> Option<Piece> {
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<ByRole<Bitboard>> {
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<Role> {
pub(crate) fn role(&self, square: Square) -> Option<Role> {
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<ByRole<Bitboard>> {
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<Setup> for Position {