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
use crate::{PieceType, PlayerColor};
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Piece {
piece_type: PieceType,
piece_color: PlayerColor,
}
impl Piece {
/// Creates a new `Piece`-struct.
#[must_use]
pub fn new(piece_type: PieceType, piece_color: PlayerColor) -> Self {
Piece {
piece_type,
piece_color,
}
}
// /// Returns the player color of the piece.
// /// - `returns` - The player color of the piece
#[must_use]
pub fn get_color(&self) -> PlayerColor {
self.piece_color
}
/// Returns the piece type of the piece.
/// - `returns` - The piece type of the piece
#[must_use]
pub fn get_type(&self) -> PieceType {
self.piece_type
}
/// Converts the `Piece` into its char representation.
/// Pieces are represented by a single letter. Uppercase letters are whites pieces, lowercase letters are blacks pieces.
/// - `returns` - A char representing the piece
#[must_use]
pub fn export_piece(&self) -> char {
let letter = self.piece_type.export_piecetype_lowercase();
let color = self.piece_color;
match color {
PlayerColor::Black => letter,
PlayerColor::White => letter.to_ascii_uppercase(),
}
}
/// Converts a char to its corresponding piece in the algebraic chess notation
/// - `letter` - The letter representing a piece
/// - `returns` - The `Piece` object represented by the given letter
#[must_use]
pub fn import_piece(letter: char) -> Option<Self> {
let piece_type = PieceType::import_piecetype(letter.to_ascii_lowercase())?;
let piece_color = if letter.is_ascii_lowercase() {
PlayerColor::Black
} else {
PlayerColor::White
};
Some(Piece {
piece_type,
piece_color,
})
}
}