[board] Add occupancy methods that return BitBoards

This commit is contained in:
Eryn Wells 2025-05-16 07:47:28 -07:00
parent 9943224ee0
commit 3b5b2f16a3
2 changed files with 37 additions and 8 deletions

View file

@ -82,7 +82,24 @@ impl Board {
} }
impl Board { impl Board {
#[must_use] pub fn occupancy(&self) -> BitBoard {
self.pieces.occpuancy()
}
pub fn vacancy(&self) -> BitBoard {
!self.occupancy()
}
pub fn friendly_occupancy(&self, color: Color) -> BitBoard {
self.pieces.friendly_occupancy(color)
}
pub fn opposing_occupancy(&self, color: Color) -> BitBoard {
self.pieces.opposing_occupancy(color)
}
}
impl Board {
pub fn display(&self) -> DiagramFormatter<'_> { pub fn display(&self) -> DiagramFormatter<'_> {
DiagramFormatter::new(self) DiagramFormatter::new(self)
} }

View file

@ -4,7 +4,7 @@ mod mailbox;
use self::mailbox::Mailbox; use self::mailbox::Mailbox;
use chessfriend_bitboard::{BitBoard, IterationDirection}; use chessfriend_bitboard::{BitBoard, IterationDirection};
use chessfriend_core::{Color, Piece, PlacedPiece, Shape, Square}; use chessfriend_core::{Color, Piece, Shape, Square};
use std::ops::BitOr; use std::ops::BitOr;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
@ -55,16 +55,28 @@ impl PieceSet {
} }
} }
/// A [`BitBoard`] representing all the pieces currently on the board. Other /// A [`BitBoard`] representing all the pieces on the board.
/// engines might refer to this concept as 'occupancy'. pub fn occpuancy(&self) -> BitBoard {
pub fn all_pieces(&self) -> BitBoard {
self.color_occupancy self.color_occupancy
.iter() .iter()
.fold(BitBoard::empty(), BitOr::bitor) .fold(BitBoard::default(), BitOr::bitor)
} }
pub(crate) fn iter(&self) -> impl Iterator<Item = PlacedPiece> { pub fn friendly_occupancy(&self, color: Color) -> BitBoard {
self.mailbox.iter() self.color_occupancy[color as usize]
}
pub fn opposing_occupancy(&self, color: Color) -> BitBoard {
self.color_occupancy.iter().enumerate().fold(
BitBoard::default(),
|acc, (index, occupancy)| {
if index == color as usize {
acc | occupancy
} else {
acc
}
},
)
} }
pub(crate) fn get(&self, square: Square) -> Option<Piece> { pub(crate) fn get(&self, square: Square) -> Option<Piece> {