From 562182d26b63bcd7e521e91558f568336b3ba875 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 18 Jul 2022 13:27:37 +0200 Subject: [PATCH] Add 'CastleRights::has_{king,queen}_side' --- src/board/castle_rights.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/board/castle_rights.rs b/src/board/castle_rights.rs index 9ea7ad6..10a602e 100644 --- a/src/board/castle_rights.rs +++ b/src/board/castle_rights.rs @@ -31,6 +31,18 @@ impl CastleRights { pub fn index(self) -> usize { self as usize } + + /// Can the player castle king-side. + #[inline(always)] + pub fn has_king_side(self) -> bool { + (self.index() & 1) != 0 + } + + /// Can the player castle king-side. + #[inline(always)] + pub fn has_queen_side(self) -> bool { + (self.index() & 2) != 0 + } } #[cfg(test)] @@ -52,4 +64,20 @@ mod test { assert_eq!(CastleRights::QueenSide.index(), 2); assert_eq!(CastleRights::BothSides.index(), 3); } + + #[test] + fn has_kingside() { + assert!(!CastleRights::NoSide.has_king_side()); + assert!(!CastleRights::QueenSide.has_king_side()); + assert!(CastleRights::KingSide.has_king_side()); + assert!(CastleRights::BothSides.has_king_side()); + } + + #[test] + fn has_queenside() { + assert!(!CastleRights::NoSide.has_queen_side()); + assert!(!CastleRights::KingSide.has_queen_side()); + assert!(CastleRights::QueenSide.has_queen_side()); + assert!(CastleRights::BothSides.has_queen_side()); + } }