chess/
color.rs

1use std::ops::Not;
2
3/// Represent a color in Chess game.
4#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Debug)]
5pub enum Color {
6    White,
7    Black,
8}
9
10/// Numbers of [`Color`] in chess game.
11pub const NUM_COLORS: usize = 2;
12
13/// Enumerate all [`colors`][Color].
14pub const ALL_COLORS: [Color; NUM_COLORS] = [Color::White, Color::Black];
15
16impl Color {
17    /// Convert the [`Color`] to a [`usize`] for table lookups.
18    #[inline]
19    pub fn to_index(&self) -> usize {
20        *self as usize
21    }
22}
23
24impl Not for Color {
25    type Output = Self;
26
27    /// Get the other color.
28    #[inline]
29    fn not(self) -> Self {
30        match self {
31            Color::White => Color::Black,
32            Color::Black => Color::White,
33        }
34    }
35}