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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
use side::{Side, BLACK};
use std::fmt;

#[cfg(test)]
use rand;

pub type Internal = usize;

/// Represents a square on the chessboard
#[derive(PartialEq, PartialOrd, Copy, Clone)]
pub struct Square(pub Internal);

const NAMES: [&'static str; 64] = [
    "a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1", "a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2",
    "a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3", "a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4",
    "a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5", "a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6",
    "a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7", "a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8",
];

#[allow(dead_code)]
pub const A1: Square = Square(0);
#[allow(dead_code)]
pub const B1: Square = Square(1);
#[allow(dead_code)]
pub const C1: Square = Square(2);
#[allow(dead_code)]
pub const D1: Square = Square(3);
#[allow(dead_code)]
pub const E1: Square = Square(4);
#[allow(dead_code)]
pub const F1: Square = Square(5);
#[allow(dead_code)]
pub const G1: Square = Square(6);
#[allow(dead_code)]
pub const H1: Square = Square(7);
#[allow(dead_code)]
pub const A2: Square = Square(8);
#[allow(dead_code)]
pub const B2: Square = Square(9);
#[allow(dead_code)]
pub const C2: Square = Square(10);
#[allow(dead_code)]
pub const D2: Square = Square(11);
#[allow(dead_code)]
pub const E2: Square = Square(12);
#[allow(dead_code)]
pub const F2: Square = Square(13);
#[allow(dead_code)]
pub const G2: Square = Square(14);
#[allow(dead_code)]
pub const H2: Square = Square(15);
#[allow(dead_code)]
pub const A3: Square = Square(16);
#[allow(dead_code)]
pub const B3: Square = Square(17);
#[allow(dead_code)]
pub const C3: Square = Square(18);
#[allow(dead_code)]
pub const D3: Square = Square(19);
#[allow(dead_code)]
pub const E3: Square = Square(20);
#[allow(dead_code)]
pub const F3: Square = Square(21);
#[allow(dead_code)]
pub const G3: Square = Square(22);
#[allow(dead_code)]
pub const H3: Square = Square(23);
#[allow(dead_code)]
pub const A4: Square = Square(24);
#[allow(dead_code)]
pub const B4: Square = Square(25);
#[allow(dead_code)]
pub const C4: Square = Square(26);
#[allow(dead_code)]
pub const D4: Square = Square(27);
#[allow(dead_code)]
pub const E4: Square = Square(28);
#[allow(dead_code)]
pub const F4: Square = Square(29);
#[allow(dead_code)]
pub const G4: Square = Square(30);
#[allow(dead_code)]
pub const H4: Square = Square(31);
#[allow(dead_code)]
pub const A5: Square = Square(32);
#[allow(dead_code)]
pub const B5: Square = Square(33);
#[allow(dead_code)]
pub const C5: Square = Square(34);
#[allow(dead_code)]
pub const D5: Square = Square(35);
#[allow(dead_code)]
pub const E5: Square = Square(36);
#[allow(dead_code)]
pub const F5: Square = Square(37);
#[allow(dead_code)]
pub const G5: Square = Square(38);
#[allow(dead_code)]
pub const H5: Square = Square(39);
#[allow(dead_code)]
pub const A6: Square = Square(40);
#[allow(dead_code)]
pub const B6: Square = Square(41);
#[allow(dead_code)]
pub const C6: Square = Square(42);
#[allow(dead_code)]
pub const D6: Square = Square(43);
#[allow(dead_code)]
pub const E6: Square = Square(44);
#[allow(dead_code)]
pub const F6: Square = Square(45);
#[allow(dead_code)]
pub const G6: Square = Square(46);
#[allow(dead_code)]
pub const H6: Square = Square(47);
#[allow(dead_code)]
pub const A7: Square = Square(48);
#[allow(dead_code)]
pub const B7: Square = Square(49);
#[allow(dead_code)]
pub const C7: Square = Square(50);
#[allow(dead_code)]
pub const D7: Square = Square(51);
#[allow(dead_code)]
pub const E7: Square = Square(52);
#[allow(dead_code)]
pub const F7: Square = Square(53);
#[allow(dead_code)]
pub const G7: Square = Square(54);
#[allow(dead_code)]
pub const H7: Square = Square(55);
#[allow(dead_code)]
pub const A8: Square = Square(56);
#[allow(dead_code)]
pub const B8: Square = Square(57);
#[allow(dead_code)]
pub const C8: Square = Square(58);
#[allow(dead_code)]
pub const D8: Square = Square(59);
#[allow(dead_code)]
pub const E8: Square = Square(60);
#[allow(dead_code)]
pub const F8: Square = Square(61);
#[allow(dead_code)]
pub const G8: Square = Square(62);
#[allow(dead_code)]
pub const H8: Square = Square(63);

impl Square {
    #[inline]
    pub fn new(s: Internal) -> Square {
        Square(s)
    }

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

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

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

    #[inline]
    pub fn raw(&self) -> Internal {
        self.0
    }

    #[inline]
    #[allow(dead_code)]
    pub fn diagonal(&self) -> usize {
        ((self.row() - self.col()) & 15) as usize
    }

    #[inline]
    #[allow(dead_code)]
    pub fn anti_diagonal(&self) -> usize {
        ((self.row() + self.col()) & 7) as usize
    }

    #[inline]
    pub fn inc(&mut self) {
        self.0 += 1
    }

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

    // Gives square from perspective of side
    // ie, flips if black
    #[inline]
    pub fn from_side(&self, side: Side) -> Square {
        Square(self.0 ^ if side == BLACK { 56 } else { 0 })
    }

    #[inline]
    pub fn flip(&self) -> Square {
        Square(self.0 ^ 56)
    }

    #[inline]
    pub fn rotate_right(&self, amount: Internal) -> Square {
        Square((self.0 + (64 - amount)) & 63)
    }

    #[inline]
    pub fn rotate_left(&self, amount: Internal) -> Square {
        Square((self.0 + amount) & 63)
    }

    pub fn to_str(&self) -> &'static str {
        NAMES[self.0 as usize]
    }

    pub fn to_string(&self) -> String {
        self.to_str().to_string()
    }

    #[inline]
    #[cfg(test)]
    pub fn random() -> Square {
        Square(rand::random::<Internal>() % 64)
    }

    // returns a square at the same row as self, and the same col as another square
    #[inline]
    pub fn along_row_with_col(&self, other: Square) -> Square {
        Square((self.0 & 56) | (other.0 & 7))
    }

    #[inline]
    pub fn change_row(&self, row: Internal) -> Square {
        Square((self.0 & 7) | (row * 8))
    }

    #[inline]
    pub fn row(&self) -> Internal {
        self.0 >> 3
    }

    #[inline]
    pub fn rowx8(&self) -> Internal {
        self.0 & 56
    }

    #[inline]
    pub fn col(&self) -> Internal {
        self.0 & 7
    }

    #[inline]
    pub fn from(row: Internal, col: Internal) -> Square {
        Square(row * 8 + col)
    }

    pub fn parse(s: &str) -> Result<Option<Square>, String> {
        if s == "-" {
            return Ok(None);
        }

        if s.len() < 2 {
            return Err("String too short".to_string());
        }

        let col_char = s.chars().nth(0).unwrap() as Internal;
        let row_char = s.chars().nth(1).unwrap() as Internal;

        let col = col_char - 'a' as Internal;
        let row = row_char - '1' as Internal;

        if col > 7 {
            return Err(format!("Bad column identifier: {}", col_char));
        }

        if row > 7 {
            return Err(format!("Bad row identifier: {}", row_char));
        }

        Ok(Some(Square::from(row, col)))
    }
}

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

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

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn along_row_with_col() {
        assert_eq!(A5.along_row_with_col(B6), B5);
    }

    #[test]
    fn flip() {
        assert_eq!(H8.flip(), H1);
        assert_eq!(C3.flip(), C6);
        assert_eq!(B2.flip(), B7);
    }

    #[test]
    fn rotate_left() {
        assert_eq!(A1.rotate_left(1), B1);
        assert_eq!(H8.rotate_left(1), A1);
        assert_eq!(C3.rotate_left(16), C5);
    }

    #[test]
    fn raw() {
        assert_eq!(H8.raw(), 63);
        assert_eq!(B3.raw(), 17);
    }

    #[test]
    fn to_string() {
        assert_eq!(A1.to_string(), "a1");
        assert_eq!(F8.to_string(), "f8");
    }

    #[test]
    fn col() {
        assert_eq!(A2.col(), 0);
        assert_eq!(C6.col(), 2);
    }

    #[test]
    fn row() {
        assert_eq!(A2.row(), 1);
        assert_eq!(C6.row(), 5);
    }
}