[core] Implement Slider::ray_direction()

This method returns iterators over the directions each kind of slider can move.
This commit is contained in:
Eryn Wells 2025-07-01 13:02:35 -07:00
parent a904e4a5bb
commit 89a9588e69

View file

@ -1,10 +1,9 @@
// Eryn Wells <eryn@erynwells.me>
use crate::{Direction, score::Score};
use std::{array, fmt, slice, str::FromStr};
use thiserror::Error;
use crate::score::Score;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Shape {
Pawn = 0,
@ -93,6 +92,54 @@ pub enum Slider {
Queen,
}
impl Slider {
pub const NUM: usize = 3;
pub const ALL: [Self; Self::NUM] = [Slider::Bishop, Slider::Rook, Slider::Queen];
/// Return the set of directions a slider can move.
///
/// ## Examples
///
/// ```
/// use chessfriend_core::{Direction, Slider};
///
/// assert_eq!(
/// Slider::Bishop.ray_directions().collect::<Vec<Direction>>(),
/// vec![
/// Direction::NorthWest,
/// Direction::NorthEast,
/// Direction::SouthEast,
/// Direction::SouthWest
/// ]
/// );
/// ```
///
#[must_use]
pub fn ray_directions(self) -> Box<dyn Iterator<Item = Direction>> {
match self {
Slider::Bishop => Box::new(
[
Direction::NorthWest,
Direction::NorthEast,
Direction::SouthEast,
Direction::SouthWest,
]
.into_iter(),
),
Slider::Rook => Box::new(
[
Direction::North,
Direction::East,
Direction::South,
Direction::West,
]
.into_iter(),
),
Slider::Queen => Box::new(Direction::ALL.into_iter()),
}
}
}
impl From<Slider> for Shape {
fn from(value: Slider) -> Self {
match value {