2024-01-15 16:03:06 -08:00
|
|
|
// Eryn Wells <eryn@erynwells.me>
|
|
|
|
|
2024-01-28 00:25:53 -08:00
|
|
|
use crate::position::piece_sets::PieceBitBoards;
|
2024-01-24 09:18:12 -08:00
|
|
|
use chessfriend_bitboard::BitBoard;
|
2024-01-28 09:46:38 -08:00
|
|
|
use chessfriend_core::{Color, Direction, PlacedPiece, Shape, Square};
|
2024-01-15 16:03:06 -08:00
|
|
|
|
2024-02-01 08:43:03 -08:00
|
|
|
macro_rules! ray_in_direction {
|
|
|
|
($square:expr, $blockers:expr, $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;
|
|
|
|
attack_ray
|
|
|
|
} else {
|
|
|
|
*ray
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2024-01-24 17:08:27 -08:00
|
|
|
pub(crate) trait SightExt {
|
2024-01-28 00:25:53 -08:00
|
|
|
fn sight(&self, pieces: &PieceBitBoards, en_passant_square: Option<Square>) -> BitBoard;
|
|
|
|
|
|
|
|
fn white_pawn_sight(
|
|
|
|
&self,
|
|
|
|
pieces: &PieceBitBoards,
|
|
|
|
en_passant_square: Option<Square>,
|
|
|
|
) -> BitBoard;
|
2024-01-28 09:46:38 -08:00
|
|
|
|
2024-01-28 00:25:53 -08:00
|
|
|
fn black_pawn_sight(
|
|
|
|
&self,
|
|
|
|
pieces: &PieceBitBoards,
|
|
|
|
en_passant_square: Option<Square>,
|
|
|
|
) -> BitBoard;
|
2024-01-28 09:46:38 -08:00
|
|
|
|
2024-01-28 00:25:53 -08:00
|
|
|
fn knight_sight(&self, pieces: &PieceBitBoards) -> BitBoard;
|
|
|
|
fn bishop_sight(&self, pieces: &PieceBitBoards) -> BitBoard;
|
|
|
|
fn rook_sight(&self, pieces: &PieceBitBoards) -> BitBoard;
|
|
|
|
fn queen_sight(&self, pieces: &PieceBitBoards) -> BitBoard;
|
|
|
|
fn king_sight(&self, pieces: &PieceBitBoards) -> BitBoard;
|
2024-01-15 16:03:06 -08:00
|
|
|
}
|
|
|
|
|
2024-02-01 08:42:19 -08:00
|
|
|
pub(crate) trait SliderRayToSquareExt {
|
2024-02-02 07:29:43 -08:00
|
|
|
fn ray_to_square(&self, origin: Square, target: Square) -> Option<BitBoard>;
|
2024-02-01 08:42:19 -08:00
|
|
|
}
|
|
|
|
|
2024-01-24 17:08:27 -08:00
|
|
|
impl SightExt for PlacedPiece {
|
2024-01-28 00:25:53 -08:00
|
|
|
fn sight(&self, pieces: &PieceBitBoards, en_passant_square: Option<Square>) -> BitBoard {
|
2024-01-15 16:03:06 -08:00
|
|
|
match self.shape() {
|
|
|
|
Shape::Pawn => match self.color() {
|
2024-01-28 00:25:53 -08:00
|
|
|
Color::White => self.white_pawn_sight(pieces, en_passant_square),
|
|
|
|
Color::Black => self.black_pawn_sight(pieces, en_passant_square),
|
2024-01-15 16:03:06 -08:00
|
|
|
},
|
2024-01-28 00:25:53 -08:00
|
|
|
Shape::Knight => self.knight_sight(pieces),
|
|
|
|
Shape::Bishop => self.bishop_sight(pieces),
|
|
|
|
Shape::Rook => self.rook_sight(pieces),
|
|
|
|
Shape::Queen => self.queen_sight(pieces),
|
|
|
|
Shape::King => self.king_sight(pieces),
|
2024-01-15 16:03:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-28 00:25:53 -08:00
|
|
|
fn white_pawn_sight(
|
|
|
|
&self,
|
|
|
|
pieces: &PieceBitBoards,
|
|
|
|
en_passant_square: Option<Square>,
|
|
|
|
) -> BitBoard {
|
|
|
|
let opponent = self.color().other();
|
2024-01-15 16:03:06 -08:00
|
|
|
let pawn: BitBoard = self.square().into();
|
|
|
|
let pawn = pawn.shift_north_west_one() | pawn.shift_north_east_one();
|
|
|
|
|
2024-01-28 00:25:53 -08:00
|
|
|
let mut possible_squares = pieces.empty_squares() | pieces.all_pieces_of_color(opponent);
|
|
|
|
if let Some(en_passant) = 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
|
|
|
|
}
|
|
|
|
|
2024-01-28 00:25:53 -08:00
|
|
|
fn black_pawn_sight(
|
|
|
|
&self,
|
|
|
|
pieces: &PieceBitBoards,
|
|
|
|
en_passant_square: Option<Square>,
|
|
|
|
) -> BitBoard {
|
|
|
|
let opponent = self.color().other();
|
|
|
|
|
2024-01-15 16:03:06 -08:00
|
|
|
let pawn: BitBoard = self.square().into();
|
|
|
|
let pawn = pawn.shift_south_west_one() | pawn.shift_south_east_one();
|
|
|
|
|
2024-01-28 00:25:53 -08:00
|
|
|
let mut possible_squares = pieces.empty_squares() | pieces.all_pieces_of_color(opponent);
|
|
|
|
if let Some(en_passant) = 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
|
|
|
|
}
|
|
|
|
|
2024-01-28 00:25:53 -08:00
|
|
|
fn knight_sight(&self, pieces: &PieceBitBoards) -> BitBoard {
|
|
|
|
BitBoard::knight_moves(self.square()) & !pieces.all_pieces_of_color(self.color())
|
2024-01-15 16:03:06 -08:00
|
|
|
}
|
|
|
|
|
2024-01-28 00:25:53 -08:00
|
|
|
fn bishop_sight(&self, pieces: &PieceBitBoards) -> BitBoard {
|
2024-01-15 16:03:06 -08:00
|
|
|
let square = self.square();
|
|
|
|
|
|
|
|
let mut sight = BitBoard::empty();
|
|
|
|
|
2024-01-28 00:25:53 -08:00
|
|
|
let blockers = pieces.all_pieces();
|
2024-01-15 16:03:06 -08:00
|
|
|
|
2024-02-01 08:43:03 -08:00
|
|
|
sight |= ray_in_direction!(square, blockers, NorthEast, occupied_squares_trailing);
|
|
|
|
sight |= ray_in_direction!(square, blockers, NorthWest, occupied_squares_trailing);
|
|
|
|
sight |= ray_in_direction!(square, blockers, SouthEast, occupied_squares);
|
|
|
|
sight |= ray_in_direction!(square, blockers, SouthWest, occupied_squares);
|
2024-01-15 16:03:06 -08:00
|
|
|
|
2024-01-28 00:25:53 -08:00
|
|
|
let friendly_pieces = pieces.all_pieces_of_color(self.color());
|
|
|
|
sight & !friendly_pieces
|
2024-01-15 16:03:06 -08:00
|
|
|
}
|
|
|
|
|
2024-01-28 00:25:53 -08:00
|
|
|
fn rook_sight(&self, pieces: &PieceBitBoards) -> BitBoard {
|
2024-01-15 16:03:06 -08:00
|
|
|
let square = self.square();
|
|
|
|
|
|
|
|
let mut sight = BitBoard::empty();
|
|
|
|
|
2024-01-28 00:25:53 -08:00
|
|
|
let blockers = pieces.all_pieces();
|
2024-01-15 16:03:06 -08:00
|
|
|
|
2024-02-01 08:43:03 -08:00
|
|
|
sight |= ray_in_direction!(square, blockers, North, occupied_squares_trailing);
|
|
|
|
sight |= ray_in_direction!(square, blockers, East, occupied_squares_trailing);
|
|
|
|
sight |= ray_in_direction!(square, blockers, South, occupied_squares);
|
|
|
|
sight |= ray_in_direction!(square, blockers, West, occupied_squares);
|
2024-01-15 16:03:06 -08:00
|
|
|
|
2024-01-28 00:25:53 -08:00
|
|
|
let friendly_pieces = pieces.all_pieces_of_color(self.color());
|
|
|
|
sight & !friendly_pieces
|
2024-01-15 16:03:06 -08:00
|
|
|
}
|
|
|
|
|
2024-01-28 00:25:53 -08:00
|
|
|
fn queen_sight(&self, pieces: &PieceBitBoards) -> BitBoard {
|
2024-01-15 16:03:06 -08:00
|
|
|
let square = self.square();
|
|
|
|
|
|
|
|
let mut sight = BitBoard::empty();
|
|
|
|
|
2024-01-28 00:25:53 -08:00
|
|
|
let blockers = pieces.all_pieces();
|
2024-01-15 16:03:06 -08:00
|
|
|
|
2024-02-01 08:43:03 -08:00
|
|
|
sight |= ray_in_direction!(square, blockers, NorthWest, occupied_squares_trailing);
|
|
|
|
sight |= ray_in_direction!(square, blockers, North, occupied_squares_trailing);
|
|
|
|
sight |= ray_in_direction!(square, blockers, NorthEast, occupied_squares_trailing);
|
|
|
|
sight |= ray_in_direction!(square, blockers, East, occupied_squares_trailing);
|
|
|
|
sight |= ray_in_direction!(square, blockers, SouthEast, occupied_squares);
|
|
|
|
sight |= ray_in_direction!(square, blockers, South, occupied_squares);
|
|
|
|
sight |= ray_in_direction!(square, blockers, SouthWest, occupied_squares);
|
|
|
|
sight |= ray_in_direction!(square, blockers, West, occupied_squares);
|
2024-01-15 16:03:06 -08:00
|
|
|
|
2024-01-28 00:25:53 -08:00
|
|
|
let friendly_pieces = pieces.all_pieces_of_color(self.color());
|
|
|
|
sight & !friendly_pieces
|
2024-01-15 16:03:06 -08:00
|
|
|
}
|
|
|
|
|
2024-01-28 00:25:53 -08:00
|
|
|
fn king_sight(&self, pieces: &PieceBitBoards) -> BitBoard {
|
|
|
|
BitBoard::king_moves(self.square()) & !pieces.all_pieces_of_color(self.color())
|
2024-01-15 16:03:06 -08:00
|
|
|
}
|
|
|
|
}
|
2024-01-17 08:44:27 -08:00
|
|
|
|
2024-02-02 07:29:43 -08:00
|
|
|
impl SliderRayToSquareExt for Shape {
|
|
|
|
fn ray_to_square(&self, origin: Square, target: Square) -> Option<BitBoard> {
|
2024-02-01 08:42:19 -08:00
|
|
|
macro_rules! ray {
|
|
|
|
($square:expr, $direction:ident) => {
|
|
|
|
(
|
|
|
|
BitBoard::ray($square, Direction::$direction),
|
|
|
|
Direction::$direction,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let target_bitboard: BitBoard = target.into();
|
|
|
|
|
2024-02-02 07:29:43 -08:00
|
|
|
let ray_and_direction = match self {
|
2024-02-01 08:42:19 -08:00
|
|
|
Shape::Bishop => [
|
2024-02-02 07:29:43 -08:00
|
|
|
ray!(origin, NorthEast),
|
|
|
|
ray!(origin, SouthEast),
|
|
|
|
ray!(origin, SouthWest),
|
|
|
|
ray!(origin, NorthWest),
|
2024-02-01 08:42:19 -08:00
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.find(|(&ray, _)| !(target_bitboard & ray).is_empty()),
|
|
|
|
Shape::Rook => [
|
2024-02-02 07:29:43 -08:00
|
|
|
ray!(origin, North),
|
|
|
|
ray!(origin, East),
|
|
|
|
ray!(origin, South),
|
|
|
|
ray!(origin, West),
|
2024-02-01 08:42:19 -08:00
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.find(|(&ray, _)| !(target_bitboard & ray).is_empty()),
|
|
|
|
Shape::Queen => [
|
2024-02-02 07:29:43 -08:00
|
|
|
ray!(origin, North),
|
|
|
|
ray!(origin, NorthEast),
|
|
|
|
ray!(origin, East),
|
|
|
|
ray!(origin, SouthEast),
|
|
|
|
ray!(origin, South),
|
|
|
|
ray!(origin, SouthWest),
|
|
|
|
ray!(origin, West),
|
|
|
|
ray!(origin, NorthWest),
|
2024-02-01 08:42:19 -08:00
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.find(|(&ray, _)| !(target_bitboard & ray).is_empty()),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some((ray, direction)) = ray_and_direction {
|
2024-02-02 07:29:43 -08:00
|
|
|
let remainder = BitBoard::ray(target, direction);
|
|
|
|
return Some(ray & !remainder);
|
2024-02-01 08:42:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-17 08:44:27 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-02-01 08:42:19 -08:00
|
|
|
use super::*;
|
|
|
|
use chessfriend_bitboard::bitboard;
|
|
|
|
use chessfriend_core::{piece, Square};
|
|
|
|
|
2024-01-17 08:44:27 -08:00
|
|
|
macro_rules! sight_test {
|
|
|
|
($test_name:ident, $position:expr, $piece:expr, $bitboard:expr) => {
|
|
|
|
#[test]
|
|
|
|
fn $test_name() {
|
|
|
|
let pos = $position;
|
2024-01-28 00:25:53 -08:00
|
|
|
let piece = $piece;
|
|
|
|
let sight = pos.sight_of_piece(&piece);
|
2024-01-17 08:44:27 -08:00
|
|
|
|
|
|
|
assert_eq!(sight, $bitboard);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
($test_name:ident, $piece:expr, $bitboard:expr) => {
|
|
|
|
sight_test! {$test_name, $crate::Position::empty(), $piece, $bitboard}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-02-01 08:42:19 -08:00
|
|
|
#[test]
|
|
|
|
fn pawns_and_knights_cannot_make_rays() {
|
2024-02-02 07:29:43 -08:00
|
|
|
assert_eq!(Shape::Pawn.ray_to_square(Square::F7, Square::E8), None);
|
|
|
|
assert_eq!(Shape::Knight.ray_to_square(Square::F6, Square::E8), None);
|
2024-02-01 08:42:19 -08:00
|
|
|
}
|
|
|
|
|
2024-01-17 08:44:27 -08:00
|
|
|
mod pawn {
|
2024-01-28 00:25:53 -08:00
|
|
|
use crate::test_position;
|
2024-01-24 09:18:12 -08:00
|
|
|
use chessfriend_bitboard::{bitboard, BitBoard};
|
2024-01-24 17:08:27 -08:00
|
|
|
use chessfriend_core::{piece, 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-28 00:25:53 -08:00
|
|
|
let piece = piece!(White Pawn on E4);
|
|
|
|
let sight = pos.sight_of_piece(&piece);
|
2024-01-17 08:44:27 -08:00
|
|
|
|
|
|
|
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-28 00:25:53 -08:00
|
|
|
let piece = piece!(White Pawn on E4);
|
|
|
|
let sight = pos.sight_of_piece(&piece);
|
2024-01-17 08:44:27 -08:00
|
|
|
|
|
|
|
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);
|
2024-01-28 00:25:53 -08:00
|
|
|
let piece = piece!(White Pawn on E5);
|
|
|
|
let sight = pos.sight_of_piece(&piece);
|
2024-01-17 08:44:27 -08:00
|
|
|
|
|
|
|
assert_eq!(sight, bitboard!(D6, F6));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
mod knight {
|
2024-01-24 09:18:12 -08:00
|
|
|
use chessfriend_bitboard::bitboard;
|
2024-01-24 17:08:27 -08:00
|
|
|
use chessfriend_core::piece;
|
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 {
|
2024-02-01 08:42:19 -08:00
|
|
|
use super::*;
|
2024-01-17 08:44:27 -08:00
|
|
|
|
|
|
|
sight_test!(
|
|
|
|
c2_bishop,
|
|
|
|
piece!(Black Bishop on C2),
|
|
|
|
bitboard!(D1, B3, A4, B1, D3, E4, F5, G6, H7)
|
|
|
|
);
|
2024-02-01 08:42:19 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ray_to_square() {
|
2024-02-02 07:29:43 -08:00
|
|
|
let generated_ray = Shape::Bishop.ray_to_square(Square::C5, Square::E7);
|
2024-02-01 08:42:19 -08:00
|
|
|
let expected_ray = bitboard![D6, E7];
|
|
|
|
assert_eq!(generated_ray, Some(expected_ray));
|
|
|
|
}
|
2024-01-17 08:44:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
mod rook {
|
2024-02-01 08:42:19 -08:00
|
|
|
use super::*;
|
2024-01-28 00:25:53 -08:00
|
|
|
use crate::test_position;
|
2024-01-17 08:44:27 -08:00
|
|
|
|
|
|
|
sight_test!(
|
|
|
|
g3_rook,
|
|
|
|
piece!(White Rook on G3),
|
|
|
|
bitboard!(G1, G2, G4, G5, G6, G7, G8, A3, B3, C3, D3, E3, F3, H3)
|
|
|
|
);
|
2024-01-28 00:25:53 -08:00
|
|
|
|
|
|
|
sight_test!(
|
|
|
|
e4_rook_with_e1_white_king_e7_black_king,
|
|
|
|
test_position![
|
|
|
|
White Rook on E4,
|
|
|
|
White King on E1,
|
|
|
|
Black King on E7,
|
|
|
|
],
|
|
|
|
piece!(White Rook on E4),
|
|
|
|
bitboard!(A4, B4, C4, D4, F4, G4, H4, E2, E3, E5, E6, E7)
|
|
|
|
);
|
2024-02-01 08:42:19 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ray_to_square() {
|
2024-02-02 07:29:43 -08:00
|
|
|
let generated_ray = Shape::Rook.ray_to_square(Square::C2, Square::C6);
|
2024-02-01 08:42:19 -08:00
|
|
|
let expected_ray = bitboard![C3, C4, C5, C6];
|
|
|
|
assert_eq!(generated_ray, Some(expected_ray));
|
|
|
|
|
2024-02-02 07:29:43 -08:00
|
|
|
let generated_ray = Shape::Rook.ray_to_square(Square::D2, Square::H2);
|
2024-02-01 08:42:19 -08:00
|
|
|
let expected_ray = bitboard![E2, F2, G2, H2];
|
|
|
|
assert_eq!(generated_ray, Some(expected_ray));
|
|
|
|
|
2024-02-02 07:29:43 -08:00
|
|
|
let generated_ray = Shape::Rook.ray_to_square(Square::G6, Square::B6);
|
2024-02-01 08:42:19 -08:00
|
|
|
let expected_ray = bitboard![B6, C6, D6, E6, F6];
|
|
|
|
assert_eq!(generated_ray, Some(expected_ray));
|
|
|
|
|
2024-02-02 07:29:43 -08:00
|
|
|
let generated_ray = Shape::Rook.ray_to_square(Square::A6, Square::A3);
|
2024-02-01 08:42:19 -08:00
|
|
|
let expected_ray = bitboard![A5, A4, A3];
|
|
|
|
assert_eq!(generated_ray, Some(expected_ray));
|
|
|
|
}
|
2024-01-28 00:25:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
mod king {
|
|
|
|
use chessfriend_bitboard::bitboard;
|
|
|
|
use chessfriend_core::piece;
|
|
|
|
|
|
|
|
sight_test!(
|
|
|
|
e1_king,
|
|
|
|
piece!(White King on E1),
|
|
|
|
bitboard![D1, D2, E2, F2, F1]
|
|
|
|
);
|
2024-01-17 08:44:27 -08:00
|
|
|
}
|
|
|
|
}
|