[board] Declare three new Display-like traits
- ASCIIDisplay → format a type using ASCII only characters - UnicodeDisplay → format a type using any Unicode characters - FENDisplay → format a type for inclusion in a FEN string
This commit is contained in:
parent
64b47a8d70
commit
ddea2c2d63
3 changed files with 64 additions and 17 deletions
15
board/src/display.rs
Normal file
15
board/src/display.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
use std::fmt;
|
||||
|
||||
pub(crate) trait ASCIIDisplay {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result;
|
||||
}
|
||||
|
||||
pub(crate) trait UnicodeDisplay {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result;
|
||||
}
|
||||
|
||||
pub(crate) trait FENDisplay {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result;
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
// Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
mod bitboard;
|
||||
mod display;
|
||||
mod moves;
|
||||
#[macro_use]
|
||||
pub mod piece;
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
// Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
use crate::{bitboard::BitBoard, Square};
|
||||
use crate::{
|
||||
display::{ASCIIDisplay, FENDisplay, UnicodeDisplay},
|
||||
Square,
|
||||
};
|
||||
use std::fmt;
|
||||
use std::slice::Iter;
|
||||
|
||||
|
@ -56,7 +59,7 @@ impl Shape {
|
|||
|
||||
fn _ascii_representation(&self) -> char {
|
||||
match self {
|
||||
Shape::Pawn => 'p',
|
||||
Shape::Pawn => 'P',
|
||||
Shape::Knight => 'N',
|
||||
Shape::Bishop => 'B',
|
||||
Shape::Rook => 'R',
|
||||
|
@ -168,22 +171,50 @@ impl Piece {
|
|||
|
||||
impl fmt::Display for Piece {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let char = match (self.color, self.shape) {
|
||||
(Color::White, Shape::Pawn) => '♟',
|
||||
(Color::White, Shape::Knight) => '♞',
|
||||
(Color::White, Shape::Bishop) => '♝',
|
||||
(Color::White, Shape::Rook) => '♜',
|
||||
(Color::White, Shape::Queen) => '♛',
|
||||
(Color::White, Shape::King) => '♚',
|
||||
(Color::Black, Shape::Pawn) => '♙',
|
||||
(Color::Black, Shape::Knight) => '♘',
|
||||
(Color::Black, Shape::Bishop) => '♗',
|
||||
(Color::Black, Shape::Rook) => '♖',
|
||||
(Color::Black, Shape::Queen) => '♕',
|
||||
(Color::Black, Shape::King) => '♔',
|
||||
};
|
||||
UnicodeDisplay::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
write!(f, "{}", char)
|
||||
impl ASCIIDisplay for Piece {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", Into::<char>::into(self.shape()))
|
||||
}
|
||||
}
|
||||
|
||||
impl UnicodeDisplay for Piece {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
match (self.color, self.shape) {
|
||||
(Color::White, Shape::Pawn) => '♟',
|
||||
(Color::White, Shape::Knight) => '♞',
|
||||
(Color::White, Shape::Bishop) => '♝',
|
||||
(Color::White, Shape::Rook) => '♜',
|
||||
(Color::White, Shape::Queen) => '♛',
|
||||
(Color::White, Shape::King) => '♚',
|
||||
(Color::Black, Shape::Pawn) => '♙',
|
||||
(Color::Black, Shape::Knight) => '♘',
|
||||
(Color::Black, Shape::Bishop) => '♗',
|
||||
(Color::Black, Shape::Rook) => '♖',
|
||||
(Color::Black, Shape::Queen) => '♕',
|
||||
(Color::Black, Shape::King) => '♔',
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl FENDisplay for Piece {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let ascii = self.shape()._ascii_representation();
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
match self.color {
|
||||
Color::White => ascii.to_ascii_uppercase(),
|
||||
Color::Black => ascii.to_ascii_lowercase(),
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue