[board] Add an option to display a board with ASCII characters
This commit is contained in:
parent
ddd14e8999
commit
a92ec9aba3
2 changed files with 15 additions and 2 deletions
|
@ -5,9 +5,17 @@ use chessfriend_bitboard::BitBoard;
|
|||
use chessfriend_core::{File, Rank, Square};
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Default)]
|
||||
pub enum PieceDisplayStyle {
|
||||
#[default]
|
||||
Unicode,
|
||||
ASCII,
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub struct DiagramFormatter<'a> {
|
||||
board: &'a Board,
|
||||
piece_style: PieceDisplayStyle,
|
||||
marked_squares: BitBoard,
|
||||
highlighted_squares: BitBoard,
|
||||
}
|
||||
|
@ -16,6 +24,7 @@ impl<'a> DiagramFormatter<'a> {
|
|||
pub fn new(board: &'a Board) -> Self {
|
||||
Self {
|
||||
board,
|
||||
piece_style: PieceDisplayStyle::default(),
|
||||
marked_squares: BitBoard::default(),
|
||||
highlighted_squares: BitBoard::default(),
|
||||
}
|
||||
|
@ -48,7 +57,11 @@ impl fmt::Display for DiagramFormatter<'_> {
|
|||
}
|
||||
|
||||
if let Some(piece) = self.board.get_piece(square) {
|
||||
write!(f, "{piece}")?;
|
||||
let ch = match self.piece_style {
|
||||
PieceDisplayStyle::Unicode => piece.to_unicode(),
|
||||
PieceDisplayStyle::ASCII => piece.to_ascii(),
|
||||
};
|
||||
write!(f, "{ch}")?;
|
||||
} else {
|
||||
let ch = if self.marked_squares.contains(square) {
|
||||
'*'
|
||||
|
|
|
@ -63,7 +63,7 @@ impl Piece {
|
|||
}
|
||||
|
||||
#[must_use]
|
||||
fn to_unicode(self) -> char {
|
||||
pub fn to_unicode(self) -> char {
|
||||
match (self.color, self.shape) {
|
||||
(Color::Black, Shape::Pawn) => '♟',
|
||||
(Color::Black, Shape::Knight) => '♞',
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue