From db0a7e9f60b2a43d579dbfeff37b2dc89d941e8a Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 16 Jul 2022 14:34:46 +0200 Subject: [PATCH] Add 'Rank::{up,down}' --- src/board/rank.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/board/rank.rs b/src/board/rank.rs index fab3cdd..ffc5314 100644 --- a/src/board/rank.rs +++ b/src/board/rank.rs @@ -50,6 +50,18 @@ impl Rank { self as usize } + /// Return the [Rank] one-row up, as seen from white's perspective. Wraps around the board. + pub fn up(self) -> Self { + // SAFETY: we know the value is in-bounds, through masking + unsafe { Self::from_index_unchecked(self.index().wrapping_add(1) & 7) } + } + + /// Return the [Rank] one-row down, as seen from white's perspective. Wraps around the board. + pub fn down(self) -> Self { + // SAFETY: we know the value is in-bounds, through masking + unsafe { Self::from_index_unchecked(self.index().wrapping_sub(1) & 7) } + } + /// Turn a [Rank] into a [Bitboard] of all squares in that rank. #[inline(always)] pub fn into_bitboard(self) -> Bitboard { @@ -76,6 +88,20 @@ mod test { assert_eq!(Rank::Eighth.index(), 7); } + #[test] + fn up() { + assert_eq!(Rank::First.up(), Rank::Second); + assert_eq!(Rank::Second.up(), Rank::Third); + assert_eq!(Rank::Eighth.up(), Rank::First); + } + + #[test] + fn down() { + assert_eq!(Rank::First.down(), Rank::Eighth); + assert_eq!(Rank::Second.down(), Rank::First); + assert_eq!(Rank::Eighth.down(), Rank::Seventh); + } + #[test] fn into_bitboard() { assert_eq!(Rank::First.into_bitboard(), Bitboard::RANKS[0]);