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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#[macro_use]
extern crate itertools;
#[macro_use]
extern crate lazy_static;
mod bitboard;
mod castlezone;
pub mod hash;
mod pieces;
mod reflectable;
mod square;
use anyhow::anyhow;
pub use bitboard::constants;
pub use bitboard::BitBoard;
pub use castlezone::{CastleZone, CastleZoneSet};
pub use pieces::Piece;
pub use reflectable::Reflectable;
pub use square::Square;
use std::fmt::{Display, Formatter};
use std::str::FromStr;
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd)]
pub enum Side {
White,
Black,
}
impl Display for Side {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Side::White => write!(f, "w"),
Side::Black => write!(f, "b"),
}
}
}
impl FromStr for Side {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"w" | "W" => Ok(Side::White),
"b" | "B" => Ok(Side::Black),
_ => Err(anyhow!("Cannot parse Side from {}", s)),
}
}
}
impl Side {
pub fn pawn_dir(self) -> Dir {
match self {
Side::White => Dir::N,
Side::Black => Dir::S,
}
}
pub fn pawn_first_rank(self) -> BitBoard {
match self {
Side::White => BitBoard::RANKS[1],
Side::Black => BitBoard::RANKS[6],
}
}
pub fn pawn_third_rank(self) -> BitBoard {
match self {
Side::White => BitBoard::RANKS[3],
Side::Black => BitBoard::RANKS[4],
}
}
pub fn pawn_promoting_from_rank(self) -> BitBoard {
match self {
Side::White => BitBoard::RANKS[6],
Side::Black => BitBoard::RANKS[1],
}
}
pub fn pawn_promoting_dest_rank(self) -> BitBoard {
match self {
Side::White => BitBoard::RANKS[7],
Side::Black => BitBoard::RANKS[0],
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[rustfmt::skip]
pub enum Dir {
N, E, S, W, NE, SE, SW, NW, NNE, NEE, SEE, SSE, SSW, SWW, NWW, NNW
}
#[cfg(test)]
mod test {
use crate::Side;
#[test]
fn display() {
assert_eq!("w", Side::White.to_string().as_str());
assert_eq!("b", Side::Black.to_string().as_str());
}
#[test]
fn from_str() {
assert_eq!(Side::White, "w".parse::<Side>().unwrap());
assert_eq!(Side::White, "W".parse::<Side>().unwrap());
assert_eq!(Side::Black, "b".parse::<Side>().unwrap());
assert_eq!(Side::Black, "b".parse::<Side>().unwrap());
}
}