// Eryn Wells use crate::{File, Position, Rank, Square}; use std::fmt; pub struct DiagramFormatter<'a>(&'a Position); impl<'a> DiagramFormatter<'a> { pub fn new(position: &'a Position) -> DiagramFormatter { DiagramFormatter(position) } } impl<'a> fmt::Display for DiagramFormatter<'a> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, " +-----------------+\n")?; for rank in Rank::ALL.iter().rev() { write!(f, "{rank} | ")?; for file in File::ALL.iter() { let square = Square::from_file_rank(*file, *rank); match self.0.piece_on_square(square) { Some(placed_piece) => write!(f, "{} ", placed_piece.piece())?, None => write!(f, ". ")?, } } write!(f, "|\n")?; } write!(f, " +-----------------+\n")?; write!(f, " a b c d e f g h\n")?; Ok(()) } } #[cfg(test)] mod tests { use super::*; use crate::Position; #[test] #[ignore] fn empty() { let pos = Position::empty(); let diagram = DiagramFormatter(&pos); println!("{}", diagram); } #[test] #[ignore] fn one_king() { let pos = position![Black King on H3]; let diagram = DiagramFormatter(&pos); println!("{}", diagram); } #[test] #[ignore] fn starting() { let pos = Position::starting(); let diagram = DiagramFormatter(&pos); println!("{}", diagram); } }