From 1ab024fce8cedd9ad0bd3ea16803a66222176b67 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 17 Jul 2022 23:27:33 +0200 Subject: [PATCH] Add 'Direction::iter_{rook,bishop,royalty,knight}' --- src/board/directions.rs | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/board/directions.rs b/src/board/directions.rs index 3345256..d339bc1 100644 --- a/src/board/directions.rs +++ b/src/board/directions.rs @@ -24,6 +24,49 @@ pub enum Direction { } impl Direction { + /// Directions that a rook could use. + pub const ROOK_DIRECTIONS: [Self; 4] = [Self::North, Self::West, Self::South, Self::East]; + + /// Directions that a bishop could use. + pub const BISHOP_DIRECTIONS: [Self; 4] = [ + Self::NorthWest, + Self::SouthWest, + Self::SouthEast, + Self::NorthEast, + ]; + + /// Directions that a knight could use. + pub const KNIGHT_DIRECTIONS: [Self; 8] = [ + Self::NorthNorthWest, + Self::NorthWestWest, + Self::SouthWestWest, + Self::SouthSouthWest, + Self::SouthSouthEast, + Self::SouthEastEast, + Self::NorthEastEast, + Self::NorthNorthEast, + ]; + + /// Iterate over all directions a rook can take. + pub fn iter_rook() -> impl Iterator { + Self::ROOK_DIRECTIONS.iter().cloned() + } + + /// Iterate over all directions a bishop can take. + pub fn iter_bishop() -> impl Iterator { + Self::BISHOP_DIRECTIONS.iter().cloned() + } + + /// Iterate over all directions a queen or king can take. + pub fn iter_royalty() -> impl Iterator { + Self::iter_rook().chain(Self::iter_bishop()) + } + + /// Iterate over all directions a knight can take. + pub fn iter_knight() -> impl Iterator { + Self::KNIGHT_DIRECTIONS.iter().cloned() + } + /// Move every piece on a board along the given direction. Do not wrap around the board. #[inline(always)] pub fn move_board(self, board: Bitboard) -> Bitboard {