75 lines
1.7 KiB
Rust
75 lines
1.7 KiB
Rust
#[derive(Debug, Copy, Clone, PartialEq)]
|
|
pub enum PieceColor {
|
|
Black,
|
|
White,
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
|
pub enum BishopType {
|
|
Dark,
|
|
Light,
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
|
pub enum PieceType {
|
|
Pawn { promoted: bool },
|
|
Rook,
|
|
Knight,
|
|
King,
|
|
Queen,
|
|
Bishop { kind: BishopType },
|
|
}
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
pub struct Piece {
|
|
kind: PieceType,
|
|
color: PieceColor,
|
|
symbol: char,
|
|
}
|
|
|
|
impl Piece {
|
|
pub fn new(color: PieceColor, kind: PieceType) -> Self {
|
|
Self {
|
|
kind,
|
|
color,
|
|
symbol: Self::get_symbol(color, kind),
|
|
}
|
|
}
|
|
|
|
fn get_symbol(color: PieceColor, kind: PieceType) -> char {
|
|
use PieceColor::*;
|
|
use PieceType::*;
|
|
|
|
match (color, kind) {
|
|
(White, Pawn { promoted: _ }) => '♙',
|
|
(White, Knight) => '♘',
|
|
(White, Bishop { kind: _ }) => '♗',
|
|
(White, Rook) => '♖',
|
|
(White, Queen) => '♕',
|
|
(White, King) => '♔',
|
|
(Black, Pawn { promoted: _ }) => '♟',
|
|
(Black, Knight) => '♞',
|
|
(Black, Bishop { kind: _ }) => '♝',
|
|
(Black, Rook) => '♜',
|
|
(Black, Queen) => '♛',
|
|
(Black, King) => '♚',
|
|
}
|
|
}
|
|
}
|
|
|
|
mod test {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_make_black_rook() {
|
|
let piece = Piece::new(PieceColor::Black, PieceType::Rook);
|
|
assert_eq!(piece.symbol, '♜');
|
|
assert_eq!(piece.kind, PieceType::Rook);
|
|
assert_eq!(piece.color, PieceColor::Black);
|
|
}
|
|
|
|
#[test]
|
|
fn test_get_white_knight() {
|
|
assert_eq!(Piece::get_symbol(PieceColor::White, PieceType::Knight), '♘');
|
|
}
|
|
} |