Minor UI improvement

master
Mark 2024-03-04 21:01:53 -08:00
parent ffae0342f1
commit 6f9ab43fff
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
1 changed files with 23 additions and 6 deletions

View File

@ -1,6 +1,5 @@
use std::fmt::Display;
use termion::color;
use termion::color::{self, Color};
use super::{PlayerAction, TreeElement};
use crate::util::{Player, Symb};
@ -49,18 +48,30 @@ enum Token {
#[derive(Clone)]
pub struct Board {
board: [Option<(Symb, Player)>; 11],
/// Number of Nones in `board`
free_spots: usize,
/// Index of the last board index that was changed
last_placed: Option<usize>,
}
impl Display for Board {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// Print board
for i in &self.board {
match i {
for (i, o) in self.board.iter().enumerate() {
match o {
Some((symb, player)) => write!(
f,
"{}{}{}",
color::Fg(player.color()),
// If index matches last placed, draw symbol in red.
// If last_placed is None, this check will always fail
// since self.board.len is always greater than i.
if self.last_placed.unwrap_or(self.board.len()) == i {
color::Fg(&color::Red as &dyn Color)
} else {
color::Fg(player.color())
},
symb,
color::Fg(color::Reset)
)?,
@ -78,6 +89,7 @@ impl Board {
Self {
free_spots: 11,
board: Default::default(),
last_placed: None,
}
}
@ -174,6 +186,7 @@ impl Board {
self.board[action.pos] = Some((action.symb, player));
self.free_spots -= 1;
self.last_placed = Some(action.pos);
true
}
@ -333,6 +346,10 @@ impl Board {
}
}
Some(Self { board, free_spots })
Some(Self {
board,
free_spots,
last_placed: None,
})
}
}