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