[board] Clean up Display and Debug implementations on BitBoard, Flags, Position, and PlacedPiece

This commit is contained in:
Eryn Wells 2024-01-19 17:59:34 -08:00
parent 2269df2ed9
commit c413db0bf1
4 changed files with 16 additions and 29 deletions

View file

@ -145,9 +145,7 @@ impl fmt::Display for BitBoard {
impl fmt::Debug for BitBoard { impl fmt::Debug for BitBoard {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.debug_tuple("BitBoard") write!(f, "BitBoard({:064b})", self.0)
.field(&format_args!("{:064b}", self.0))
.finish()
} }
} }

View file

@ -240,6 +240,12 @@ impl PlacedPiece {
} }
} }
impl fmt::Display for PlacedPiece {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}{}", self.piece, self.square)
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View file

@ -2,8 +2,9 @@
use super::position::BoardSide; use super::position::BoardSide;
use crate::piece::Color; use crate::piece::Color;
use std::fmt;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub(super) struct Flags(u8); pub(super) struct Flags(u8);
impl Flags { impl Flags {
@ -25,6 +26,12 @@ impl Flags {
} }
} }
impl fmt::Debug for Flags {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Flags({:08b})", self.0)
}
}
impl Default for Flags { impl Default for Flags {
fn default() -> Self { fn default() -> Self {
Flags(0b00001111) Flags(0b00001111)

View file

@ -8,7 +8,6 @@ use crate::{
BitBoard, Square, BitBoard, Square,
}; };
use std::cell::OnceCell; use std::cell::OnceCell;
use std::fmt;
/// A lateral side of the board relative to the player. Kingside is the side the /// A lateral side of the board relative to the player. Kingside is the side the
/// player's king starts on. Queenside is the side of the board the player's /// player's king starts on. Queenside is the side of the board the player's
@ -18,7 +17,7 @@ pub(crate) enum BoardSide {
Queen, Queen,
} }
#[derive(Clone, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub struct Position { pub struct Position {
color_to_move: Color, color_to_move: Color,
flags: Flags, flags: Flags,
@ -238,29 +237,6 @@ impl FromIterator<PlacedPiece> for Position {
} }
} }
impl fmt::Debug for Position {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Position {{\n")?;
write!(f, " color_to_move: {:?},\n", self.color_to_move)?;
write!(f, " pieces_per_color: [\n")?;
for bb in self.pieces_per_color {
write!(f, " {bb:?},\n")?;
}
write!(f, " ],\n")?;
write!(f, " pieces_per_type: [\n")?;
for color_bbs in self.pieces_per_type {
write!(f, " [\n")?;
for bb in color_bbs {
write!(f, " {bb:?},\n")?;
}
write!(f, " ],\n")?;
}
write!(f, " ],\n}}")
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;