1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use super::{BitBoard, File, Rank};
/// All the directions.
pub const DIRS: [Direction; 8] = [
Direction::Up,
Direction::Down,
Direction::Left,
Direction::Right,
Direction::UpLeft,
Direction::UpRight,
Direction::DownLeft,
Direction::DownRight,
];
/// The [`Direction`] enum represents a direction on the chess board.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Direction {
Up,
Down,
Left,
Right,
UpLeft,
UpRight,
DownLeft,
DownRight,
}
impl Direction {
/// Gets a [`usize`] that used to index arrays by the [`Direction`].
///
/// # Examples
/// ```no_run
/// use rchess::Direction;
///
/// let mut direction_counts = [0;8];
/// direction_counts[Direction::Up.index()] += 1;
/// direction_counts[Direction::DownRight.index()] += 1;
/// ```
pub const fn index(&self) -> usize {
*self as usize
}
/// Gets a [`BitBoard`] containing the squares of the edge a [`Direction`] will eventually hit.
///
/// # Examples
/// ```no_run
/// use rchess::{Direction, Square, BitBoard};
///
/// let mut loc = BitBoard::from_square(Square::E5);
/// while !loc.overlaps(Direction::Up.edge()) {
/// // Do something until we hit the top edge.
/// loc.shift_dir(Direction::Up);
/// }
/// ```
pub const fn edge(&self) -> BitBoard {
match self {
Direction::Up => BitBoard::from_rank(Rank::Eighth),
Direction::Down => BitBoard::from_rank(Rank::First),
Direction::Left => BitBoard::from_file(File::A),
Direction::Right => BitBoard::from_file(File::H),
Direction::UpLeft => BitBoard::from_rank(Rank::Eighth).or(BitBoard::from_file(File::A)),
Direction::UpRight => {
BitBoard::from_rank(Rank::Eighth).or(BitBoard::from_file(File::H))
}
Direction::DownLeft => {
BitBoard::from_rank(Rank::First).or(BitBoard::from_file(File::A))
}
Direction::DownRight => {
BitBoard::from_rank(Rank::First).or(BitBoard::from_file(File::H))
}
}
}
}