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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use std::fmt;
use side::Side;

pub type Internal = usize;

/// Represents a piece for a particular side (eg black knight)
#[derive(PartialEq, PartialOrd, Copy, Clone)]
pub struct Piece(Internal);

const CHARS: [char; 12] = ['B', 'b', 'Q', 'q', 'R', 'r', 'N', 'n', 'P', 'p', 'K', 'k'];
// const SYMBOLS: [char; 14] = ['♙', '♟', '♘', '♞', '♗', '♝', '♖', '♜',
//                             '♕', '♛', '♔', '♚', '.'];
const NAMES: [&'static str; 12] = ["white bishop",
                                   "black bishop",
                                   "white queen",
                                   "black queen",
                                   "white rook",
                                   "black rook",
                                   "white knight",
                                   "black knight",
                                   "white pawn",
                                   "black pawn",
                                   "white king",
                                   "black king"];

/// Represents a kind of piece (eg knight)
#[derive(PartialEq, PartialOrd, Copy, Clone)]
pub struct Kind(pub Internal);

impl Kind {
    #[inline]
    pub fn pc(&self, side: Side) -> Piece {
        Piece((self.0 << 1) | side.raw() as Internal)
    }

    #[inline]
    pub fn to_usize(&self) -> usize {
        self.0 as usize
    }

    #[inline]
    pub const fn to_u8(&self) -> u8 {
        self.0 as u8
    }

    pub fn to_char(&self) -> char {
        CHARS[self.to_usize() << 1]
    }
}

impl fmt::Debug for Kind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_char())
    }
}

impl fmt::Display for Kind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_char())
    }
}


#[allow(dead_code)]
pub const BISHOP: Kind = Kind(0);
#[allow(dead_code)]
pub const QUEEN: Kind = Kind(1);
#[allow(dead_code)]
pub const ROOK: Kind = Kind(2);
#[allow(dead_code)]
pub const KNIGHT: Kind = Kind(3);
#[allow(dead_code)]
pub const PAWN: Kind = Kind(4);
#[allow(dead_code)]
pub const KING: Kind = Kind(5);
#[allow(dead_code)]
pub const NULL_KIND: Kind = Kind(6);

#[allow(dead_code)]
pub const WHITE_BISHOP: Piece = Piece(0);
#[allow(dead_code)]
pub const BLACK_BISHOP: Piece = Piece(1);

#[allow(dead_code)]
pub const WHITE_QUEEN: Piece = Piece(2);
#[allow(dead_code)]
pub const BLACK_QUEEN: Piece = Piece(3);

#[allow(dead_code)]
pub const WHITE_ROOK: Piece = Piece(4);
#[allow(dead_code)]
pub const BLACK_ROOK: Piece = Piece(5);


#[allow(dead_code)]
pub const WHITE_KNIGHT: Piece = Piece(6);
#[allow(dead_code)]
pub const BLACK_KNIGHT: Piece = Piece(7);

#[allow(dead_code)]
pub const WHITE_PAWN: Piece = Piece(8);
#[allow(dead_code)]
pub const BLACK_PAWN: Piece = Piece(9);

#[allow(dead_code)]
pub const WHITE_KING: Piece = Piece(10);
#[allow(dead_code)]
pub const BLACK_KING: Piece = Piece(11);

#[allow(dead_code)]
pub const NULL_PIECE: Piece = Piece(12);

impl Piece {
    #[inline]
    pub fn to_usize(&self) -> usize {
        self.0 as usize
    }

    #[inline]
    pub fn to_char(&self) -> char {
        CHARS[self.to_usize()]
    }

    #[inline]
    pub fn kind(&self) -> Kind {
        Kind(self.0 >> 1)
    }

    #[inline]
    pub fn is_none(&self) -> bool {
        *self == NULL_PIECE
    }

    #[inline]
    pub fn is_some(&self) -> bool {
        *self != NULL_PIECE
    }

    // assumes piece present
    #[inline]
    pub fn is_slider(&self) -> bool {
        self.0 <= BLACK_ROOK.0
    }

    pub fn to_string(&self) -> &'static str {
        NAMES[self.to_usize()]
    }

    pub fn string_plural(&self) -> String {
        NAMES[self.to_usize()].to_string() + &"s"
    }

    #[inline]
    pub fn side(&self) -> Side {
        Side(self.to_usize() & 1)
    }

    pub fn iter() -> PiecesIter {
        PiecesIter(Piece(0))
    }

    pub fn parse(chr: char) -> Result<Piece, String> {
        for pc in Piece::iter() {
            if pc.to_char() == chr {
                return Ok(pc);
            }
        }
        Err(format!("Invalid piece: {}", chr))
    }
}

#[derive(Debug)]
pub struct PiecesIter(Piece);

impl Iterator for PiecesIter {
    type Item = Piece;

    fn next(&mut self) -> Option<Piece> {
        let pc = self.0;

        if pc >= Piece(12) {
            return None;
        }

        (self.0).0 += 1;

        Some(pc)
    }
}

impl fmt::Display for Piece {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_char())
    }
}

impl fmt::Debug for Piece {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_char())
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use side::{BLACK, WHITE};

    #[test]
    fn to_char() {
        assert_eq!(BLACK_ROOK.to_char(), 'r');
        assert_eq!(WHITE_KING.to_char(), 'K');
    }

    #[test]
    fn side() {
        assert_eq!(WHITE_PAWN.side(), WHITE);
        assert_eq!(BLACK_KNIGHT.side(), BLACK);
    }

    #[test]
    fn to_usize() {
        assert_eq!(BLACK_ROOK.to_usize(), 5);
        assert_eq!(WHITE_KING.to_usize(), 10);
    }

    #[test]
    fn kind() {
        assert_eq!(WHITE_PAWN.kind(), PAWN);
        assert_eq!(BLACK_KING.kind(), KING);
    }
}