[position] Implement a SliderRayToSquare trait
This trait declares ray_to_square() which should return a BitBoard representing a ray from a Square to another Square. The ray should include the target Square. PlacedPiece implements this trait.
This commit is contained in:
parent
cac13b4bc7
commit
722a90b860
3 changed files with 141 additions and 7 deletions
|
@ -39,7 +39,7 @@ impl BitBoard {
|
|||
library::FILES[*file as usize]
|
||||
}
|
||||
|
||||
pub fn ray(sq: Square, dir: Direction) -> BitBoard {
|
||||
pub fn ray(sq: Square, dir: Direction) -> &'static BitBoard {
|
||||
library::library().ray(sq, dir)
|
||||
}
|
||||
|
||||
|
@ -107,6 +107,24 @@ impl BitBoard {
|
|||
pub fn occupied_squares_trailing(&self) -> impl Iterator<Item = Square> {
|
||||
LeadingBitScanner::new(self.0).map(|idx| unsafe { Square::from_index(idx as u8) })
|
||||
}
|
||||
|
||||
pub fn first_occupied_square(&self) -> Option<Square> {
|
||||
let leading_zeros = self.0.leading_zeros() as u8;
|
||||
if leading_zeros < Square::NUM as u8 {
|
||||
unsafe { Some(Square::from_index(Square::NUM as u8 - leading_zeros - 1)) }
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn first_occupied_square_trailing(&self) -> Option<Square> {
|
||||
let trailing_zeros = self.0.trailing_zeros() as u8;
|
||||
if trailing_zeros < Square::NUM as u8 {
|
||||
unsafe { Some(Square::from_index(trailing_zeros)) }
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for BitBoard {
|
||||
|
@ -377,4 +395,14 @@ mod tests {
|
|||
assert_eq!(BitBoard::from(Square::A1), BitBoard(0b1));
|
||||
assert_eq!(BitBoard::from(Square::H8), BitBoard(1 << 63));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn first_occupied_squares() {
|
||||
let bb = bitboard![A8, E1];
|
||||
assert_eq!(bb.first_occupied_square(), Some(Square::A8));
|
||||
assert_eq!(bb.first_occupied_square_trailing(), Some(Square::E1));
|
||||
|
||||
let bb = bitboard![D6, E7, F8];
|
||||
assert_eq!(bb.first_occupied_square_trailing(), Some(Square::D6));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -220,8 +220,8 @@ impl MoveLibrary {
|
|||
ray
|
||||
}
|
||||
|
||||
pub(super) fn ray(&self, sq: Square, dir: Direction) -> BitBoard {
|
||||
self.rays[sq as usize][dir as usize]
|
||||
pub(super) fn ray(&self, sq: Square, dir: Direction) -> &BitBoard {
|
||||
&self.rays[sq as usize][dir as usize]
|
||||
}
|
||||
|
||||
pub(super) fn pawn_pushes(&self, sq: Square, color: Color) -> BitBoard {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue