1
0
Fork 0

new quad-bitboard representation

This commit is contained in:
Paul-Nicolas Madelaine 2025-11-29 14:12:20 +01:00
parent 57b78880f2
commit 4c137d3c95
3 changed files with 43 additions and 39 deletions

View file

@ -29,7 +29,7 @@ use alloc::string::String;
/// [`from_text_record`](Setup::from_text_record) and [`to_text_record`](Setup::to_text_record).
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Setup {
/// bitboards = [pawns | bishops | queens, knights | bishops | kings, rooks | queens | kings, white]
/// bitboards = [pawns | bishops | queens, knights | bishops | kings, rooks | queens | kings, black]
pub(crate) bitboards: [Bitboard; 4],
pub(crate) turn: Color,
pub(crate) en_passant: OptionSquare,
@ -221,15 +221,12 @@ impl Setup {
Ok(())
}
/// Returns the occupancy of a square.
/// Returns the piece on the square, if any.
#[inline]
pub fn get(&self, square: Square) -> Option<Piece> {
Some(Piece {
role: self.role(square)?,
color: match self.is_white(square) {
true => Color::White,
false => Color::Black,
},
color: self.color(square),
})
}
@ -255,11 +252,11 @@ impl Setup {
#[inline]
pub fn set(&mut self, square: Square, piece: Option<Piece>) {
let mask = !square.bitboard();
let [p_b_q, n_b_k, r_q_k, w] = &mut self.bitboards;
let [p_b_q, n_b_k, r_q_k, black] = &mut self.bitboards;
*p_b_q &= mask;
*n_b_k &= mask;
*r_q_k &= mask;
*w &= mask;
*black &= mask;
if let Some(piece) = piece {
let to = square.bitboard();
match piece.role {
@ -286,8 +283,8 @@ impl Setup {
}
}
match piece.color {
Color::White => *w |= to,
Color::Black => (),
Color::White => (),
Color::Black => *black |= to,
}
}
}
@ -319,7 +316,7 @@ impl Setup {
/// - pawns, bishops and queens
/// - knights, bishops and kings
/// - rooks, queens and kings
/// - white pieces
/// - black pieces
#[inline]
pub fn bitboards(&self) -> [Bitboard; 4] {
self.bitboards
@ -335,13 +332,13 @@ impl Setup {
/// is `rnbqkbnr/pppp1ppp/8/4p3/8/8/PPPPPPPP/RNBQKBNR w Qk e6`.
#[inline]
pub fn mirror(&self) -> Self {
let [p_b_q, n_b_k, r_q_k, w] = self.bitboards;
let [p_b_q, n_b_k, r_q_k, black] = self.bitboards;
Self {
bitboards: [
p_b_q.mirror(),
n_b_k.mirror(),
r_q_k.mirror(),
(w ^ (p_b_q | n_b_k | r_q_k)).mirror(),
(black ^ (p_b_q | n_b_k | r_q_k)).mirror(),
],
turn: !self.turn,
en_passant: self
@ -359,9 +356,9 @@ impl Setup {
pub fn into_position(self) -> Result<Position, IllegalPosition> {
let mut reasons = IllegalPositionReasons::new();
let [p_b_q, n_b_k, r_q_k, w] = self.bitboards;
let [p_b_q, n_b_k, r_q_k, black] = self.bitboards;
debug_assert!((!(p_b_q | n_b_k | r_q_k) & w).is_empty());
debug_assert!((!(p_b_q | n_b_k | r_q_k) & black).is_empty());
debug_assert!((p_b_q & n_b_k & r_q_k).is_empty());
let k = n_b_k & r_q_k;
@ -372,8 +369,8 @@ impl Setup {
let p = p_b_q ^ b ^ q;
let pieces = ByColor::with(|color| {
let mask = match color {
Color::White => w,
Color::Black => !w,
Color::White => !black,
Color::Black => black,
};
ByRole::with(|role| {
mask & match role {
@ -518,10 +515,11 @@ impl Setup {
}
}
/// Returns the color of the piece on the square if any, and `Black` if the square is not occupied.
#[inline]
pub(crate) fn is_white(&self, square: Square) -> bool {
let [_, _, _, w] = self.bitboards;
w.contains(square)
pub(crate) fn color(&self, square: Square) -> Color {
let [_, _, _, black] = self.bitboards;
unsafe { Color::new_unchecked(((black & square.bitboard()).0 >> square as u8) as u8) }
}
}