From 7a7e7f3665a1c902ce1dfeb32a2c4da2537856c4 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 11ba5e8..c2499d7 100644 --- a/src/board/rank.rs +++ b/src/board/rank.rs @@ -54,6 +54,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 { @@ -80,6 +92,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]);