diff --git a/src/board.rs b/src/board.rs index 6cae402..7ab8023 100644 --- a/src/board.rs +++ b/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 } diff --git a/src/complexagent.rs b/src/complexagent.rs index 852445a..2d988d2 100644 --- a/src/complexagent.rs +++ b/src/complexagent.rs @@ -146,7 +146,7 @@ impl Agent for ComplexAgent { } impl ComplexAgent { - pub fn new(color: Piece) -> Self { + pub const fn new(color: Piece) -> Self { Self { color, curr_move: None, diff --git a/src/game.rs b/src/game.rs index 1b5b861..e70acfe 100644 --- a/src/game.rs +++ b/src/game.rs @@ -11,9 +11,9 @@ impl std::fmt::Display for Game { f, "Players: {} ({}) and {} ({})", self.players[0].name(), - self.players[0].color().text(), + self.players[0].color().symbol(), self.players[1].name(), - self.players[1].color().text() + self.players[1].color().symbol() )?; write!(f, "{}", self.board)?; Ok(()) @@ -73,14 +73,6 @@ impl Game { } self.step(current_player); - - // std::thread::sleep(Duration::from_millis(200)); - } - // Print Game Over Screen - println!("{}", self); - match self.board.get_winner() { - Some(winner) => println!("Game Over! {} Wins!", winner.text()), - None => print!("It's a tie!! You either both suck or are both really good..."), } } } diff --git a/src/piece.rs b/src/piece.rs index 5b50987..e3014c5 100644 --- a/src/piece.rs +++ b/src/piece.rs @@ -12,12 +12,19 @@ impl Piece { } } - pub const fn text(&self) -> &'static str { + pub const fn symbol(&self) -> &'static str { match self { Piece::White => "■", Piece::Black => "□", } } + + pub const fn text(&self) -> &'static str { + match self { + Piece::Black => "Black", + Piece::White => "White", + } + } } impl std::ops::Not for Piece {