69 lines
1.6 KiB
Rust
69 lines
1.6 KiB
Rust
// Eryn Wells <eryn@erynwells.me>
|
|
|
|
use crate::Board;
|
|
use chessfriend_core::{File, Rank, Square};
|
|
use std::fmt;
|
|
|
|
#[must_use]
|
|
pub struct DiagramFormatter<'a>(&'a Board);
|
|
|
|
impl<'a> DiagramFormatter<'a> {
|
|
pub fn new(board: &'a Board) -> Self {
|
|
Self(board)
|
|
}
|
|
}
|
|
|
|
impl<'a> fmt::Display for DiagramFormatter<'a> {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
writeln!(f, " ╔═════════════════╗")?;
|
|
|
|
for rank in Rank::ALL.into_iter().rev() {
|
|
write!(f, "{rank} ║ ")?;
|
|
|
|
for file in File::ALL {
|
|
let square = Square::from_file_rank(file, rank);
|
|
match self.0.get_piece(square) {
|
|
Some(piece) => write!(f, "{piece} ")?,
|
|
None => write!(f, "· ")?,
|
|
}
|
|
}
|
|
|
|
writeln!(f, "║")?;
|
|
}
|
|
|
|
writeln!(f, " ╚═════════════════╝")?;
|
|
writeln!(f, " a b c d e f g h")?;
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::{test_board, Board};
|
|
|
|
#[test]
|
|
#[ignore]
|
|
fn empty() {
|
|
let pos = test_board!(empty);
|
|
let diagram = DiagramFormatter(&pos);
|
|
println!("{diagram}");
|
|
}
|
|
|
|
#[test]
|
|
#[ignore]
|
|
fn one_king() {
|
|
let pos = test_board![Black King on H3];
|
|
let diagram = DiagramFormatter(&pos);
|
|
println!("{diagram}");
|
|
}
|
|
|
|
#[test]
|
|
#[ignore]
|
|
fn starting() {
|
|
let pos = test_board!(starting);
|
|
let diagram = DiagramFormatter(&pos);
|
|
println!("{diagram}");
|
|
}
|
|
}
|