From 63228c2d9e820d3827169393cb9610220f033e0a Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 16 Jul 2022 14:34:33 +0200 Subject: [PATCH] Add 'File::{left,right}' --- src/board/file.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/board/file.rs b/src/board/file.rs index a7ab385..ac3e91e 100644 --- a/src/board/file.rs +++ b/src/board/file.rs @@ -50,6 +50,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 { @@ -76,6 +88,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]);