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
|
|
|
|
|
|
|
pub struct DiagramFormatter<'a>(&'a Position);
|
|
|
|
|
2023-12-28 15:11:57 -07:00
|
|
|
impl<'a> DiagramFormatter<'a> {
|
|
|
|
pub fn new(position: &'a Position) -> DiagramFormatter {
|
|
|
|
DiagramFormatter(position)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-26 21:37:22 -07:00
|
|
|
impl<'a> fmt::Display for DiagramFormatter<'a> {
|
|
|
|
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};
|
2024-01-19 18:08:41 -08:00
|
|
|
use crate::{Position, PositionBuilder};
|
2023-12-26 21:37:22 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty() {
|
|
|
|
let pos = Position::empty();
|
|
|
|
let diagram = DiagramFormatter(&pos);
|
|
|
|
println!("{}", diagram);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn one_king() {
|
2024-01-19 18:08:41 -08:00
|
|
|
let pos = PositionBuilder::new()
|
|
|
|
.place_piece(piece!(Black King on H3))
|
|
|
|
.build();
|
2023-12-26 21:37:22 -07:00
|
|
|
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
|
|
|
}
|