make board scale better

This commit is contained in:
Simon Gardling 2025-02-07 21:39:17 -05:00
parent cc3d1b3d95
commit 41bc93276e
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
4 changed files with 24 additions and 24 deletions

View File

@ -14,23 +14,25 @@ pub struct Board {
impl fmt::Display for Board { impl fmt::Display for Board {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let horiz_sep_line = "-".repeat(BOARD_SIZE * 2 + 1); 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 // Print numbers at top so the board can be read more easier
write!(f, " ")?; write!(f, "{} ", space_padding)?;
for j in 0..BOARD_SIZE { for j in 0..BOARD_SIZE {
write!(f, "{} ", j)?; write!(f, "{:0PADDING$} ", j)?;
} }
writeln!(f)?; writeln!(f)?;
for i in 0..BOARD_SIZE { 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 { for j in 0..BOARD_SIZE {
write!( write!(
f, 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)?; writeln!(f)?;
@ -49,8 +51,7 @@ impl fmt::Display for Board {
// Print game over screen // Print game over screen
if self.game_over() { if self.game_over() {
match self.get_winner() { match self.get_winner() {
Some(Piece::Black) => writeln!(f, "Black Wins"), Some(piece) => writeln!(f, "{} Wins", piece.text()),
Some(Piece::White) => writeln!(f, "White Wins"),
None => writeln!(f, "Tie"), None => writeln!(f, "Tie"),
}? }?
} }
@ -67,10 +68,10 @@ impl Board {
} }
pub const fn starting_pos(mut self) -> Self { pub const fn starting_pos(mut self) -> Self {
self.place_unchecked(3, 3, Piece::White); self.place_unchecked((BOARD_SIZE / 2) - 1, (BOARD_SIZE / 2) - 1, Piece::White);
self.place_unchecked(4, 3, Piece::Black); self.place_unchecked(BOARD_SIZE / 2, (BOARD_SIZE / 2) - 1, Piece::Black);
self.place_unchecked(3, 4, Piece::Black); self.place_unchecked((BOARD_SIZE / 2) - 1, BOARD_SIZE / 2, Piece::Black);
self.place_unchecked(4, 4, Piece::White); self.place_unchecked(BOARD_SIZE / 2, BOARD_SIZE / 2, Piece::White);
self self
} }

View File

@ -146,7 +146,7 @@ impl Agent for ComplexAgent {
} }
impl ComplexAgent { impl ComplexAgent {
pub fn new(color: Piece) -> Self { pub const fn new(color: Piece) -> Self {
Self { Self {
color, color,
curr_move: None, curr_move: None,

View File

@ -11,9 +11,9 @@ impl std::fmt::Display for Game {
f, f,
"Players: {} ({}) and {} ({})", "Players: {} ({}) and {} ({})",
self.players[0].name(), self.players[0].name(),
self.players[0].color().text(), self.players[0].color().symbol(),
self.players[1].name(), self.players[1].name(),
self.players[1].color().text() self.players[1].color().symbol()
)?; )?;
write!(f, "{}", self.board)?; write!(f, "{}", self.board)?;
Ok(()) Ok(())
@ -73,14 +73,6 @@ impl Game {
} }
self.step(current_player); 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..."),
} }
} }
} }

View File

@ -12,12 +12,19 @@ impl Piece {
} }
} }
pub const fn text(&self) -> &'static str { pub const fn symbol(&self) -> &'static str {
match self { match self {
Piece::White => "", Piece::White => "",
Piece::Black => "", Piece::Black => "",
} }
} }
pub const fn text(&self) -> &'static str {
match self {
Piece::Black => "Black",
Piece::White => "White",
}
}
} }
impl std::ops::Not for Piece { impl std::ops::Not for Piece {