Add 'Direction::slide_{square,board}'

This commit is contained in:
Bruno BELANYI 2022-07-18 00:27:46 +02:00
parent 896f615bba
commit 0dde0d5dbd

View file

@ -106,11 +106,39 @@ impl Direction {
} }
} }
} }
/// Slide a board along the given [Direction], i.e: return all successive applications of
/// [Direction::move_square] until no new squares can be reached.
/// It does not make sense to use this method with knight-only directions, and it will panic in
/// debug-mode if it happens.
#[inline(always)]
pub fn slide_square(self, square: Square) -> Bitboard {
self.slide_board(square.into_bitboard())
}
/// Slide a board along the given [Direction], i.e: return all successive applications of
/// [Direction::move_board] until no new squares can be reached.
/// It does not make sense to use this method with knight-only directions, and it will panic in
/// debug-mode if it happens.
#[inline(always)]
pub fn slide_board(self, mut board: Bitboard) -> Bitboard {
debug_assert!(!Self::KNIGHT_DIRECTIONS.contains(&self));
let mut res = Default::default();
while board != Bitboard::EMPTY {
board = self.move_board(board);
res = res | board;
}
res
}
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use crate::board::{File, Rank};
#[test] #[test]
fn north() { fn north() {
@ -591,4 +619,40 @@ mod test {
Bitboard::EMPTY, Bitboard::EMPTY,
); );
} }
#[test]
fn slide() {
assert_eq!(
Direction::North.slide_square(Square::A1),
File::A.into_bitboard() - Square::A1
);
assert_eq!(
Direction::West.slide_square(Square::H1),
Rank::First.into_bitboard() - Square::H1
);
assert_eq!(
Direction::South.slide_square(Square::A8),
File::A.into_bitboard() - Square::A8
);
assert_eq!(
Direction::East.slide_square(Square::A1),
Rank::First.into_bitboard() - Square::A1
);
assert_eq!(
Direction::NorthWest.slide_square(Square::H1),
Bitboard::ANTI_DIAGONAL - Square::H1
);
assert_eq!(
Direction::SouthWest.slide_square(Square::H8),
Bitboard::DIAGONAL - Square::H8
);
assert_eq!(
Direction::SouthEast.slide_square(Square::A8),
Bitboard::ANTI_DIAGONAL - Square::A8
);
assert_eq!(
Direction::NorthEast.slide_square(Square::A1),
Bitboard::DIAGONAL - Square::A1
);
}
} }