Iterate by Rank and File when building a diagram

This commit is contained in:
Eryn Wells 2024-01-06 16:25:03 -08:00
parent aac1a85507
commit c3e3ebfa97

View file

@ -1,8 +1,7 @@
// Eryn Wells <eryn@erynwells.me> // Eryn Wells <eryn@erynwells.me>
use crate::{Position, Square}; use crate::{File, Position, Rank, Square};
use std::fmt; use std::{fmt, fmt::Write};
use std::fmt::Write;
pub struct DiagramFormatter<'a>(&'a Position); pub struct DiagramFormatter<'a>(&'a Position);
@ -18,11 +17,11 @@ impl<'a> fmt::Display for DiagramFormatter<'a> {
output.push_str(" +-----------------+\n"); output.push_str(" +-----------------+\n");
for rank in (0..8).rev() { for rank in Rank::ALL.iter().rev() {
write!(output, "{} | ", rank + 1)?; write!(output, "{} | ", rank)?;
for file in 0..8 { for file in File::ALL.iter() {
let square = Square::from_rank_file(rank, file).unwrap(); let square = Square::from_file_rank(*file, *rank);
match self.0.piece_on_square(square) { match self.0.piece_on_square(square) {
Some(placed_piece) => write!(output, "{} ", placed_piece.piece())?, Some(placed_piece) => write!(output, "{} ", placed_piece.piece())?,
None => output.push_str(". "), None => output.push_str(". "),
@ -56,8 +55,8 @@ mod tests {
fn one_king() { fn one_king() {
let mut pos = Position::empty(); let mut pos = Position::empty();
pos.place_piece( pos.place_piece(
&Piece::king(Color::Black), Piece::king(Color::Black),
&Square::from_algebraic_str("h3").expect("h3"), Square::from_algebraic_str("h3").expect("h3"),
) )
.expect("Unable to place piece"); .expect("Unable to place piece");