[board] Implement piece sight algorithms

Add a new Sight trait, implemented by PlacedPiece. The implementation of this
trait produces a BitBoard representing the squares visible to the placed piece.
This commit is contained in:
Eryn Wells 2024-01-15 16:03:06 -08:00
parent 3b40aacd52
commit 3ecc263701
4 changed files with 184 additions and 0 deletions

View file

@ -44,6 +44,8 @@ pub struct Position {
/// Bitboards representing positions of particular piece types per color.
pieces_per_type: [[BitBoard; 6]; 2],
en_passant_square: Option<Square>,
}
impl Position {
@ -70,6 +72,7 @@ impl Position {
BitBoard::empty(),
],
],
en_passant_square: None,
}
}
@ -101,6 +104,7 @@ impl Position {
black_pieces.iter().fold(BitBoard::empty(), |a, b| a | *b),
],
pieces_per_type: [white_pieces, black_pieces],
en_passant_square: None,
}
}
@ -155,6 +159,16 @@ impl Position {
self.pieces_per_color[Color::White as usize] | self.pieces_per_color[Color::Black as usize]
}
#[inline]
pub(crate) fn friendly_pieces(&self) -> BitBoard {
self.bitboard_for_color(self.color_to_move)
}
#[inline]
pub(crate) fn opposing_pieces(&self) -> BitBoard {
self.bitboard_for_color(self.color_to_move.other())
}
/// Return a BitBoard representing the set of squares containing a piece.
/// This set is the inverse of `occupied_squares`.
#[inline]
@ -195,6 +209,10 @@ impl Position {
pub fn pieces(&self, color: Color) -> Pieces {
Pieces::new(&self, color)
}
pub fn en_passant_square(&self) -> Option<Square> {
self.en_passant_square
}
}
impl Default for Position {