2024-01-15 16:03:06 -08:00
|
|
|
// Eryn Wells <eryn@erynwells.me>
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
piece::{Color, PlacedPiece, Shape},
|
|
|
|
BitBoard, Position,
|
|
|
|
};
|
2024-01-24 08:48:19 -08:00
|
|
|
use chessfriend_core::Direction;
|
2024-01-15 16:03:06 -08:00
|
|
|
|
|
|
|
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() {
|
2024-01-21 09:21:27 -08:00
|
|
|
let en_passant_bitboard: BitBoard = en_passant.into();
|
|
|
|
possible_squares |= en_passant_bitboard;
|
2024-01-15 16:03:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2024-01-21 09:21:27 -08:00
|
|
|
possible_squares |= &en_passant.into();
|
2024-01-15 16:03:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
2024-01-17 08:44:27 -08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
macro_rules! sight_test {
|
|
|
|
($test_name:ident, $position:expr, $piece:expr, $bitboard:expr) => {
|
|
|
|
#[test]
|
|
|
|
fn $test_name() {
|
|
|
|
let pos = $position;
|
|
|
|
let pp = $piece;
|
|
|
|
let sight = pp.sight_in_position(&pos);
|
|
|
|
|
|
|
|
assert_eq!(sight, $bitboard);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
($test_name:ident, $piece:expr, $bitboard:expr) => {
|
|
|
|
sight_test! {$test_name, $crate::Position::empty(), $piece, $bitboard}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
mod pawn {
|
2024-01-24 08:32:09 -08:00
|
|
|
use crate::{sight::Sight, BitBoard};
|
2024-01-24 08:48:19 -08:00
|
|
|
use chessfriend_core::Square;
|
2024-01-17 08:44:27 -08:00
|
|
|
|
|
|
|
sight_test!(e4_pawn, piece!(White Pawn on E4), bitboard!(D5, F5));
|
|
|
|
|
|
|
|
sight_test!(
|
|
|
|
e4_pawn_one_blocker,
|
2024-01-21 13:06:44 -08:00
|
|
|
test_position![
|
2024-01-17 08:44:27 -08:00
|
|
|
White Bishop on D5,
|
|
|
|
White Pawn on E4,
|
|
|
|
],
|
|
|
|
piece!(White Pawn on E4),
|
|
|
|
bitboard!(F5)
|
|
|
|
);
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn e4_pawn_two_blocker() {
|
2024-01-21 13:06:44 -08:00
|
|
|
let pos = test_position!(
|
2024-01-17 08:44:27 -08:00
|
|
|
White Bishop on D5,
|
|
|
|
White Queen on F5,
|
|
|
|
White Pawn on E4,
|
|
|
|
);
|
2024-01-21 13:06:44 -08:00
|
|
|
|
2024-01-17 08:44:27 -08:00
|
|
|
let pp = piece!(White Pawn on E4);
|
|
|
|
let sight = pp.sight_in_position(&pos);
|
|
|
|
|
|
|
|
assert_eq!(sight, BitBoard::empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn e4_pawn_capturable() {
|
2024-01-21 13:06:44 -08:00
|
|
|
let pos = test_position!(
|
2024-01-17 08:44:27 -08:00
|
|
|
Black Bishop on D5,
|
|
|
|
White Queen on F5,
|
|
|
|
White Pawn on E4,
|
|
|
|
);
|
2024-01-21 13:06:44 -08:00
|
|
|
|
2024-01-17 08:44:27 -08:00
|
|
|
let pp = piece!(White Pawn on E4);
|
|
|
|
let sight = pp.sight_in_position(&pos);
|
|
|
|
|
|
|
|
assert_eq!(sight, bitboard!(D5));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn e5_en_passant() {
|
2024-01-21 13:06:44 -08:00
|
|
|
let mut pos = test_position!(
|
2024-01-17 08:44:27 -08:00
|
|
|
White Pawn on E5,
|
|
|
|
Black Pawn on D5,
|
|
|
|
);
|
|
|
|
pos.test_set_en_passant_square(Square::D6);
|
|
|
|
let pp = piece!(White Pawn on E5);
|
|
|
|
let sight = pp.sight_in_position(&pos);
|
|
|
|
|
|
|
|
assert_eq!(sight, bitboard!(D6, F6));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
mod knight {
|
2024-01-21 10:39:24 -08:00
|
|
|
use crate::sight::Sight;
|
2024-01-17 08:44:27 -08:00
|
|
|
|
|
|
|
sight_test!(
|
|
|
|
f6_knight,
|
|
|
|
piece!(Black Knight on F6),
|
|
|
|
bitboard!(H7, G8, E8, D7, D5, E4, G4, H5)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
mod bishop {
|
|
|
|
use crate::sight::Sight;
|
|
|
|
|
|
|
|
sight_test!(
|
|
|
|
c2_bishop,
|
|
|
|
piece!(Black Bishop on C2),
|
|
|
|
bitboard!(D1, B3, A4, B1, D3, E4, F5, G6, H7)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
mod rook {
|
|
|
|
use crate::sight::Sight;
|
|
|
|
|
|
|
|
sight_test!(
|
|
|
|
g3_rook,
|
|
|
|
piece!(White Rook on G3),
|
|
|
|
bitboard!(G1, G2, G4, G5, G6, G7, G8, A3, B3, C3, D3, E3, F3, H3)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|