[bitboard] Implement BitBoard::occupied_squares_direction
Iterate a BitBoard in a direction (from leading or trailing edge) based on a board direction.
This commit is contained in:
		
							parent
							
								
									146e4d34d3
								
							
						
					
					
						commit
						b6d27356ac
					
				
					 1 changed files with 32 additions and 0 deletions
				
			
		|  | @ -233,6 +233,38 @@ impl BitBoard { | |||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /// Iterate through the occupied squares in a direction specified by a
 | ||||
|     /// compass direction. This method is mose useful for bitboards of slider
 | ||||
|     /// rays so that iteration proceeds in order along the ray's direction.
 | ||||
|     ///
 | ||||
|     /// ## Examples
 | ||||
|     ///
 | ||||
|     /// ```
 | ||||
|     /// use chessfriend_bitboard::BitBoard;
 | ||||
|     /// use chessfriend_core::{Direction, Square};
 | ||||
|     ///
 | ||||
|     /// let ray = BitBoard::ray(Square::E4, Direction::North);
 | ||||
|     /// assert_eq!(
 | ||||
|     ///     ray.occupied_squares_direction(Direction::North).collect::<Vec<Square>>(),
 | ||||
|     ///     vec![Square::E5, Square::E6, Square::E7, Square::E8]
 | ||||
|     /// );
 | ||||
|     /// ```
 | ||||
|     ///
 | ||||
|     #[must_use] | ||||
|     pub fn occupied_squares_direction( | ||||
|         &self, | ||||
|         direction: Direction, | ||||
|     ) -> Box<dyn Iterator<Item = Square>> { | ||||
|         match direction { | ||||
|             Direction::North | Direction::NorthEast | Direction::NorthWest | Direction::East => { | ||||
|                 Box::new(self.occupied_squares_trailing()) | ||||
|             } | ||||
|             Direction::SouthEast | Direction::South | Direction::SouthWest | Direction::West => { | ||||
|                 Box::new(self.occupied_squares_leading()) | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     #[must_use] | ||||
|     pub fn occupied_squares_leading(&self) -> LeadingBitScanner { | ||||
|         LeadingBitScanner::new(self.0) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue