Board: make string formatting stuff const

This commit is contained in:
2025-03-20 02:03:11 -04:00
parent c46b8ef8b8
commit 92b982cf35
3 changed files with 13 additions and 5 deletions

View File

@@ -88,22 +88,22 @@ pub struct Board {
impl fmt::Display for Board {
#[allow(clippy::repeat_once)] // clippy gets mad about when PADDING == 1
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let horiz_sep_line = "-".repeat((Self::SIZE * 2 + 1) as usize);
const HORIZ_SEP_LINE: &str = const_str::repeat!("-", (Board::SIZE * 2 + 1) as usize);
// basically calculates the # of digits BOARD_SIZE needs
const PADDING: usize = (Board::SIZE - 1).ilog10() as usize + 1;
let space_padding = " ".repeat(PADDING);
const SPACE_PADDING: &str = const_str::repeat!(" ", PADDING);
// Print numbers at top so the board can be read more easier
write!(f, "{} ", space_padding)?;
write!(f, "{} ", SPACE_PADDING)?;
for j in (0..Self::SIZE).rev() {
write!(f, "{:0PADDING$} ", j)?;
}
writeln!(f)?;
for i in (0..Self::SIZE).rev() {
writeln!(f, "{}{}", space_padding, horiz_sep_line)?;
writeln!(f, "{}{}", SPACE_PADDING, HORIZ_SEP_LINE)?;
write!(f, "{:0PADDING$}|", i)?;
for j in (0..Self::SIZE).rev() {
@@ -120,7 +120,7 @@ impl fmt::Display for Board {
}
// put a line at the bottom of the board too
writeln!(f, " {}", horiz_sep_line)?;
writeln!(f, " {}", HORIZ_SEP_LINE)?;
// Print the current score
write!(