[board] Implement a DiagramFormatter that writes a graphical diagram of a Position
Remove fmt::Display from Position and move display to a DiagramFormatter type that implements that trait and writes a position to a formatter.
This commit is contained in:
parent
8d06cbf0f8
commit
19c48b9816
3 changed files with 63 additions and 25 deletions
61
board/src/position/diagram_formatter.rs
Normal file
61
board/src/position/diagram_formatter.rs
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
// Eryn Wells <eryn@erynwells.me>
|
||||||
|
|
||||||
|
use crate::{Position, Square};
|
||||||
|
use std::fmt;
|
||||||
|
use std::fmt::Write;
|
||||||
|
|
||||||
|
pub struct DiagramFormatter<'a>(&'a 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 (0..8).rev() {
|
||||||
|
write!(output, "{} | ", rank + 1)?;
|
||||||
|
|
||||||
|
for file in 0..8 {
|
||||||
|
let square = Square::from_rank_file(rank, file).unwrap();
|
||||||
|
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;
|
||||||
|
|
||||||
|
#[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(
|
||||||
|
&Piece::king(Color::Black),
|
||||||
|
&Square::from_algebraic_str("h3").expect("h3"),
|
||||||
|
)
|
||||||
|
.expect("Unable to place piece");
|
||||||
|
|
||||||
|
let diagram = DiagramFormatter(&pos);
|
||||||
|
println!("{}", diagram);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,7 @@
|
||||||
|
mod diagram_formatter;
|
||||||
mod pieces;
|
mod pieces;
|
||||||
mod position;
|
mod position;
|
||||||
|
|
||||||
|
pub use diagram_formatter::DiagramFormatter;
|
||||||
pub use pieces::Pieces;
|
pub use pieces::Pieces;
|
||||||
pub use position::Position;
|
pub use position::Position;
|
||||||
|
|
|
@ -144,31 +144,6 @@ impl fmt::Debug for Position {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Position {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
let mut output = String::new();
|
|
||||||
|
|
||||||
output.push_str(" +-----------------+\n");
|
|
||||||
|
|
||||||
for rank in (0..8).rev() {
|
|
||||||
output.push_str("{rank} | ");
|
|
||||||
|
|
||||||
for file in 0..8 {
|
|
||||||
let square = Square::from_rank_file(rank, file);
|
|
||||||
// TODO: Get piece at square
|
|
||||||
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue