Move a whole bunch of stuff to the new chessfriend_board package
This commit is contained in:
parent
797606785e
commit
1d82d27f84
12 changed files with 1130 additions and 41 deletions
68
board/src/display.rs
Normal file
68
board/src/display.rs
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
// Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
use crate::Board;
|
||||
use chessfriend_core::{File, Rank, Square};
|
||||
use std::fmt;
|
||||
|
||||
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.piece_on_square(square) {
|
||||
Some(placed_piece) => write!(f, "{} ", placed_piece.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}");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue