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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use crate::{BitBoard, Side, Square};
use anyhow::{anyhow, Error, Result};
use std::fmt::{Display, Formatter};
use std::str::FromStr;

mod kings;
mod knights;
mod pawns;
mod sliding;

/// Value type wrapping a single integer representing one of the 12
/// different pieces in a game of chess.
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
#[rustfmt::skip]
pub enum Piece {
    WP, WN, WB, WR, WQ, WK,
    BP, BN, BB, BR, BQ, BK,
}

impl Display for Piece {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", format!("{:?}", self).to_lowercase())
    }
}

impl FromStr for Piece {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let lower = s.to_lowercase();
        Piece::all()
            .find(|p| p.to_string() == lower)
            .ok_or(anyhow!("Cannot parse {} as piece", s))
    }
}

impl Piece {
    /// Create an iterator traversing over all pieces in order.
    pub fn all() -> impl Iterator<Item = Piece> {
        ALL.iter().cloned()
    }

    /// Create an iterator traversing over all white pieces in order.
    pub fn whites() -> impl Iterator<Item = Piece> {
        WHITE.iter().cloned()
    }

    /// Create an iterator traversing over all black pieces in order.
    pub fn blacks() -> impl Iterator<Item = Piece> {
        BLACK.iter().cloned()
    }

    /// Returns the king which belongs to the given side.
    pub fn king(side: Side) -> Piece {
        match side {
            Side::White => Piece::WK,
            Side::Black => Piece::BK,
        }
    }

    /// Returns the queen which belongs to the given side.
    pub fn queen(side: Side) -> Piece {
        match side {
            Side::White => Piece::WQ,
            Side::Black => Piece::BQ,
        }
    }

    /// Returns the rook belonging to the given side.
    pub fn rook(side: Side) -> Piece {
        match side {
            Side::White => Piece::WR,
            Side::Black => Piece::BR,
        }
    }

    /// Returns the pawn which belongs to the given side.
    pub fn pawn(side: Side) -> Piece {
        match side {
            Side::White => Piece::WP,
            Side::Black => Piece::BP,
        }
    }

    /// Returns a slice containing all pieces belonging to the given side.
    pub fn of(side: Side) -> impl Iterator<Item = Piece> {
        match side {
            Side::White => (&WHITE).iter().cloned(),
            Side::Black => (&BLACK).iter().cloned(),
        }
    }

    /// Returns the side that this piece belongs to.
    pub fn side(self) -> Side {
        if (self as u8) < 6 {
            Side::White
        } else {
            Side::Black
        }
    }

    /// Checks whether this piece is either a white or black pawn.
    pub fn is_pawn(self) -> bool {
        (self as u8) % 6 == 0
    }

    /// Checks whether this piece is either a white or black knight.
    pub fn is_knight(self) -> bool {
        (self as u8) % 6 == 1
    }

    /// Computes the control set for this piece given it's location and the
    /// locations of all the white and black pieces on the board.
    pub fn control(self, loc: Square, whites: BitBoard, blacks: BitBoard) -> BitBoard {
        Piece::CONTROL_FN[self as usize](loc, whites, blacks)
    }

    /// Computes the control set for this piece given it's location on an
    /// empty board.
    pub fn empty_control(self, loc: Square) -> BitBoard {
        self.control(loc, BitBoard::EMPTY, BitBoard::EMPTY)
    }

    /// Computes the set of legal moves for this piece given it's location
    /// and the locations of all the white and black pieces on the board.
    /// Note that this method does not take into account special restrictions
    /// for or due to the king, e.g. can't move in such a way to put the king
    /// into check.
    pub fn moves(self, loc: Square, whites: BitBoard, blacks: BitBoard) -> BitBoard {
        Piece::MOVE_FN[self as usize](loc, whites, blacks)
    }

    const CONTROL_FN: [fn(Square, BitBoard, BitBoard) -> BitBoard; 12] = [
        pawns::white_control,
        knights::control,
        sliding::bishops::control,
        sliding::rooks::control,
        sliding::queens::control,
        kings::control,
        pawns::black_control,
        knights::control,
        sliding::bishops::control,
        sliding::rooks::control,
        sliding::queens::control,
        kings::control,
    ];

    const MOVE_FN: [fn(Square, BitBoard, BitBoard) -> BitBoard; 12] = [
        pawns::white_moves,
        knights::white_moves,
        sliding::bishops::white_moves,
        sliding::rooks::white_moves,
        sliding::queens::white_moves,
        kings::white_moves,
        pawns::black_moves,
        knights::black_moves,
        sliding::bishops::black_moves,
        sliding::rooks::black_moves,
        sliding::queens::black_moves,
        kings::black_moves,
    ];
}

/// Constant piece groupings.
const ALL: [Piece; 12] = [
    Piece::WP,
    Piece::WN,
    Piece::WB,
    Piece::WR,
    Piece::WQ,
    Piece::WK,
    Piece::BP,
    Piece::BN,
    Piece::BB,
    Piece::BR,
    Piece::BQ,
    Piece::BK,
];

const WHITE: [Piece; 6] = [
    Piece::WP,
    Piece::WN,
    Piece::WB,
    Piece::WR,
    Piece::WQ,
    Piece::WK,
];

const BLACK: [Piece; 6] = [
    Piece::BP,
    Piece::BN,
    Piece::BB,
    Piece::BR,
    Piece::BQ,
    Piece::BK,
];

#[cfg(test)]
mod test {
    use crate::Piece;

    #[test]
    fn display() {
        assert_eq!("wp", Piece::WP.to_string().as_str());
        assert_eq!("br", Piece::BR.to_string().as_str());
    }

    #[test]
    fn from_str() {
        assert_eq!(Piece::WP, "wp".parse::<Piece>().unwrap());
        assert_eq!(Piece::WP, "WP".parse::<Piece>().unwrap());
        assert_eq!(Piece::BQ, "bq".parse::<Piece>().unwrap());
        assert!("ba".parse::<Piece>().is_err());
        assert!("bqs".parse::<Piece>().is_err());
        assert!("wxk".parse::<Piece>().is_err());
    }
}