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
/// Different types of pieces in chess.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum PieceType {
Pawn,
Rook,
Bishop,
Knight,
Queen,
King,
}
impl PieceType {
/// Converts the `PieceType` into its char representation.
/// Pieces are represented by a single letter. All letters are lowercase.
/// - `returns` - A char representing the piece
#[must_use]
pub fn export_piecetype_lowercase(self) -> char {
match self {
PieceType::Pawn => 'p',
PieceType::Rook => 'r',
PieceType::Bishop => 'b',
PieceType::Knight => 'n',
PieceType::Queen => 'q',
PieceType::King => 'k',
}
}
/// Converts the `PieceType` into its char representation.
/// Pieces are represented by a single letter. All letters are uppercase.
/// - `returns` - A char representing the piece
#[must_use]
pub fn export_piecetype_uppercase(self) -> char {
match self {
PieceType::Pawn => 'P',
PieceType::Rook => 'R',
PieceType::Bishop => 'B',
PieceType::Knight => 'N',
PieceType::Queen => 'Q',
PieceType::King => 'K',
}
}
/// Creates a `PieceType` out of a letter
/// - `piece_identifier` - The char representing the piece (lowercase only)
/// - `returns` - A `PieceType` object
#[must_use]
pub fn import_piecetype(piece_identifier: char) -> Option<Self> {
match piece_identifier {
'b' | 'B' => Some(PieceType::Bishop),
'q' | 'Q' => Some(PieceType::Queen),
'n' | 'N' => Some(PieceType::Knight),
'r' | 'R' => Some(PieceType::Rook),
'k' | 'K' => Some(PieceType::King),
'p' | 'P' => Some(PieceType::Pawn),
_ => None,
}
}
}