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
//! [`Piece`] representation
use crate::*;
#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(feature = "std")]
use std::string::String;
use core::fmt::*;
crate::helpers::simple_enum! {
/// Shogi piece types.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Piece {
/// A pawn.
Pawn,
/// A lance.
Lance,
/// A knight.
Knight,
/// A silver general.
Silver,
/// A bishop.
Bishop,
/// A rook.
Rook,
/// A gold general.
Gold,
/// A king.
King,
/// Tokin (promoted pawn)
Tokin,
/// Promoted lance.
PLance,
/// Promoted knight.
PKnight,
/// Promoted silver.
PSilver,
/// Promoted bishop.
PBishop,
/// Promoted rook.
PRook
}
}
// The piece representation in SFEN strings requires either one or
// two chars. Black/Black pieces are indicated by uppercase letters;
// White/White pieces by lowerase. Promoted pieces are indicated by
// a '+' prefix.
crate::helpers::simple_error! {
/// The value was not a valid [`Piece`].
pub struct PieceParseError = "The value is not a valid Piece.";
}
impl Piece {
/// Number of simple, unpromoted piece types other than King.
pub const HAND_NUM: usize = 7;
/// Max number of pieces for a piece type to have in hand
pub const MAX_HAND: [u8; Self::NUM] = [
18, // Pawn
4, // Lance
4, // Knight
4, // Silver
2, // Bishop
2, // Rook
4, // Gold
0, 0, 0, 0, 0, 0, 0,
];
// piece -> promoted piece (promoted pieces map to themselves)
const PROMOTED: [Self; Self::NUM] = [
Piece::Tokin,
Piece::PLance,
Piece::PKnight,
Piece::PSilver,
Piece::PBishop,
Piece::PRook,
Piece::Gold,
Piece::King,
Piece::Tokin,
Piece::PLance,
Piece::PKnight,
Piece::PSilver,
Piece::PBishop,
Piece::PRook,
];
// promoted piece -> piece (unpromoted pieces map to themselves)
const UNPROMOTED: [Self; Self::NUM] = [
Piece::Pawn,
Piece::Lance,
Piece::Knight,
Piece::Silver,
Piece::Bishop,
Piece::Rook,
Piece::Gold,
Piece::King,
Piece::Pawn,
Piece::Lance,
Piece::Knight,
Piece::Silver,
Piece::Bishop,
Piece::Rook,
];
/// Is this piece a promoted piece?
#[inline(always)]
pub const fn is_promoted(self) -> bool {
(self as usize) >= Self::Tokin as usize
}
/// Is this piece a non-promoted piece?
#[inline(always)]
pub const fn is_unpromoted(self) -> bool {
(self as usize) < Self::Tokin as usize
}
/// Can this piece ever promote?
#[inline(always)]
pub const fn is_promotable(self) -> bool {
(self as usize) < Self::Gold as usize
}
/// Can this piece with given color promote on the given square?
///
/// # Examples
///
/// ```
/// use haitaka_types::*;
/// assert!(Piece::Pawn.can_promote(Color::Black, Square::C1));
/// assert!(Piece::Pawn.can_promote(Color::White, Square::G1));
/// assert!(! Piece::Pawn.can_promote(Color::Black, Square::H7));
/// assert!(! Piece::Pawn.can_promote(Color::White, Square::C3));
/// ```
#[inline(always)]
pub const fn can_promote(self, color: Color, square: Square) -> bool {
if !self.is_promotable() {
false
} else {
let rank = square.rank() as usize;
match color {
Color::White => rank > 5,
Color::Black => rank < 3,
}
}
}
/// Must this piece with given color promote on the given square?
///
/// # Examples
///
/// ```
/// use haitaka_types::*;
/// assert!(Piece::Pawn.must_promote(Color::Black, Square::A3));
/// assert!(Piece::Lance.must_promote(Color::Black, Square::A3));
/// assert!(Piece::Knight.must_promote(Color::Black, Square::A3));
///
/// assert!(Piece::Pawn.must_promote(Color::White, Square::I3));
/// assert!(Piece::Lance.must_promote(Color::White, Square::I3));
/// assert!(Piece::Knight.must_promote(Color::White, Square::I3));
///
/// assert!(!Piece::Pawn.must_promote(Color::Black, Square::B3));
/// assert!(!Piece::Lance.must_promote(Color::Black, Square::B3));
/// assert!(Piece::Knight.must_promote(Color::Black, Square::B3));
///
/// assert!(!Piece::Pawn.must_promote(Color::White, Square::H3));
/// assert!(!Piece::Lance.must_promote(Color::White, Square::H3));
/// assert!(Piece::Knight.must_promote(Color::White, Square::H3));
/// ```
#[inline(always)]
pub const fn must_promote(self, color: Color, square: Square) -> bool {
let rank = square.rank() as usize;
if 1 < rank && rank < 7 {
return false;
}
match color {
Color::White => match self {
Self::Pawn | Self::Lance => rank == 8,
Self::Knight => rank >= 7,
_ => false,
},
Color::Black => match self {
Self::Pawn | Self::Lance => rank == 0,
Self::Knight => rank <= 1,
_ => false,
},
}
}
/// Can this piece with given color be dropped on the given square?
/// If the piece must promote on the square, it can not be dropped there.
///
/// This function does not perform any occupancy check.
/// It assumes the given square is empty.
///
#[inline(always)]
pub const fn can_drop(self, color: Color, square: Square) -> bool {
!self.must_promote(color, square)
}
/// Promote this piece.
///
/// Never panics. If the piece cannot be promoted, it the same piece is returned.
#[inline(always)]
pub const fn promote(self) -> Self {
Self::PROMOTED[self as usize]
}
/// Unpromote this piece.
///
/// Never panics. If the piece is an unpromoted piece, the same piece is returned.
#[inline(always)]
pub const fn unpromote(self) -> Self {
Self::UNPROMOTED[self as usize]
}
pub fn try_from_char(c: char) -> Option<(Self, Color)> {
match c {
'p' => Some((Self::Pawn, Color::White)),
'l' => Some((Self::Lance, Color::White)),
'n' => Some((Self::Knight, Color::White)),
's' => Some((Self::Silver, Color::White)),
'g' => Some((Self::Gold, Color::White)),
'r' => Some((Self::Rook, Color::White)),
'b' => Some((Self::Bishop, Color::White)),
'k' => Some((Self::King, Color::White)),
'P' => Some((Self::Pawn, Color::Black)),
'L' => Some((Self::Lance, Color::Black)),
'N' => Some((Self::Knight, Color::Black)),
'S' => Some((Self::Silver, Color::Black)),
'G' => Some((Self::Gold, Color::Black)),
'R' => Some((Self::Rook, Color::Black)),
'B' => Some((Self::Bishop, Color::Black)),
'K' => Some((Self::King, Color::Black)),
_ => None,
}
}
pub fn try_from_str(s: &str) -> Option<(Self, Color)> {
let mut chars = s.chars();
let first = chars.next()?; // Get the first character
let second = chars.next(); // Get the second character, if any
match (first, second) {
// Promoted pieces (e.g., "+p", "+l")
('+', Some(c)) => match c {
'p' => Some((Self::Tokin, Color::White)),
'l' => Some((Self::PLance, Color::White)),
'n' => Some((Self::PKnight, Color::White)),
's' => Some((Self::PSilver, Color::White)),
'b' => Some((Self::PBishop, Color::White)),
'r' => Some((Self::PRook, Color::White)),
'P' => Some((Self::Tokin, Color::Black)),
'L' => Some((Self::PLance, Color::Black)),
'N' => Some((Self::PKnight, Color::Black)),
'S' => Some((Self::PSilver, Color::Black)),
'B' => Some((Self::PBishop, Color::Black)),
'R' => Some((Self::PRook, Color::Black)),
_ => None,
},
// Unpromoted pieces (e.g., "p", "l")
(c, None) => match c {
'p' => Some((Self::Pawn, Color::White)),
'l' => Some((Self::Lance, Color::White)),
'n' => Some((Self::Knight, Color::White)),
's' => Some((Self::Silver, Color::White)),
'g' => Some((Self::Gold, Color::White)),
'r' => Some((Self::Rook, Color::White)),
'b' => Some((Self::Bishop, Color::White)),
'k' => Some((Self::King, Color::White)),
'P' => Some((Self::Pawn, Color::Black)),
'L' => Some((Self::Lance, Color::Black)),
'N' => Some((Self::Knight, Color::Black)),
'S' => Some((Self::Silver, Color::Black)),
'G' => Some((Self::Gold, Color::Black)),
'R' => Some((Self::Rook, Color::Black)),
'B' => Some((Self::Bishop, Color::Black)),
'K' => Some((Self::King, Color::Black)),
_ => None,
},
// Invalid input
_ => None,
}
}
pub fn to_str(self, color: Color) -> String {
let s: &str = match self {
Self::Pawn => "p",
Self::Lance => "l",
Self::Knight => "n",
Self::Silver => "s",
Self::Bishop => "b",
Self::Rook => "r",
Self::Gold => "g",
Self::King => "k",
Self::Tokin => "+p",
Self::PLance => "+l",
Self::PKnight => "+n",
Self::PSilver => "+s",
Self::PBishop => "+b",
Self::PRook => "+r",
};
if color == Color::Black {
s.to_uppercase()
} else {
String::from(s)
}
}
}
impl core::str::FromStr for Piece {
type Err = PieceParseError;
fn from_str(s: &str) -> core::result::Result<Self, Self::Err> {
Piece::try_from_str(s)
.map(|(piece, _color)| piece) // Extract the `Piece` from the tuple
.ok_or(PieceParseError) // Convert `Option` to `Result`
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ColoredPiece {
pub piece: Piece,
pub color: Color,
}
impl core::str::FromStr for ColoredPiece {
type Err = PieceParseError;
fn from_str(s: &str) -> core::result::Result<Self, Self::Err> {
Piece::try_from_str(s)
.map(|(piece, color)| ColoredPiece { piece, color })
.ok_or(PieceParseError)
}
}
impl core::fmt::Display for ColoredPiece {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.piece.to_str(self.color))
}
}