[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

155
board/src/sight.rs Normal file
View file

@ -0,0 +1,155 @@
// Eryn Wells <eryn@erynwells.me>
use crate::{
piece::{Color, PlacedPiece, Shape},
square::Direction,
BitBoard, Position,
};
pub(crate) trait Sight {
fn sight_in_position(&self, position: &Position) -> BitBoard;
}
impl Sight for PlacedPiece {
fn sight_in_position(&self, position: &Position) -> BitBoard {
match self.shape() {
Shape::Pawn => match self.color() {
Color::White => self.white_pawn_sight_in_position(position),
Color::Black => self.black_pawn_sight_in_position(position),
},
Shape::Knight => self.knight_sight_in_position(position),
Shape::Bishop => self.bishop_sight_in_position(position),
Shape::Rook => self.rook_sight_in_position(position),
Shape::Queen => self.queen_sight_in_position(position),
Shape::King => self.king_sight_in_position(position),
}
}
}
impl PlacedPiece {
fn white_pawn_sight_in_position(&self, position: &Position) -> BitBoard {
let pawn: BitBoard = self.square().into();
let pawn = pawn.shift_north_west_one() | pawn.shift_north_east_one();
let mut possible_squares = position.empty_squares() | position.opposing_pieces();
if let Some(en_passant) = position.en_passant_square() {
possible_squares |= en_passant.into();
}
pawn & possible_squares
}
fn black_pawn_sight_in_position(&self, position: &Position) -> BitBoard {
let pawn: BitBoard = self.square().into();
let pawn = pawn.shift_south_west_one() | pawn.shift_south_east_one();
let mut possible_squares = position.empty_squares() | position.opposing_pieces();
if let Some(en_passant) = position.en_passant_square() {
possible_squares |= en_passant.into();
}
pawn & possible_squares
}
fn knight_sight_in_position(&self, position: &Position) -> BitBoard {
BitBoard::knight_moves(self.square()) & !position.friendly_pieces()
}
fn bishop_sight_in_position(&self, position: &Position) -> BitBoard {
let square = self.square();
let mut sight = BitBoard::empty();
let blockers = position.occupied_squares();
macro_rules! update_moves_with_ray {
($direction:ident, $occupied_squares:tt) => {
let ray = BitBoard::ray(square, Direction::$direction);
if let Some(first_occupied_square) =
BitBoard::$occupied_squares(&(ray & blockers)).next()
{
let remainder = BitBoard::ray(first_occupied_square, Direction::$direction);
let attack_ray = ray & !remainder;
sight |= attack_ray;
} else {
sight |= ray;
}
};
}
update_moves_with_ray!(NorthEast, occupied_squares_trailing);
update_moves_with_ray!(NorthWest, occupied_squares_trailing);
update_moves_with_ray!(SouthEast, occupied_squares);
update_moves_with_ray!(SouthWest, occupied_squares);
sight
}
fn rook_sight_in_position(&self, position: &Position) -> BitBoard {
let square = self.square();
let mut sight = BitBoard::empty();
let blockers = position.occupied_squares();
macro_rules! update_moves_with_ray {
($direction:ident, $occupied_squares:tt) => {
let ray = BitBoard::ray(square, Direction::$direction);
if let Some(first_occupied_square) =
BitBoard::$occupied_squares(&(ray & blockers)).next()
{
let remainder = BitBoard::ray(first_occupied_square, Direction::$direction);
let attack_ray = ray & !remainder;
sight |= attack_ray;
} else {
sight |= ray;
}
};
}
update_moves_with_ray!(North, occupied_squares_trailing);
update_moves_with_ray!(East, occupied_squares_trailing);
update_moves_with_ray!(South, occupied_squares);
update_moves_with_ray!(West, occupied_squares);
sight
}
fn queen_sight_in_position(&self, position: &Position) -> BitBoard {
let square = self.square();
let mut sight = BitBoard::empty();
let blockers = position.occupied_squares();
macro_rules! update_moves_with_ray {
($direction:ident, $occupied_squares:tt) => {
let ray = BitBoard::ray(square, Direction::$direction);
if let Some(first_occupied_square) =
BitBoard::$occupied_squares(&(ray & blockers)).next()
{
let remainder = BitBoard::ray(first_occupied_square, Direction::$direction);
let attack_ray = ray & !remainder;
sight |= attack_ray;
} else {
sight |= ray;
}
};
}
update_moves_with_ray!(NorthWest, occupied_squares_trailing);
update_moves_with_ray!(North, occupied_squares_trailing);
update_moves_with_ray!(NorthEast, occupied_squares_trailing);
update_moves_with_ray!(East, occupied_squares_trailing);
update_moves_with_ray!(SouthEast, occupied_squares);
update_moves_with_ray!(South, occupied_squares);
update_moves_with_ray!(SouthWest, occupied_squares);
update_moves_with_ray!(West, occupied_squares);
sight
}
fn king_sight_in_position(&self, position: &Position) -> BitBoard {
BitBoard::king_moves(self.square()) & !position.friendly_pieces()
}
}