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
use std::{
    fmt::{Display, Formatter},
    ops::{AddAssign, Neg, SubAssign},
};

// use colored::Colorize;

use serde::{Deserialize, Serialize};

#[derive(Hash, Eq, PartialEq, Copy, Clone, Debug, Serialize, Deserialize)]
pub enum Bw {
    Black = 0,
    White = 1,
}

impl Bw {
    #[inline]
    pub fn as_u8(&self) -> u8 {
        *self as u8
    }

    #[inline]
    pub fn as_alnum(&self) -> char {
        match self {
            Bw::Black => 'B',
            Bw::White => 'W',
        }
    }
}

impl Display for Bw {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Bw::Black => write!(f, "Black"),
            Bw::White => write!(f, "White"),
        }
    }
}

impl Neg for Bw {
    type Output = Bw;

    #[inline]
    fn neg(self) -> Self::Output {
        match self {
            Bw::Black => Bw::White,
            Bw::White => Bw::Black,
        }
    }
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(transparent)]
pub struct BOrW(pub i8);

impl Display for BOrW {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self.to_bwn() {
            Some((bw, n)) => write!(f, "{}{}", bw.as_alnum(), n),
            None => write!(f, "__"),
        }
    }
}

impl std::fmt::Debug for BOrW {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self.to_bwn() {
            Some((bw, n)) => f.write_str(&format!("{}{}", bw.as_alnum(), n)),
            /*.color(match bw {
                Bw::Black => "red",
                Bw::White => "blue",
            })) */
            None => f.write_str(" 0"),
        }
    }
}

impl BOrW {
    #[inline]
    pub const fn empty() -> BOrW {
        BOrW(0)
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.0 == 0
    }

    #[inline]
    pub fn to_bwn(self) -> Option<(Bw, u8)> {
        if self.0 > 0 {
            Some((Bw::White, self.0 as u8))
        } else if self.0 < 0 {
            Some((Bw::Black, -self.0 as u8))
        } else {
            None
        }
    }

    #[inline]
    pub fn to_count(self) -> u8 {
        self.0.abs() as u8
    }

    #[inline]
    pub fn to_color(self) -> Option<Bw> {
        if self.0 > 0 {
            Some(Bw::White)
        } else if self.0 < 0 {
            Some(Bw::Black)
        } else {
            None
        }
    }

    #[inline]
    pub fn matches(&self, bw: Bw) -> bool {
        self.to_color() == Some(bw)
    }

    #[inline]
    pub fn white(n: u8) -> Self {
        Self(n as i8)
    }

    #[inline]
    pub fn black(n: u8) -> Self {
        Self(-(n as i8))
    }

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

    #[inline]
    pub fn as_alnum(self) -> char {
        match self.to_bwn() {
            Some((Bw::White, n)) => (n + b'a') as char,
            Some((Bw::Black, n)) => (n + b'A') as char,
            None => '0',
        }
    }
}

impl Default for BOrW {
    #[inline]
    fn default() -> Self {
        Self(0)
    }
}

impl From<(Bw, u8)> for BOrW {
    #[inline]
    fn from((bw, n): (Bw, u8)) -> Self {
        Self(match (n, bw) {
            (0, _) => 0,
            (_, Bw::Black) => -(n as i8),
            (_, Bw::White) => n as i8,
        })
    }
}

impl AddAssign<i8> for BOrW {
    #[inline]
    fn add_assign(&mut self, mut rhs: i8) {
        if self.0 < 0 {
            rhs = -rhs;
        }

        self.0 += rhs;
    }
}

impl SubAssign<i8> for BOrW {
    #[inline]
    fn sub_assign(&mut self, rhs: i8) {
        debug_assert!(self.0 != 0);

        *self += -rhs;
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(transparent)]
pub struct BAndW([u8; 2]);

impl BAndW {
    #[inline]
    pub fn new(black: u8, white: u8) -> Self {
        Self([black, white])
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.0[0] == 0 && self.0[1] == 0
    }

    #[inline]
    pub fn get(&self, bw: &Bw) -> u8 {
        self.0[*bw as usize]
    }

    #[inline]
    pub fn set(&mut self, bw: &Bw, n: u8) {
        self.0[*bw as usize] = n
    }

    #[inline]
    pub fn empty() -> Self {
        Self([0, 0])
    }

    #[inline]
    pub fn as_u32(&self) -> u32 {
        (self.0[0] as u32) << 8 | self.0[1] as u32
    }

    #[inline]
    pub fn from_u32(n: u32) -> Self {
        Self([(n >> 8) as u8, n as u8])
    }

    #[inline]
    pub fn as_alnum(&self) -> [char; 2] {
        [(self.0[0] + b'A') as char, (self.0[1] + b'a') as char]
    }
}