make board scale better
This commit is contained in:
25
src/board.rs
25
src/board.rs
@@ -14,23 +14,25 @@ pub struct Board {
|
||||
impl fmt::Display for Board {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let horiz_sep_line = "-".repeat(BOARD_SIZE * 2 + 1);
|
||||
const PADDING: usize = (BOARD_SIZE - 1).ilog10() as usize + 1;
|
||||
let space_padding = " ".repeat(PADDING);
|
||||
|
||||
// Print numbers at top so I can read the board easier
|
||||
write!(f, " ")?;
|
||||
// Print numbers at top so the board can be read more easier
|
||||
write!(f, "{} ", space_padding)?;
|
||||
for j in 0..BOARD_SIZE {
|
||||
write!(f, "{} ", j)?;
|
||||
write!(f, "{:0PADDING$} ", j)?;
|
||||
}
|
||||
writeln!(f)?;
|
||||
|
||||
for i in 0..BOARD_SIZE {
|
||||
writeln!(f, " {}", horiz_sep_line)?;
|
||||
writeln!(f, "{}{}", space_padding, horiz_sep_line)?;
|
||||
|
||||
write!(f, "{}|", i)?;
|
||||
write!(f, "{:0PADDING$}|", i)?;
|
||||
for j in 0..BOARD_SIZE {
|
||||
write!(
|
||||
f,
|
||||
"{}|",
|
||||
self.get(i, j).as_ref().map(Piece::text).unwrap_or(" ")
|
||||
self.get(i, j).as_ref().map(Piece::symbol).unwrap_or(" ")
|
||||
)?;
|
||||
}
|
||||
writeln!(f)?;
|
||||
@@ -49,8 +51,7 @@ impl fmt::Display for Board {
|
||||
// Print game over screen
|
||||
if self.game_over() {
|
||||
match self.get_winner() {
|
||||
Some(Piece::Black) => writeln!(f, "Black Wins"),
|
||||
Some(Piece::White) => writeln!(f, "White Wins"),
|
||||
Some(piece) => writeln!(f, "{} Wins", piece.text()),
|
||||
None => writeln!(f, "Tie"),
|
||||
}?
|
||||
}
|
||||
@@ -67,10 +68,10 @@ impl Board {
|
||||
}
|
||||
|
||||
pub const fn starting_pos(mut self) -> Self {
|
||||
self.place_unchecked(3, 3, Piece::White);
|
||||
self.place_unchecked(4, 3, Piece::Black);
|
||||
self.place_unchecked(3, 4, Piece::Black);
|
||||
self.place_unchecked(4, 4, Piece::White);
|
||||
self.place_unchecked((BOARD_SIZE / 2) - 1, (BOARD_SIZE / 2) - 1, Piece::White);
|
||||
self.place_unchecked(BOARD_SIZE / 2, (BOARD_SIZE / 2) - 1, Piece::Black);
|
||||
self.place_unchecked((BOARD_SIZE / 2) - 1, BOARD_SIZE / 2, Piece::Black);
|
||||
self.place_unchecked(BOARD_SIZE / 2, BOARD_SIZE / 2, Piece::White);
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user