Build out system to get formatted display.

This commit is contained in:
Dorian 2019-03-01 21:46:06 -05:00
parent fc9a706c90
commit 6b9027bdd8
1 changed files with 44 additions and 10 deletions

View File

@ -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);
}
}