From 6b9027bdd8f5bd6bed1e3aff4be554688185ec11 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Fri, 1 Mar 2019 21:46:06 -0500 Subject: [PATCH] Build out system to get formatted display. --- engine/src/lib.rs | 54 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/engine/src/lib.rs b/engine/src/lib.rs index 6fa013c..9c041af 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -59,6 +59,11 @@ impl Board { tiles: playing_tiles, } } + + fn get_index(&self, row: u32, column: u32) -> usize { + let width = self.dimensions.get_size(); + (row * width + column) as usize + } } static COLUMN_COORDINATE_BORDER: &str = " ABCDEFGHIJKL"; @@ -75,16 +80,33 @@ impl fmt::Display for Board { } write!(formatter, "\n")?; + for row in 0..width { + write!(formatter, "{ }", (row + 1).to_string())?; + if row % 2 == 0 { + write!(formatter, " ")?; + } -// for {} -// -// let square = match tile { -// Piece::LightPawn => "♙", -// Piece::LightKing => "♔", -// Piece::DarkPawn => "♟", -// Piece::DarkKing => "♚", -// }; + for col in 0..(width / 2) { + let token = match self.tiles[self.get_index(row, col)] { + BoardTile::Piece(Piece::LightPawn) => "○", + BoardTile::Piece(Piece::LightKing) => "⍟", + BoardTile::Piece(Piece::DarkPawn) => "●", + BoardTile::Piece(Piece::DarkKing) => "✪", + BoardTile::Empty => "✪", + }; + + let adjacent = if row % 2 == 1 && col == width - 1 { + "" + } else { + " " + }; + + write!(formatter, "{}{}", token, adjacent)?; + } + + write!(formatter, "\n")?; + } Ok(()) } @@ -99,8 +121,20 @@ mod tests { #[test] fn setup_international_board() { - let expected = r#" ABCDEFGHIJ -"#; + let expected = + "\ + ABCDEFGHIJ\n\ + 1 ○ ○ ○ ○ ○\n\ + 2○ ○ ○ ○ ○ \n\ + 3 ○ ○ ○ ○ ○\n\ + 4○ ○ ○ ○ ○ \n\ + 5 ◰ ◰ ◰ ◰ ◰\n\ + 6◰ ◰ ◰ ◰ ◰ \n\ + 7 ● ● ● ● ●\n\ + 8● ● ● ● ● \n\ + 9 ● ● ● ● ●\n\ + 10● ● ● ● ● \n\ + "; assert_eq!(Board::new(BoardSize::International).to_string(), expected); } }