myopic_core/
lib.rs

1#[macro_use]
2extern crate itertools;
3#[macro_use]
4extern crate lazy_static;
5#[macro_use]
6pub extern crate enumset;
7
8mod bitboard;
9mod castlezone;
10pub mod hash;
11mod pieces;
12mod reflectable;
13mod square;
14
15use anyhow::anyhow;
16pub use bitboard::constants;
17pub use bitboard::BitBoard;
18pub use castlezone::CastleZone;
19pub use pieces::Piece;
20pub use reflectable::Reflectable;
21pub use square::Square;
22use std::fmt::{Display, Formatter};
23use std::str::FromStr;
24
25/// Represents the two different teams in a game of chess.
26#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
27pub enum Side {
28    White,
29    Black,
30}
31
32impl Display for Side {
33    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
34        match self {
35            Side::White => write!(f, "w"),
36            Side::Black => write!(f, "b"),
37        }
38    }
39}
40
41impl FromStr for Side {
42    type Err = anyhow::Error;
43
44    fn from_str(s: &str) -> Result<Self, Self::Err> {
45        match s {
46            "w" | "W" => Ok(Side::White),
47            "b" | "B" => Ok(Side::Black),
48            _ => Err(anyhow!("Cannot parse Side from {}", s)),
49        }
50    }
51}
52
53impl Side {
54    /// Get the vertical direction in which a pawn on this side moves
55    /// (north or south).
56    pub fn pawn_dir(self) -> Dir {
57        match self {
58            Side::White => Dir::N,
59            Side::Black => Dir::S,
60        }
61    }
62
63    /// Get the rank on which a pawn on this side starts the game.
64    pub fn pawn_first_rank(self) -> BitBoard {
65        match self {
66            Side::White => BitBoard::RANKS[1],
67            Side::Black => BitBoard::RANKS[6],
68        }
69    }
70
71    /// Get the rank to which a pawn on this side moves to following
72    /// it's special two rank first move.
73    pub fn pawn_third_rank(self) -> BitBoard {
74        match self {
75            Side::White => BitBoard::RANKS[3],
76            Side::Black => BitBoard::RANKS[4],
77        }
78    }
79
80    /// Get the rank a pawn on this side must be on for it to be able
81    /// to promote on it's next move.
82    pub fn pawn_promoting_from_rank(self) -> BitBoard {
83        match self {
84            Side::White => BitBoard::RANKS[6],
85            Side::Black => BitBoard::RANKS[1],
86        }
87    }
88
89    /// The rank a pawn on this side will end up on after promoting to
90    /// another piece.
91    pub fn pawn_promoting_dest_rank(self) -> BitBoard {
92        match self {
93            Side::White => BitBoard::RANKS[7],
94            Side::Black => BitBoard::RANKS[0],
95        }
96    }
97}
98
99/// Type representing a square on a chessboard.
100#[derive(Debug, EnumSetType, Hash, PartialOrd, Ord)]
101#[rustfmt::skip]
102pub enum Dir {
103    N, E, S, W, NE, SE, SW, NW, NNE, NEE, SEE, SSE, SSW, SWW, NWW, NNW
104}
105
106#[cfg(test)]
107mod test {
108    use crate::Side;
109
110    #[test]
111    fn display() {
112        assert_eq!("w", Side::White.to_string().as_str());
113        assert_eq!("b", Side::Black.to_string().as_str());
114    }
115
116    #[test]
117    fn from_str() {
118        assert_eq!(Side::White, "w".parse::<Side>().unwrap());
119        assert_eq!(Side::White, "W".parse::<Side>().unwrap());
120        assert_eq!(Side::Black, "b".parse::<Side>().unwrap());
121        assert_eq!(Side::Black, "b".parse::<Side>().unwrap());
122    }
123}