Add 'File::{left,right}'

This commit is contained in:
Bruno BELANYI 2022-07-16 14:34:33 +02:00
parent 6501466d3e
commit 2b4797ec47

View file

@ -54,6 +54,18 @@ impl File {
self as usize
}
/// Return the [File] to the left, as seen from white's perspective. Wraps around the board.
pub fn left(self) -> Self {
// SAFETY: we know the value is in-bounds, through masking
unsafe { Self::from_index_unchecked(self.index().wrapping_sub(1) & 7) }
}
/// Return the [File] to the right, as seen from white's perspective. Wraps around the board.
pub fn right(self) -> Self {
// SAFETY: we know the value is in-bounds, through masking
unsafe { Self::from_index_unchecked(self.index().wrapping_add(1) & 7) }
}
/// Turn a [File] into a [Bitboard] of all squares in that file.
#[inline(always)]
pub fn into_bitboard(self) -> Bitboard {
@ -80,6 +92,20 @@ mod test {
assert_eq!(File::H.index(), 7);
}
#[test]
fn left() {
assert_eq!(File::A.left(), File::H);
assert_eq!(File::B.left(), File::A);
assert_eq!(File::H.left(), File::G);
}
#[test]
fn right() {
assert_eq!(File::A.right(), File::B);
assert_eq!(File::B.right(), File::C);
assert_eq!(File::H.right(), File::A);
}
#[test]
fn into_bitboard() {
assert_eq!(File::A.into_bitboard(), Bitboard::FILES[0]);