This makes Position immutable, even if declared mut. I think this will also make it easier to build positions going forward. Moving to a PieceBitBoards struct also required a lot of places that return owned BitBoards to return refs. This is basically fine except for adding a few more & here and there. This was a huge refactoring project. All the move generators and a bunch of BitBoard stuff needed to be updated.
69 lines
1.7 KiB
Rust
69 lines
1.7 KiB
Rust
// Eryn Wells <eryn@erynwells.me>
|
|
|
|
use crate::{File, Position, Rank, Square};
|
|
use std::{fmt, fmt::Write};
|
|
|
|
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 {
|
|
let mut output = String::new();
|
|
|
|
output.push_str(" +-----------------+\n");
|
|
|
|
for rank in Rank::ALL.iter().rev() {
|
|
write!(output, "{} | ", 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!(output, "{} ", placed_piece.piece())?,
|
|
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, PositionBuilder};
|
|
|
|
#[test]
|
|
fn empty() {
|
|
let pos = Position::empty();
|
|
let diagram = DiagramFormatter(&pos);
|
|
println!("{}", diagram);
|
|
}
|
|
|
|
#[test]
|
|
fn one_king() {
|
|
let pos = PositionBuilder::new()
|
|
.place_piece(piece!(Black King on H3))
|
|
.build();
|
|
let diagram = DiagramFormatter(&pos);
|
|
println!("{}", diagram);
|
|
}
|
|
|
|
#[test]
|
|
fn starting() {
|
|
let pos = Position::starting();
|
|
let diagram = DiagramFormatter(&pos);
|
|
println!("{}", diagram);
|
|
}
|
|
}
|