From aa3b464bb8e10f6ca67841e5fdc2c4a989c4f4bd Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 18 Jul 2022 13:50:23 +0200 Subject: [PATCH] Add 'CastleRights::unmoved_rooks' --- src/board/castle_rights.rs | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/board/castle_rights.rs b/src/board/castle_rights.rs index 9663bc0..0e44e03 100644 --- a/src/board/castle_rights.rs +++ b/src/board/castle_rights.rs @@ -1,3 +1,5 @@ +use super::{Bitboard, Color, File, Square}; + /// Current castle rights for a player. #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum CastleRights { @@ -62,6 +64,22 @@ impl CastleRights { // SAFETY: we know the value is in-bounds unsafe { Self::from_index_unchecked(self.index() & !to_remove.index()) } } + + /// Which rooks have not been moved for a given [CastleRights] and [Color]. + #[inline(always)] + pub fn unmoved_rooks(self, color: Color) -> Bitboard { + let rank = color.first_rank(); + + let king_side_square = Square::new(File::H, rank); + let queen_side_square = Square::new(File::A, rank); + + match self { + Self::NoSide => Bitboard::EMPTY, + Self::KingSide => king_side_square.into_bitboard(), + Self::QueenSide => queen_side_square.into_bitboard(), + Self::BothSides => king_side_square | queen_side_square, + } + } } #[cfg(test)] @@ -139,4 +157,40 @@ mod test { CastleRights::KingSide ); } + + #[test] + fn unmoved_rooks() { + assert_eq!( + CastleRights::NoSide.unmoved_rooks(Color::White), + Bitboard::EMPTY + ); + assert_eq!( + CastleRights::NoSide.unmoved_rooks(Color::Black), + Bitboard::EMPTY + ); + assert_eq!( + CastleRights::KingSide.unmoved_rooks(Color::White), + Square::H1.into_bitboard() + ); + assert_eq!( + CastleRights::KingSide.unmoved_rooks(Color::Black), + Square::H8.into_bitboard() + ); + assert_eq!( + CastleRights::QueenSide.unmoved_rooks(Color::White), + Square::A1.into_bitboard() + ); + assert_eq!( + CastleRights::QueenSide.unmoved_rooks(Color::Black), + Square::A8.into_bitboard() + ); + assert_eq!( + CastleRights::BothSides.unmoved_rooks(Color::White), + Square::A1 | Square::H1 + ); + assert_eq!( + CastleRights::BothSides.unmoved_rooks(Color::Black), + Square::A8 | Square::H8 + ); + } }