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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
//! A chess move.

use core::fmt;

use uncon::FromUnchecked;

use color::Color;
use castle::Right;
use piece;
use square::{File, Rank, Square};

#[cfg(test)]
mod tests;

#[cfg(all(test, nightly))]
mod benches;

mod vec;
pub use self::vec::*;

macro_rules! base {
    ($s1:expr, $s2:expr) => {
        (($s1 as u16) << SRC_SHIFT) | (($s2 as u16) << DST_SHIFT)
    }
}

macro_rules! kind {
    ($k:ident) => { (Kind::$k as u16) << KIND_SHIFT };
}

macro_rules! meta {
    ($m:expr) => { ($m as u16) << META_SHIFT };
}

const SRC_SHIFT:  usize =  0;
const DST_SHIFT:  usize =  6;
const RANK_SHIFT: usize =  3;
const META_SHIFT: usize = 12;
const KIND_SHIFT: usize = 14;

const SQ_MASK:   u16 = SRC_MASK | (DST_MASK << DST_SHIFT);
const SRC_MASK:  u16 = 0b111111;
const DST_MASK:  u16 = SRC_MASK;
const META_MASK: u16 = 0b11;
const KIND_MASK: u16 = META_MASK;

const FILE_MASK: u16 = 0b000111000111;
const RANK_MASK: u16 = FILE_MASK << RANK_SHIFT;

const LO_MASK: u16 = 0b111;
const FILE_LO: u16 = FILE_MASK / LO_MASK;

/// A chess piece move from one square to another.
///
/// Each instance has the following memory layout:
///
/// - Source **[6 bits]**
///
/// - Destination **[6 bits]**
///
/// - Meta **[2 bits]** (optional)
///
/// - Kind **[2 bits]** ([`Normal`], [`Promotion`], [`Castle`], [`EnPassant`])
///
/// [`Normal`]:    ./kind/struct.Normal.html
/// [`Promotion`]: ./kind/struct.Promotion.html
/// [`Castle`]:    ./kind/struct.Castle.html
/// [`EnPassant`]: ./kind/struct.EnPassant.html
#[derive(PartialEq, Eq, Clone, Copy, Hash, FromUnchecked)]
pub struct Move(pub(crate) u16);

impl From<Move> for u16 {
    #[inline]
    fn from(mv: Move) -> u16 { mv.0 }
}

impl fmt::Debug for Move {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.matches().fmt(f)
    }
}

impl Move {
    /// Creates a new `Move` from one square to another.
    #[inline]
    pub fn normal(src: Square, dst: Square) -> Move {
        kind::Normal::new(src, dst).into()
    }

    /// Creates a new promotion move for `color` at `file`.
    #[inline]
    pub fn promotion(file: File, color: Color, piece: piece::Promotion) -> Move {
        kind::Promotion::new(file, color, piece).into()
    }

    /// Creates a new castle move for `right`.
    #[inline]
    pub fn castle(right: Right) -> Move {
        kind::Castle::from(right).into()
    }

    /// Creates an en passant move from one square to another.
    #[inline]
    pub fn en_passant(src: Square, dst: Square) -> Option<Move> {
        kind::EnPassant::try_new(src, dst).map(Into::into)
    }

    /// Returns whether the squares of `self` equal the squares of `other`.
    #[inline]
    pub fn squares_eq<T: Into<Move>>(self, other: T) -> bool {
        u16::from(self) & SQ_MASK == u16::from(other.into()) & SQ_MASK
    }

    /// Returns the source square for `self`.
    #[inline]
    pub fn src(self) -> Square {
        ((self.0 >> SRC_SHIFT) & SRC_MASK).into()
    }

    /// Returns the destination square for `self`.
    #[inline]
    pub fn dst(self) -> Square {
        ((self.0 >> DST_SHIFT) & DST_MASK).into()
    }

    /// Returns the kind for `self`.
    #[inline]
    pub fn kind(self) -> Kind {
        ((self.0 >> KIND_SHIFT) & KIND_MASK).into()
    }

    #[inline]
    fn meta(self) -> u16 {
        (self.0 >> META_SHIFT) & META_MASK
    }

    /// Returns `self` a castle move if it can be converted into one.
    #[inline]
    pub fn to_castle(self) -> Option<kind::Castle> {
        match self.kind() {
            Kind::Castle => Some(kind::Castle(self)),
            _ => kind::Castle::try_new(self.src(), self.dst()),
        }
    }

    /// Returns `self` as an en passant move if it can be converted into one.
    #[inline]
    pub fn to_en_passant(self) -> Option<kind::EnPassant> {
        match self.kind() {
            Kind::EnPassant => Some(kind::EnPassant(self)),
            _ => kind::EnPassant::try_new(self.src(), self.dst()),
        }
    }

    /// Returns a `match`-able type that represents the inner variant of `self`.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use hexe_core::mv::{Move, Matches};
    /// use hexe_core::square::Square;
    ///
    /// let mv = Move::normal(Square::A1, Square::A7);
    ///
    /// match mv.matches() {
    ///     Matches::Normal(mv)    => println!("{:?}", mv.src()),
    ///     Matches::Castle(mv)    => println!("{:?}", mv.right()),
    ///     Matches::Promotion(mv) => println!("{:?}", mv.piece()),
    ///     Matches::EnPassant(mv) => println!("{:?}", mv.capture()),
    /// }
    /// ```
    #[inline]
    pub fn matches(self) -> Matches {
        match self.kind() {
            Kind::Normal    => kind::Normal(self).into(),
            Kind::Castle    => kind::Castle(self).into(),
            Kind::Promotion => kind::Promotion(self).into(),
            Kind::EnPassant => kind::EnPassant(self).into(),
        }
    }
}

/// A chess piece move kind.
#[derive(PartialEq, Eq, Clone, Copy, Hash, FromUnchecked)]
#[uncon(impl_from, other(u16, u32, u64, usize))]
#[repr(u8)]
pub enum Kind {
    /// Normal move.
    Normal,
    /// [Castling](https://en.wikipedia.org/wiki/Castling) move.
    Castle,
    /// [Promotion](https://en.wikipedia.org/wiki/Promotion_(chess)) move.
    Promotion,
    /// [En passant](https://en.wikipedia.org/wiki/En_passant) move.
    EnPassant,
}

/// A `match`-able inner representation `Move`.
#[derive(Copy, Clone)]
pub enum Matches {
    /// Normal move.
    Normal(kind::Normal),
    /// [Castling](https://en.wikipedia.org/wiki/Castling) move.
    Castle(kind::Castle),
    /// [Promotion](https://en.wikipedia.org/wiki/Promotion_(chess)) move.
    Promotion(kind::Promotion),
    /// [En passant](https://en.wikipedia.org/wiki/En_passant) move.
    EnPassant(kind::EnPassant),
}

impl From<Move> for Matches {
    #[inline]
    fn from(mv: Move) -> Matches { mv.matches() }
}

macro_rules! impl_matches {
    ($($k:ident, $m:ident, $d:expr;)+) => {
        impl Matches {
            $(
                #[doc = $d]
                pub fn $m(self) -> Option<kind::$k> {
                    if let Matches::$k(mv) = self { Some(mv) } else { None }
                }
            )+
        }
    };
    ($($k:ident, $m:ident;)+) => {
        $(
            impl From<kind::$k> for Matches {
                #[inline]
                fn from(mv: kind::$k) -> Matches { Matches::$k(mv) }
            }
        )+

        impl_matches! { $(
            $k, $m, concat!("Returns the inner `", stringify!($k), "` match.");
        )+ }
    };
}

impl_matches! {
    Normal,    normal;
    Castle,    castle;
    Promotion, promotion;
    EnPassant, en_passant;
}

impl fmt::Debug for Matches {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Matches::Normal(mv)    => mv.fmt(f),
            Matches::Castle(mv)    => mv.fmt(f),
            Matches::Promotion(mv) => mv.fmt(f),
            Matches::EnPassant(mv) => mv.fmt(f),
        }
    }
}

/// The different underlying kinds of moves.
pub mod kind {
    use super::*;
    use core::ops;

    const WK: u16 = base!(Square::E1, Square::G1);
    const WQ: u16 = base!(Square::E1, Square::C1);
    const BK: u16 = base!(Square::E8, Square::G8);
    const BQ: u16 = base!(Square::E8, Square::C8);

    macro_rules! impl_from_move {
        ($($t:ident),+) => { $(
            impl From<$t> for Move {
                #[inline]
                fn from(mv: $t) -> Move { mv.0 }
            }

            impl From<$t> for u16 {
                #[inline]
                fn from(mv: $t) -> u16 { (mv.0).0 }
            }

            impl FromUnchecked<Move> for $t {
                #[inline]
                unsafe fn from_unchecked(mv: Move) -> $t { $t(mv) }
            }

            impl FromUnchecked<u16> for $t {
                #[inline]
                unsafe fn from_unchecked(bits: u16) -> $t { $t(Move(bits)) }
            }

            impl ops::Deref for $t {
                type Target = Move;

                #[inline]
                fn deref(&self) -> &Move { &self.0 }
            }

            impl AsRef<Move> for $t {
                #[inline]
                fn as_ref(&self) -> &Move { self }
            }
        )+}
    }

    impl_from_move! { Normal, Castle, Promotion, EnPassant }

    /// A normal, non-special move.
    #[derive(PartialEq, Eq, Clone, Copy, Hash)]
    pub struct Normal(pub(crate) Move);

    impl fmt::Debug for Normal {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            f.debug_struct("Normal").field("src", &self.src())
                                    .field("dst", &self.dst())
                                    .finish()
        }
    }

    impl Normal {
        /// Creates a new normal move from `src` to `dst`.
        #[inline]
        pub fn new(src: Square, dst: Square) -> Normal {
            Normal(Move(base!(src, dst) | kind!(Normal)))
        }

        /// Returns the kind for `self`.
        #[inline]
        pub fn kind(self) -> Kind { Kind::Normal }
    }

    /// A castling move.
    #[derive(PartialEq, Eq, Clone, Copy, Hash)]
    pub struct Castle(pub(crate) Move);

    impl fmt::Debug for Castle {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            f.debug_struct("Castle").field("src", &self.src())
                                    .field("dst", &self.dst())
                                    .field("right", &self.right())
                                    .finish()
        }
    }

    impl From<Right> for Castle {
        #[inline]
        fn from(right: Right) -> Castle {
            static ALL: [u16; 4] = [
                WK | kind!(Castle) | meta!(Right::WhiteKing),
                WQ | kind!(Castle) | meta!(Right::WhiteQueen),
                BK | kind!(Castle) | meta!(Right::BlackKing),
                BQ | kind!(Castle) | meta!(Right::BlackQueen),
            ];
            Castle(Move(ALL[right as usize]))
        }
    }

    impl From<Right> for Move {
        #[inline]
        fn from(right: Right) -> Move { Castle::from(right).into() }
    }

    impl Castle {
        /// Creates a new instance for the castle right.
        #[inline]
        pub fn new(right: Right) -> Castle { right.into() }

        /// Attempts to create a new castle move for the given squares.
        #[inline]
        pub fn try_new(src: Square, dst: Square) -> Option<Castle> {
            match src {
                Square::E1 => match dst {
                    Square::G1 => Some(Right::WhiteKing.into()),
                    Square::C1 => Some(Right::WhiteQueen.into()),
                    _ => None,
                },
                Square::E8 => match dst {
                    Square::G8 => Some(Right::BlackKing.into()),
                    Square::C8 => Some(Right::BlackQueen.into()),
                    _ => None,
                },
                _ => None,
            }
        }

        /// Returns the kind for `self`.
        #[inline]
        pub fn kind(self) -> Kind { Kind::Castle }

        /// Returns the castle right for `self`.
        #[inline]
        pub fn right(self) -> Right { self.meta().into() }
    }

    /// A promotion move.
    #[derive(PartialEq, Eq, Clone, Copy, Hash)]
    pub struct Promotion(pub(crate) Move);

    impl fmt::Debug for Promotion {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            f.debug_struct("Promotion").field("src", &self.src())
                                       .field("dst", &self.dst())
                                       .field("piece", &self.piece())
                                       .finish()
        }
    }

    impl Promotion {
        /// Creates a new promotion move.
        #[inline]
        pub fn new(file: File, color: Color, piece: piece::Promotion) -> Promotion {
            const WHITE: u16 = base!(Rank::Seven, Rank::Eight) << RANK_SHIFT;
            const BLACK: u16 = base!(Rank::Two,   Rank::One)   << RANK_SHIFT;

            let file = FILE_LO * file as u16;
            let rank = match color {
                Color::White => WHITE,
                Color::Black => BLACK,
            };

            Promotion(Move(file | rank | kind!(Promotion) | meta!(piece)))
        }

        /// Returns the kind for `self`.
        #[inline]
        pub fn kind(self) -> Kind { Kind::Promotion }

        /// Creates a promotion move using `Queen` as its piece.
        #[inline]
        pub fn queen(file: File, color: Color) -> Promotion {
            Promotion::new(file, color, piece::Promotion::Queen)
        }

        /// Returns the color of the moving piece.
        #[inline]
        pub fn color(self) -> Color {
            // src rank is even for white and odd for black
            let inner = u16::from(self);
            ((inner >> RANK_SHIFT) & 1).into()
        }

        /// Returns the promotion piece.
        #[inline]
        pub fn piece(self) -> piece::Promotion { self.meta().into() }
    }

    /// An en passant move.
    #[derive(PartialEq, Eq, Clone, Copy, Hash)]
    pub struct EnPassant(pub(crate) Move);

    impl fmt::Debug for EnPassant {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            f.debug_struct("EnPassant").field("src", &self.src())
                                       .field("dst", &self.dst())
                                       .finish()
        }
    }

    impl EnPassant {
        /// Attempts to create a new en passant move.
        #[inline]
        pub fn try_new(src: Square, dst: Square) -> Option<EnPassant> {
            let val = unsafe { EnPassant::new_unchecked(src, dst) };
            if val.is_legal() { Some(val) } else { None }
        }

        /// Creates a new en passant move without checking whether it is legal.
        #[inline]
        pub unsafe fn new_unchecked(src: Square, dst: Square) -> EnPassant {
            let kind = (Kind::EnPassant as u16) << KIND_SHIFT;
            EnPassant(Move(base!(src, dst) | kind))
        }

        /// Returns the kind for `self`.
        #[inline]
        pub fn kind(self) -> Kind { Kind::EnPassant }

        /// Returns the square of the captured piece.
        #[inline]
        pub fn capture(self) -> Square {
            Square::new(self.dst().file(),
                        self.src().rank())
        }

        /// Returns the color of the moving piece.
        #[inline]
        pub fn color(self) -> Color {
            // src rank is even for white and odd for black
            let inner = u16::from(self);
            ((inner >> RANK_SHIFT) & 1).into()
        }

        /// Returns whether the en passant is legal and is acting on the correct
        /// squares.
        #[inline]
        fn is_legal(self) -> bool {
            pub const W_EP: u16 = base!(Rank::Five, Rank::Six)   << RANK_SHIFT;
            pub const B_EP: u16 = base!(Rank::Four, Rank::Three) << RANK_SHIFT;

            let ranks = u16::from(self) & RANK_MASK;
            let color = match ranks {
                W_EP => Color::White,
                B_EP => Color::Black,
                _ => return false,
            };

            self.src().pawn_attacks(color).contains(self.dst())
        }
    }
}