From 3b5b2f16a3b5b17b259acefc5ecfcc75d40660ea Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 16 May 2025 07:47:28 -0700 Subject: [PATCH] [board] Add occupancy methods that return BitBoards --- board/src/board.rs | 19 ++++++++++++++++++- board/src/piece_sets.rs | 26 +++++++++++++++++++------- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/board/src/board.rs b/board/src/board.rs index ed719a5..3922511 100644 --- a/board/src/board.rs +++ b/board/src/board.rs @@ -82,7 +82,24 @@ 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<'_> { DiagramFormatter::new(self) } diff --git a/board/src/piece_sets.rs b/board/src/piece_sets.rs index 8096100..ff66b98 100644 --- a/board/src/piece_sets.rs +++ b/board/src/piece_sets.rs @@ -4,7 +4,7 @@ mod mailbox; use self::mailbox::Mailbox; use chessfriend_bitboard::{BitBoard, IterationDirection}; -use chessfriend_core::{Color, Piece, PlacedPiece, Shape, Square}; +use chessfriend_core::{Color, Piece, Shape, Square}; use std::ops::BitOr; #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] @@ -55,16 +55,28 @@ impl PieceSet { } } - /// A [`BitBoard`] representing all the pieces currently on the board. Other - /// engines might refer to this concept as 'occupancy'. - pub fn all_pieces(&self) -> BitBoard { + /// A [`BitBoard`] representing all the pieces on the board. + pub fn occpuancy(&self) -> BitBoard { self.color_occupancy .iter() - .fold(BitBoard::empty(), BitOr::bitor) + .fold(BitBoard::default(), BitOr::bitor) } - pub(crate) fn iter(&self) -> impl Iterator { - self.mailbox.iter() + pub fn friendly_occupancy(&self, color: Color) -> BitBoard { + 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 {