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
use bitboard::{BitBoard, EMPTY};
use color::Color;
use square::Square;

/// What castle rights does a particular player have?
#[derive(Copy, Clone, PartialEq, PartialOrd)]
pub enum CastleRights {
    NoRights,
    KingSide,
    QueenSide,
    Both,
}

impl CastleRights {
    /// Can I castle kingside?
    pub fn has_kingside(&self) -> bool {
        self.to_index() & 1 == 1
    }

    /// Can I castle queenside?
    pub fn has_queenside(&self) -> bool {
        self.to_index() & 2 == 2
    }

    /// What squares need to be empty to castle kingside?
    pub fn kingside_squares(&self, color: Color) -> BitBoard {
        BitBoard::set(color.to_my_backrank(), 6) ^
        BitBoard::set(color.to_my_backrank(), 5)
    }

    /// What squares need to be empty to castle queenside?
    pub fn queenside_squares(&self, color: Color) -> BitBoard {
        BitBoard::set(color.to_my_backrank(), 1) ^
        BitBoard::set(color.to_my_backrank(), 2) ^
        BitBoard::set(color.to_my_backrank(), 3)
    }

    /// Remove castle rights, and return a new `CastleRights`.
    pub fn remove(&self, remove: CastleRights) -> CastleRights {
        CastleRights::from_index(self.to_index() & !remove.to_index())
    }

    /// Convert `CastleRights` to `usize` for table lookups
    pub fn to_index(&self) -> usize {
        *self as usize
    }

    /// Convert `usize` to `CastleRights`.  Panic if invalid number.
    pub fn from_index(i: usize) -> CastleRights {
        match i {
            0 => CastleRights::NoRights,
            1 => CastleRights::KingSide,
            2 => CastleRights::QueenSide,
            3 => CastleRights::Both,
            _ => unreachable!()
        }
    }

    /// Which rooks can we "guarantee" we haven't moved yet?
    pub fn unmoved_rooks(&self, color: Color) -> BitBoard {
        match *self {
            CastleRights::NoRights => EMPTY,
            CastleRights::KingSide => BitBoard::set(color.to_my_backrank(), 7),
            CastleRights::QueenSide => BitBoard::set(color.to_my_backrank(), 0),
            CastleRights::Both => BitBoard::set(color.to_my_backrank(), 0) ^
                                  BitBoard::set(color.to_my_backrank(), 7)
        }
    }

    /// Given a square of a rook, which side is it on?
    /// Note: It is invalid to pass in a non-rook square.  The code may panic.
    pub fn rook_square_to_castle_rights(square: Square) -> CastleRights {
        match square.file() {
            0 => CastleRights::QueenSide,
            7 => CastleRights::KingSide,
            _ => unreachable!()
        }
    }
}