1
0
Fork 0
This commit is contained in:
Paul-Nicolas Madelaine 2025-09-10 17:48:34 +02:00
parent ad3c6d0cfa
commit 4f93a095b5
5 changed files with 47 additions and 24 deletions

View file

@ -1,22 +1,34 @@
//! Sets of squares.
use crate::board::*; use crate::board::*;
use std::iter::ExactSizeIterator; use std::iter::ExactSizeIterator;
use std::iter::FusedIterator; use std::iter::FusedIterator;
/// A set of squares.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Bitboard(pub(crate) u64); pub struct Bitboard(pub u64);
impl Bitboard { impl Bitboard {
/// Returns an empty bitboard.
#[inline] #[inline]
pub fn new() -> Self { pub fn new() -> Self {
Self(0) Self(0)
} }
/// Returns `true` if the bitboard is empty.
#[inline] #[inline]
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.0 == 0 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] #[inline]
pub fn first(&self) -> Option<Square> { pub fn first(&self) -> Option<Square> {
let mask = self.0; 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] #[inline]
pub fn pop(&mut self) -> Option<Square> { pub fn pop(&mut self) -> Option<Square> {
let Self(ref mut mask) = self; let Self(ref mut mask) = self;
@ -37,13 +50,27 @@ impl Bitboard {
square square
} }
///Inserts a square in the bitboard.
#[inline] #[inline]
pub fn insert(&mut self, square: Square) { pub fn insert(&mut self, square: Square) {
self.0 |= 1 << square as u8; self.0 |= 1 << square as u8;
} }
/// Removes a square from the bitboard.
#[inline] #[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 { match direction {
Direction::North => Self(self.0 << 8), Direction::North => Self(self.0 << 8),
Direction::NorthEast => Self(self.0 << 9) & !File::A.bitboard(), Direction::NorthEast => Self(self.0 << 9) & !File::A.bitboard(),
@ -55,17 +82,6 @@ impl Bitboard {
Direction::NorthWest => Self(self.0 << 7) & !File::H.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 { impl std::ops::BitOr for Bitboard {

View file

@ -3,9 +3,9 @@
use crate::bitboard::*; use crate::bitboard::*;
macro_rules! container { 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)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct $b<T>(pub(crate) [T; $n]); $vis struct $b<T>(pub(crate) [T; $n]);
#[allow(unused)] #[allow(unused)]
impl<T> $b<T> { impl<T> $b<T> {
#[inline] #[inline]
@ -35,7 +35,7 @@ pub enum Color {
Black, Black,
} }
container!(Color, ByColor, 2); container!(pub, Color, ByColor, 2);
impl Color { impl Color {
#[inline] #[inline]
@ -93,7 +93,7 @@ pub enum File {
H, H,
} }
container!(File, ByFile, 8); container!(pub(crate), File, ByFile, 8);
impl File { impl File {
#[inline] #[inline]
@ -165,7 +165,7 @@ pub enum Rank {
Eighth, Eighth,
} }
container!(Rank, ByRank, 8); container!(pub(crate), Rank, ByRank, 8);
impl Rank { impl Rank {
#[inline] #[inline]
@ -243,7 +243,7 @@ pub enum Square{
A8, B8, C8, D8, E8, F8, G8, H8, A8, B8, C8, D8, E8, F8, G8, H8,
} }
container!(Square, BySquare, 64); container!(pub(crate), Square, BySquare, 64);
impl Square { impl Square {
#[inline] #[inline]
@ -500,7 +500,7 @@ impl Role {
} }
#[derive(Clone, Copy, PartialEq, Eq, Hash)] #[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct ByRole<T>(pub(crate) [T; 6]); pub struct ByRole<T>(pub(crate) [T; 6]);
#[allow(unused)] #[allow(unused)]
impl<T> ByRole<T> { impl<T> ByRole<T> {
#[inline] #[inline]
@ -572,7 +572,7 @@ pub(crate) enum Direction {
West, West,
} }
container!(Direction, ByDirection, 8); container!(pub(crate), Direction, ByDirection, 8);
impl Direction { impl Direction {
#[inline] #[inline]
@ -614,7 +614,7 @@ pub enum CastlingSide {
Long, Long,
} }
container!(CastlingSide, ByCastlingSide, 2); container!(pub(crate), CastlingSide, ByCastlingSide, 2);
impl CastlingSide { impl CastlingSide {
#[inline] #[inline]

View file

@ -73,10 +73,10 @@
//! - etc. //! - etc.
pub(crate) mod array_vec; pub(crate) mod array_vec;
pub(crate) mod bitboard;
pub(crate) mod magics; pub(crate) mod magics;
pub(crate) mod rays; pub(crate) mod rays;
pub mod bitboard;
pub mod board; pub mod board;
pub mod lookup; pub mod lookup;
pub mod position; pub mod position;

View file

@ -281,6 +281,12 @@ impl Position {
} }
} }
/// Returns the bitboards for every piece type and color.
#[inline]
pub fn bitboards(&self) -> ByColor<ByRole<Bitboard>> {
self.setup.bitboards()
}
pub(crate) fn move_from_uci<'l>(&'l self, uci: UciMove) -> Result<Move<'l>, InvalidUciMove> { pub(crate) fn move_from_uci<'l>(&'l self, uci: UciMove) -> Result<Move<'l>, InvalidUciMove> {
struct VisitorImpl<const ROLE: u8> { struct VisitorImpl<const ROLE: u8> {
role: Role, role: Role,

View file

@ -383,8 +383,9 @@ impl Setup {
} }
} }
/// Returns the bitboards for every piece type and color.
#[inline] #[inline]
pub(crate) fn bitboards(&self) -> ByColor<ByRole<Bitboard>> { pub fn bitboards(&self) -> ByColor<ByRole<Bitboard>> {
let Self { let Self {
w, w,
p_b_q, p_b_q,