2023-12-26 21:37:22 -07:00
|
|
|
// Eryn Wells <eryn@erynwells.me>
|
|
|
|
|
2024-01-06 16:25:03 -08:00
|
|
|
use crate::{File, Position, Rank, Square};
|
|
|
|
use std::{fmt, fmt::Write};
|
2023-12-26 21:37:22 -07:00
|
|
|
|
2024-01-14 10:23:35 -08:00
|
|
|
pub struct DiagramFormatter<'a, PosC>(&'a Position<PosC>);
|
2023-12-26 21:37:22 -07:00
|
|
|
|
2024-01-14 10:23:35 -08:00
|
|
|
impl<'a, PosC> DiagramFormatter<'a, PosC> {
|
|
|
|
pub fn new(position: &'a Position<PosC>) -> DiagramFormatter<PosC> {
|
2023-12-28 15:11:57 -07:00
|
|
|
DiagramFormatter(position)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-14 10:23:35 -08:00
|
|
|
impl<'a, PosC> fmt::Display for DiagramFormatter<'a, PosC> {
|
2023-12-26 21:37:22 -07:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let mut output = String::new();
|
|
|
|
|
|
|
|
output.push_str(" +-----------------+\n");
|
|
|
|
|
2024-01-06 16:25:03 -08:00
|
|
|
for rank in Rank::ALL.iter().rev() {
|
|
|
|
write!(output, "{} | ", rank)?;
|
2023-12-26 21:37:22 -07:00
|
|
|
|
2024-01-06 16:25:03 -08:00
|
|
|
for file in File::ALL.iter() {
|
|
|
|
let square = Square::from_file_rank(*file, *rank);
|
2023-12-26 21:37:22 -07:00
|
|
|
match self.0.piece_on_square(square) {
|
2023-12-29 09:17:33 -08:00
|
|
|
Some(placed_piece) => write!(output, "{} ", placed_piece.piece())?,
|
2023-12-26 21:37:22 -07:00
|
|
|
None => output.push_str(". "),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output.push_str("|\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
output.push_str(" +-----------------+\n");
|
|
|
|
output.push_str(" a b c d e f g h\n");
|
|
|
|
|
|
|
|
write!(f, "{}", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::piece::{Color, Piece};
|
|
|
|
use crate::Position;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty() {
|
|
|
|
let pos = Position::empty();
|
|
|
|
let diagram = DiagramFormatter(&pos);
|
|
|
|
println!("{}", diagram);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn one_king() {
|
|
|
|
let mut pos = Position::empty();
|
|
|
|
pos.place_piece(
|
2024-01-06 16:25:03 -08:00
|
|
|
Piece::king(Color::Black),
|
|
|
|
Square::from_algebraic_str("h3").expect("h3"),
|
2023-12-26 21:37:22 -07:00
|
|
|
)
|
|
|
|
.expect("Unable to place piece");
|
|
|
|
|
|
|
|
let diagram = DiagramFormatter(&pos);
|
|
|
|
println!("{}", diagram);
|
|
|
|
}
|
2023-12-27 07:59:05 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn starting() {
|
|
|
|
let pos = Position::starting();
|
|
|
|
let diagram = DiagramFormatter(&pos);
|
|
|
|
println!("{}", diagram);
|
|
|
|
}
|
2023-12-26 21:37:22 -07:00
|
|
|
}
|