This commit is contained in:
Eryn Wells 2025-05-08 17:37:51 -07:00
parent d5cdf273c8
commit 091cc99cb3
42 changed files with 805 additions and 1662 deletions

View file

@ -9,15 +9,15 @@ use chessfriend_board::Board;
use chessfriend_core::{Color, Direction, PlacedPiece, Shape, Square};
macro_rules! ray_in_direction {
($square:expr, $blockers:expr, $direction:ident, $occupied_squares:tt) => {{
($square:expr, $blockers:expr, $direction:ident, $first_occupied_square:tt) => {{
let ray = BitBoard::ray($square, Direction::$direction);
if let Some(first_occupied_square) = BitBoard::$occupied_squares(&(ray & $blockers)).next()
{
let ray_blockers = ray & $blockers;
if let Some(first_occupied_square) = ray_blockers.$first_occupied_square() {
let remainder = BitBoard::ray(first_occupied_square, Direction::$direction);
let attack_ray = ray & !remainder;
attack_ray
} else {
*ray
ray
}
}};
}
@ -51,34 +51,34 @@ fn _knight_sight(knight_square: Square, blockers: BitBoard) -> BitBoard {
fn _bishop_sight(bishop_square: Square, occupancy: BitBoard) -> BitBoard {
#[rustfmt::skip]
let sight = ray_in_direction!(bishop_square, occupancy, NorthEast, occupied_squares_trailing)
| ray_in_direction!(bishop_square, occupancy, NorthWest, occupied_squares_trailing)
| ray_in_direction!(bishop_square, occupancy, SouthEast, occupied_squares)
| ray_in_direction!(bishop_square, occupancy, SouthWest, occupied_squares);
let sight = ray_in_direction!(bishop_square, occupancy, NorthEast, first_occupied_square_trailing)
| ray_in_direction!(bishop_square, occupancy, NorthWest, first_occupied_square_trailing)
| ray_in_direction!(bishop_square, occupancy, SouthEast, first_occupied_square_leading)
| ray_in_direction!(bishop_square, occupancy, SouthWest, first_occupied_square_leading);
sight
}
fn _rook_sight(rook_square: Square, occupancy: BitBoard) -> BitBoard {
#[rustfmt::skip]
let sight = ray_in_direction!(rook_square, occupancy, North, occupied_squares_trailing)
| ray_in_direction!(rook_square, occupancy, East, occupied_squares_trailing)
| ray_in_direction!(rook_square, occupancy, South, occupied_squares)
| ray_in_direction!(rook_square, occupancy, West, occupied_squares);
let sight = ray_in_direction!(rook_square, occupancy, North, first_occupied_square_trailing)
| ray_in_direction!(rook_square, occupancy, East, first_occupied_square_trailing)
| ray_in_direction!(rook_square, occupancy, South, first_occupied_square_leading)
| ray_in_direction!(rook_square, occupancy, West, first_occupied_square_leading);
sight
}
fn _queen_sight(queen_square: Square, occupancy: BitBoard) -> BitBoard {
#[rustfmt::skip]
let sight = ray_in_direction!(queen_square, occupancy, NorthWest, occupied_squares_trailing)
| ray_in_direction!(queen_square, occupancy, North, occupied_squares_trailing)
| ray_in_direction!(queen_square, occupancy, NorthEast, occupied_squares_trailing)
| ray_in_direction!(queen_square, occupancy, East, occupied_squares_trailing)
| ray_in_direction!(queen_square, occupancy, SouthEast, occupied_squares)
| ray_in_direction!(queen_square, occupancy, South, occupied_squares)
| ray_in_direction!(queen_square, occupancy, SouthWest, occupied_squares)
| ray_in_direction!(queen_square, occupancy, West, occupied_squares);
let sight = ray_in_direction!(queen_square, occupancy, NorthWest, first_occupied_square_trailing)
| ray_in_direction!(queen_square, occupancy, North, first_occupied_square_trailing)
| ray_in_direction!(queen_square, occupancy, NorthEast, first_occupied_square_trailing)
| ray_in_direction!(queen_square, occupancy, East, first_occupied_square_trailing)
| ray_in_direction!(queen_square, occupancy, SouthEast, first_occupied_square_leading)
| ray_in_direction!(queen_square, occupancy, South, first_occupied_square_leading)
| ray_in_direction!(queen_square, occupancy, SouthWest, first_occupied_square_leading)
| ray_in_direction!(queen_square, occupancy, West, first_occupied_square_leading);
sight
}
@ -129,9 +129,9 @@ impl SightExt for PlacedPiece {
Color::Black => self.black_pawn_sight(board, en_passant_square),
},
Shape::Knight => self.knight_sight(board),
Shape::Bishop => self.bishop_sight(board.all_pieces_bitboard()),
Shape::Rook => self.rook_sight(board.all_pieces_bitboard()),
Shape::Queen => self.queen_sight(board.all_pieces_bitboard()),
Shape::Bishop => self.bishop_sight(board.pieces.all_pieces()),
Shape::Rook => self.rook_sight(board.pieces.all_pieces()),
Shape::Queen => self.queen_sight(board.pieces.all_pieces()),
Shape::King => self.king_sight(board),
}
}
@ -139,46 +139,40 @@ impl SightExt for PlacedPiece {
impl KingSightExt for PlacedPiece {
fn king_sight(&self, board: &Board) -> BitBoard {
_king_sight(
self.square(),
board.all_pieces_of_color_bitboard(self.color()),
)
_king_sight(self.square, board.pieces.all_pieces_of_color(self.color))
}
}
impl KnightSightExt for PlacedPiece {
fn knight_sight(&self, board: &Board) -> BitBoard {
_knight_sight(
self.square(),
board.all_pieces_of_color_bitboard(self.color()),
)
_knight_sight(self.square, board.pieces.all_pieces_of_color(self.color))
}
}
impl PawnSightExt for PlacedPiece {
fn pawn_sight(&self, board: &Board, en_passant_square: Option<Square>) -> BitBoard {
match self.color() {
match self.color {
Color::White => self.white_pawn_sight(board, en_passant_square),
Color::Black => self.black_pawn_sight(board, en_passant_square),
}
}
fn white_pawn_sight(&self, board: &Board, en_passant_square: Option<Square>) -> BitBoard {
let opponent = self.color().other();
let opponent = self.color.other();
_white_pawn_sight(
self.square().into(),
board.all_pieces_bitboard(),
board.all_pieces_of_color_bitboard(opponent),
self.square.into(),
board.pieces.all_pieces(),
board.pieces.all_pieces_of_color(opponent),
en_passant_square.into(),
)
}
fn black_pawn_sight(&self, board: &Board, en_passant_square: Option<Square>) -> BitBoard {
let opponent = self.color().other();
let opponent = self.piece.color.other();
_black_pawn_sight(
self.square().into(),
board.all_pieces_bitboard(),
board.all_pieces_of_color_bitboard(opponent),
self.square.into(),
board.pieces.all_pieces(),
board.pieces.all_pieces_of_color(opponent),
en_passant_square.into(),
)
}
@ -186,19 +180,19 @@ impl PawnSightExt for PlacedPiece {
impl BishopSightExt for PlacedPiece {
fn bishop_sight(&self, occupancy: BitBoard) -> BitBoard {
_bishop_sight(self.square(), occupancy)
_bishop_sight(self.square, occupancy)
}
}
impl RookSightExt for PlacedPiece {
fn rook_sight(&self, occupancy: BitBoard) -> BitBoard {
_rook_sight(self.square(), occupancy)
_rook_sight(self.square, occupancy)
}
}
impl QueenSightExt for PlacedPiece {
fn queen_sight(&self, occupancy: BitBoard) -> BitBoard {
_queen_sight(self.square(), occupancy)
_queen_sight(self.square, occupancy)
}
}
@ -225,7 +219,7 @@ impl SliderRayToSquareExt for Shape {
ray!(origin, NorthWest),
]
.into_iter()
.find(|(&ray, _)| !(target_bitboard & ray).is_empty()),
.find(|(ray, _)| !(target_bitboard & ray).is_empty()),
Shape::Rook => [
ray!(origin, North),
ray!(origin, East),
@ -233,7 +227,7 @@ impl SliderRayToSquareExt for Shape {
ray!(origin, West),
]
.into_iter()
.find(|(&ray, _)| !(target_bitboard & ray).is_empty()),
.find(|(ray, _)| !(target_bitboard & ray).is_empty()),
Shape::Queen => [
ray!(origin, North),
ray!(origin, NorthEast),
@ -245,7 +239,7 @@ impl SliderRayToSquareExt for Shape {
ray!(origin, NorthWest),
]
.into_iter()
.find(|(&ray, _)| !(target_bitboard & ray).is_empty()),
.find(|(ray, _)| !(target_bitboard & ray).is_empty()),
_ => None,
};
@ -307,10 +301,9 @@ mod tests {
mod pawn {
use crate::test_position;
use chessfriend_bitboard::{bitboard, BitBoard};
use chessfriend_core::{piece, Square};
use chessfriend_moves::EnPassant;
use chessfriend_core::piece;
sight_test!(e4_pawn, piece!(White Pawn on E4), bitboard!(D5, F5));
sight_test!(e4_pawn, piece!(White Pawn on E4), bitboard![D5 F5]);
sight_test!(
e4_pawn_one_blocker,
@ -352,15 +345,14 @@ mod tests {
#[test]
fn e5_en_passant() {
let mut pos = test_position!(
let pos = test_position!(White, [
White Pawn on E5,
Black Pawn on D5,
);
pos.test_set_en_passant(EnPassant::from_target_square(Square::D6).unwrap());
], D6);
let piece = piece!(White Pawn on E5);
let sight = pos.sight_of_piece(&piece);
assert_eq!(sight, bitboard!(D6, F6));
assert_eq!(sight, bitboard!(D6 F6));
}
}
@ -372,7 +364,7 @@ mod tests {
sight_test!(
f6_knight,
piece!(Black Knight on F6),
bitboard!(H7, G8, E8, D7, D5, E4, G4, H5)
bitboard![H7 G8 E8 D7 D5 E4 G4 H5]
);
}
@ -382,13 +374,13 @@ mod tests {
sight_test!(
c2_bishop,
piece!(Black Bishop on C2),
bitboard!(D1, B3, A4, B1, D3, E4, F5, G6, H7)
bitboard![D1 B3 A4 B1 D3 E4 F5 G6 H7]
);
#[test]
fn ray_to_square() {
let generated_ray = Shape::Bishop.ray_to_square(Square::C5, Square::E7);
let expected_ray = bitboard![D6, E7];
let expected_ray = bitboard![D6 E7];
assert_eq!(generated_ray, Some(expected_ray));
}
}
@ -400,7 +392,7 @@ mod tests {
sight_test!(
g3_rook,
piece!(White Rook on G3),
bitboard!(G1, G2, G4, G5, G6, G7, G8, A3, B3, C3, D3, E3, F3, H3)
bitboard![G1 G2 G4 G5 G6 G7 G8 A3 B3 C3 D3 E3 F3 H3]
);
sight_test!(
@ -411,25 +403,25 @@ mod tests {
Black King on E7,
],
piece!(White Rook on E4),
bitboard!(A4, B4, C4, D4, F4, G4, H4, E2, E3, E5, E6, E7, E1)
bitboard![A4 B4 C4 D4 F4 G4 H4 E2 E3 E5 E6 E7 E1]
);
#[test]
fn ray_to_square() {
let generated_ray = Shape::Rook.ray_to_square(Square::C2, Square::C6);
let expected_ray = bitboard![C3, C4, C5, C6];
let expected_ray = bitboard![C3 C4 C5 C6];
assert_eq!(generated_ray, Some(expected_ray));
let generated_ray = Shape::Rook.ray_to_square(Square::D2, Square::H2);
let expected_ray = bitboard![E2, F2, G2, H2];
let expected_ray = bitboard![E2 F2 G2 H2];
assert_eq!(generated_ray, Some(expected_ray));
let generated_ray = Shape::Rook.ray_to_square(Square::G6, Square::B6);
let expected_ray = bitboard![B6, C6, D6, E6, F6];
let expected_ray = bitboard![B6 C6 D6 E6 F6];
assert_eq!(generated_ray, Some(expected_ray));
let generated_ray = Shape::Rook.ray_to_square(Square::A6, Square::A3);
let expected_ray = bitboard![A5, A4, A3];
let expected_ray = bitboard![A5 A4 A3];
assert_eq!(generated_ray, Some(expected_ray));
}
}
@ -438,10 +430,6 @@ mod tests {
use chessfriend_bitboard::bitboard;
use chessfriend_core::piece;
sight_test!(
e1_king,
piece!(White King on E1),
bitboard![D1, D2, E2, F2, F1]
);
sight_test!(e1_king, piece!(White King on E1), bitboard![D1 D2 E2 F2 F1]);
}
}