Add 'MagicMoves'

This commit is contained in:
Bruno BELANYI 2024-03-29 02:56:01 +00:00
parent d519cfb817
commit 2bdfbbf467

View file

@ -1,7 +1,7 @@
pub(crate) mod generation;
mod mask;
use crate::board::Bitboard;
use crate::board::{Bitboard, Square};
/// A type representing the magic board indexing a given [crate::board::Square].
#[derive(Clone, Debug)]
@ -24,3 +24,36 @@ impl Magic {
base_index + self.offset
}
}
/// A type encapsulating a database of [Magic] bitboard moves.
#[derive(Clone, Debug)]
#[allow(unused)] // FIXME: remove when used
pub(crate) struct MagicMoves {
magics: Vec<Magic>,
moves: Vec<Bitboard>,
}
#[allow(unused)] // FIXME: remove when used
impl MagicMoves {
/// Initialize a new [MagicMoves] given a matching list of [Magic] and its corresponding moves
/// as a [Bitboard].
///
/// # Safety
///
/// This should only be called with values generated by [crate::movegen::wizardry::generation].
pub unsafe fn new(magics: Vec<Magic>, moves: Vec<Bitboard>) -> Self {
Self { magics, moves }
}
/// Get the set of valid moves for a piece standing on a [Square], given a set of blockers.
pub fn query(&self, square: Square, blockers: Bitboard) -> Bitboard {
// SAFETY: indices are in range by construction
unsafe {
let index = self
.magics
.get_unchecked(square.index())
.get_index(blockers);
*self.moves.get_unchecked(index)
}
}
}