[position] Rewrite sight methods in terms of Board and pass BitBoard arguments by value
This commit is contained in:
parent
30188d478e
commit
90657e3818
1 changed files with 60 additions and 72 deletions
|
@ -4,8 +4,8 @@
|
|||
//! that a piece can see. In other words, it's the set of squares attacked or
|
||||
//! controled by a piece.
|
||||
|
||||
use crate::position::piece_sets::PieceBitBoards;
|
||||
use chessfriend_bitboard::BitBoard;
|
||||
use chessfriend_board::Board;
|
||||
use chessfriend_core::{Color, Direction, PlacedPiece, Shape, Square};
|
||||
|
||||
macro_rules! ray_in_direction {
|
||||
|
@ -24,10 +24,10 @@ macro_rules! ray_in_direction {
|
|||
|
||||
/// Compute sight of a white pawn.
|
||||
fn _white_pawn_sight(
|
||||
pawn: &BitBoard,
|
||||
occupancy: &BitBoard,
|
||||
blockers: &BitBoard,
|
||||
en_passant_square: &BitBoard,
|
||||
pawn: BitBoard,
|
||||
occupancy: BitBoard,
|
||||
blockers: BitBoard,
|
||||
en_passant_square: BitBoard,
|
||||
) -> BitBoard {
|
||||
let possible_squares = !occupancy | blockers | en_passant_square;
|
||||
let pawn = pawn.shift_north_west_one() | pawn.shift_north_east_one();
|
||||
|
@ -35,21 +35,21 @@ fn _white_pawn_sight(
|
|||
}
|
||||
|
||||
fn _black_pawn_sight(
|
||||
pawn: &BitBoard,
|
||||
occupancy: &BitBoard,
|
||||
blockers: &BitBoard,
|
||||
en_passant_square: &BitBoard,
|
||||
pawn: BitBoard,
|
||||
occupancy: BitBoard,
|
||||
blockers: BitBoard,
|
||||
en_passant_square: BitBoard,
|
||||
) -> BitBoard {
|
||||
let possible_squares = !occupancy | blockers | en_passant_square;
|
||||
let pawn = pawn.shift_south_west_one() | pawn.shift_south_east_one();
|
||||
pawn & possible_squares
|
||||
}
|
||||
|
||||
fn _knight_sight(knight_square: Square, blockers: &BitBoard) -> BitBoard {
|
||||
fn _knight_sight(knight_square: Square, blockers: BitBoard) -> BitBoard {
|
||||
BitBoard::knight_moves(knight_square) & !blockers
|
||||
}
|
||||
|
||||
fn _bishop_sight(bishop_square: Square, occupancy: &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)
|
||||
|
@ -59,7 +59,7 @@ fn _bishop_sight(bishop_square: Square, occupancy: &BitBoard) -> BitBoard {
|
|||
sight
|
||||
}
|
||||
|
||||
fn _rook_sight(rook_square: Square, occupancy: &BitBoard) -> BitBoard {
|
||||
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)
|
||||
|
@ -69,7 +69,7 @@ fn _rook_sight(rook_square: Square, occupancy: &BitBoard) -> BitBoard {
|
|||
sight
|
||||
}
|
||||
|
||||
fn _queen_sight(queen_square: Square, occupancy: &BitBoard) -> BitBoard {
|
||||
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)
|
||||
|
@ -83,48 +83,38 @@ fn _queen_sight(queen_square: Square, occupancy: &BitBoard) -> BitBoard {
|
|||
sight
|
||||
}
|
||||
|
||||
fn _king_sight(king_square: Square, blockers: &BitBoard) -> BitBoard {
|
||||
fn _king_sight(king_square: Square, blockers: BitBoard) -> BitBoard {
|
||||
BitBoard::king_moves(king_square) & !blockers
|
||||
}
|
||||
pub(crate) trait BishopSightExt {
|
||||
fn bishop_sight(&self, occupancy: &BitBoard) -> BitBoard;
|
||||
fn bishop_sight(&self, occupancy: BitBoard) -> BitBoard;
|
||||
}
|
||||
|
||||
pub(crate) trait KingSightExt {
|
||||
fn king_sight(&self, pieces: &PieceBitBoards) -> BitBoard;
|
||||
fn king_sight(&self, board: &Board) -> BitBoard;
|
||||
}
|
||||
|
||||
pub(crate) trait KnightSightExt {
|
||||
fn knight_sight(&self, pieces: &PieceBitBoards) -> BitBoard;
|
||||
fn knight_sight(&self, board: &Board) -> BitBoard;
|
||||
}
|
||||
|
||||
pub(crate) trait PawnSightExt {
|
||||
fn pawn_sight(&self, pieces: &PieceBitBoards, en_passant_square: Option<Square>) -> BitBoard;
|
||||
|
||||
fn white_pawn_sight(
|
||||
&self,
|
||||
pieces: &PieceBitBoards,
|
||||
en_passant_square: Option<Square>,
|
||||
) -> BitBoard;
|
||||
|
||||
fn black_pawn_sight(
|
||||
&self,
|
||||
pieces: &PieceBitBoards,
|
||||
en_passant_square: Option<Square>,
|
||||
) -> BitBoard;
|
||||
fn pawn_sight(&self, board: &Board, en_passant_square: Option<Square>) -> BitBoard;
|
||||
fn white_pawn_sight(&self, board: &Board, en_passant_square: Option<Square>) -> BitBoard;
|
||||
fn black_pawn_sight(&self, board: &Board, en_passant_square: Option<Square>) -> BitBoard;
|
||||
}
|
||||
pub(crate) trait QueenSightExt {
|
||||
fn queen_sight(&self, occupancy: &BitBoard) -> BitBoard;
|
||||
fn queen_sight(&self, occupancy: BitBoard) -> BitBoard;
|
||||
}
|
||||
|
||||
pub(crate) trait RookSightExt {
|
||||
fn rook_sight(&self, occupancy: &BitBoard) -> BitBoard;
|
||||
fn rook_sight(&self, occupancy: BitBoard) -> BitBoard;
|
||||
}
|
||||
|
||||
pub(crate) trait SliderSightExt: BishopSightExt + QueenSightExt + RookSightExt {}
|
||||
|
||||
pub(crate) trait SightExt {
|
||||
fn sight(&self, pieces: &PieceBitBoards, en_passant_square: Option<Square>) -> BitBoard;
|
||||
fn sight(&self, board: &Board, en_passant_square: Option<Square>) -> BitBoard;
|
||||
}
|
||||
|
||||
pub(crate) trait SliderRayToSquareExt {
|
||||
|
@ -132,84 +122,82 @@ pub(crate) trait SliderRayToSquareExt {
|
|||
}
|
||||
|
||||
impl SightExt for PlacedPiece {
|
||||
fn sight(&self, pieces: &PieceBitBoards, en_passant_square: Option<Square>) -> BitBoard {
|
||||
fn sight(&self, board: &Board, en_passant_square: Option<Square>) -> BitBoard {
|
||||
match self.shape() {
|
||||
Shape::Pawn => match self.color() {
|
||||
Color::White => self.white_pawn_sight(pieces, en_passant_square),
|
||||
Color::Black => self.black_pawn_sight(pieces, en_passant_square),
|
||||
Color::White => self.white_pawn_sight(board, en_passant_square),
|
||||
Color::Black => self.black_pawn_sight(board, en_passant_square),
|
||||
},
|
||||
Shape::Knight => self.knight_sight(pieces),
|
||||
Shape::Bishop => self.bishop_sight(pieces.all_pieces()),
|
||||
Shape::Rook => self.rook_sight(pieces.all_pieces()),
|
||||
Shape::Queen => self.queen_sight(pieces.all_pieces()),
|
||||
Shape::King => self.king_sight(pieces),
|
||||
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::King => self.king_sight(board),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl KingSightExt for PlacedPiece {
|
||||
fn king_sight(&self, pieces: &PieceBitBoards) -> BitBoard {
|
||||
_king_sight(self.square(), pieces.all_pieces_of_color(self.color()))
|
||||
fn king_sight(&self, board: &Board) -> BitBoard {
|
||||
_king_sight(
|
||||
self.square(),
|
||||
board.all_pieces_of_color_bitboard(self.color()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl KnightSightExt for PlacedPiece {
|
||||
fn knight_sight(&self, pieces: &PieceBitBoards) -> BitBoard {
|
||||
_knight_sight(self.square(), pieces.all_pieces_of_color(self.color()))
|
||||
fn knight_sight(&self, board: &Board) -> BitBoard {
|
||||
_knight_sight(
|
||||
self.square(),
|
||||
board.all_pieces_of_color_bitboard(self.color()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl PawnSightExt for PlacedPiece {
|
||||
fn pawn_sight(&self, pieces: &PieceBitBoards, en_passant_square: Option<Square>) -> BitBoard {
|
||||
fn pawn_sight(&self, board: &Board, en_passant_square: Option<Square>) -> BitBoard {
|
||||
match self.color() {
|
||||
Color::White => self.white_pawn_sight(pieces, en_passant_square),
|
||||
Color::Black => self.black_pawn_sight(pieces, en_passant_square),
|
||||
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,
|
||||
pieces: &PieceBitBoards,
|
||||
en_passant_square: Option<Square>,
|
||||
) -> BitBoard {
|
||||
fn white_pawn_sight(&self, board: &Board, en_passant_square: Option<Square>) -> BitBoard {
|
||||
let opponent = self.color().other();
|
||||
_white_pawn_sight(
|
||||
&self.square().into(),
|
||||
pieces.all_pieces(),
|
||||
pieces.all_pieces_of_color(opponent),
|
||||
&en_passant_square.into(),
|
||||
self.square().into(),
|
||||
board.all_pieces_bitboard(),
|
||||
board.all_pieces_of_color_bitboard(opponent),
|
||||
en_passant_square.into(),
|
||||
)
|
||||
}
|
||||
|
||||
fn black_pawn_sight(
|
||||
&self,
|
||||
pieces: &PieceBitBoards,
|
||||
en_passant_square: Option<Square>,
|
||||
) -> BitBoard {
|
||||
fn black_pawn_sight(&self, board: &Board, en_passant_square: Option<Square>) -> BitBoard {
|
||||
let opponent = self.color().other();
|
||||
_black_pawn_sight(
|
||||
&self.square().into(),
|
||||
pieces.all_pieces(),
|
||||
pieces.all_pieces_of_color(opponent),
|
||||
&en_passant_square.into(),
|
||||
self.square().into(),
|
||||
board.all_pieces_bitboard(),
|
||||
board.all_pieces_of_color_bitboard(opponent),
|
||||
en_passant_square.into(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl BishopSightExt for PlacedPiece {
|
||||
fn bishop_sight(&self, occupancy: &BitBoard) -> BitBoard {
|
||||
fn bishop_sight(&self, occupancy: BitBoard) -> BitBoard {
|
||||
_bishop_sight(self.square(), occupancy)
|
||||
}
|
||||
}
|
||||
|
||||
impl RookSightExt for PlacedPiece {
|
||||
fn rook_sight(&self, occupancy: &BitBoard) -> BitBoard {
|
||||
fn rook_sight(&self, occupancy: BitBoard) -> BitBoard {
|
||||
_rook_sight(self.square(), occupancy)
|
||||
}
|
||||
}
|
||||
|
||||
impl QueenSightExt for PlacedPiece {
|
||||
fn queen_sight(&self, occupancy: &BitBoard) -> BitBoard {
|
||||
fn queen_sight(&self, occupancy: BitBoard) -> BitBoard {
|
||||
_queen_sight(self.square(), occupancy)
|
||||
}
|
||||
}
|
||||
|
@ -271,19 +259,19 @@ impl SliderRayToSquareExt for Shape {
|
|||
}
|
||||
|
||||
impl BishopSightExt for Square {
|
||||
fn bishop_sight(&self, occupancy: &BitBoard) -> BitBoard {
|
||||
fn bishop_sight(&self, occupancy: BitBoard) -> BitBoard {
|
||||
_bishop_sight(*self, occupancy)
|
||||
}
|
||||
}
|
||||
|
||||
impl QueenSightExt for Square {
|
||||
fn queen_sight(&self, occupancy: &BitBoard) -> BitBoard {
|
||||
fn queen_sight(&self, occupancy: BitBoard) -> BitBoard {
|
||||
_queen_sight(*self, occupancy)
|
||||
}
|
||||
}
|
||||
|
||||
impl RookSightExt for Square {
|
||||
fn rook_sight(&self, occupancy: &BitBoard) -> BitBoard {
|
||||
fn rook_sight(&self, occupancy: BitBoard) -> BitBoard {
|
||||
_rook_sight(*self, occupancy)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue