[board] Fix some bugs in the starting position

Turns out I was doing the starting position really wrong. In an upcoming commit,
I will implement FEN output for Positions. While doing that work, I found several
issues that were causing the output of the FEN formatter to return garbage.

Implement a handful of unit tests to track down the errors.

Rename Shape::_ascii_representation() → Shape::to_ascii.
Implement to_ascii() on Piece.
This commit is contained in:
Eryn Wells 2024-01-21 14:56:03 -08:00
parent 84c9c43a7d
commit 829d9af52c
5 changed files with 87 additions and 42 deletions

View file

@ -63,7 +63,7 @@ impl Shape {
PROMOTABLE_SHAPES.iter()
}
fn _ascii_representation(&self) -> char {
fn to_ascii(&self) -> char {
match self {
Shape::Pawn => 'P',
Shape::Knight => 'N',
@ -83,12 +83,12 @@ impl TryFrom<char> for Shape {
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
'p' => Ok(Shape::Pawn),
'N' => Ok(Shape::Knight),
'B' => Ok(Shape::Bishop),
'R' => Ok(Shape::Rook),
'Q' => Ok(Shape::Queen),
'K' => Ok(Shape::King),
'P' | 'p' => Ok(Shape::Pawn),
'N' | 'n' => Ok(Shape::Knight),
'B' | 'b' => Ok(Shape::Bishop),
'R' | 'r' => Ok(Shape::Rook),
'Q' | 'q' => Ok(Shape::Queen),
'K' | 'k' => Ok(Shape::King),
_ => Err(TryFromError),
}
}
@ -105,13 +105,13 @@ impl TryFrom<&str> for Shape {
impl Into<char> for &Shape {
fn into(self) -> char {
self._ascii_representation()
self.to_ascii()
}
}
impl Into<char> for Shape {
fn into(self) -> char {
self._ascii_representation()
self.to_ascii()
}
}
@ -173,6 +173,10 @@ impl Piece {
is_shape!(is_rook, Rook);
is_shape!(is_queen, Queen);
is_shape!(is_king, King);
pub fn to_ascii(&self) -> char {
self.shape.to_ascii()
}
}
impl fmt::Display for Piece {
@ -193,18 +197,18 @@ impl UnicodeDisplay for Piece {
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) => '♔',
(Color::Black, Shape::Pawn) => '♟',
(Color::Black, Shape::Knight) => '♞',
(Color::Black, Shape::Bishop) => '♝',
(Color::Black, Shape::Rook) => '♜',
(Color::Black, Shape::Queen) => '♛',
(Color::Black, Shape::King) => '♚',
(Color::White, Shape::Pawn) => '♙',
(Color::White, Shape::Knight) => '♘',
(Color::White, Shape::Bishop) => '♗',
(Color::White, Shape::Rook) => '♖',
(Color::White, Shape::Queen) => '♕',
(Color::White, Shape::King) => '♔',
}
)
}
@ -212,7 +216,7 @@ impl UnicodeDisplay for Piece {
impl FENDisplay for Piece {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let ascii = self.shape()._ascii_representation();
let ascii = self.shape().to_ascii();
write!(
f,
"{}",