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