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 std::fmt::Display;
use termion::color::{self, Color};
use termion::color;
use super::{PlayerAction, TreeElement}; use super::{PlayerAction, TreeElement};
use crate::util::{Player, Symb}; use crate::util::{Player, Symb};
@ -49,18 +48,30 @@ enum Token {
#[derive(Clone)] #[derive(Clone)]
pub struct Board { pub struct Board {
board: [Option<(Symb, Player)>; 11], board: [Option<(Symb, Player)>; 11],
/// Number of Nones in `board`
free_spots: usize, free_spots: usize,
/// Index of the last board index that was changed
last_placed: Option<usize>,
} }
impl Display for Board { impl 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 {
// Print board // Print board
for i in &self.board { for (i, o) in self.board.iter().enumerate() {
match i { match o {
Some((symb, player)) => write!( Some((symb, player)) => write!(
f, 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, symb,
color::Fg(color::Reset) color::Fg(color::Reset)
)?, )?,
@ -78,6 +89,7 @@ impl Board {
Self { Self {
free_spots: 11, free_spots: 11,
board: Default::default(), board: Default::default(),
last_placed: None,
} }
} }
@ -174,6 +186,7 @@ impl Board {
self.board[action.pos] = Some((action.symb, player)); self.board[action.pos] = Some((action.symb, player));
self.free_spots -= 1; self.free_spots -= 1;
self.last_placed = Some(action.pos);
true true
} }
@ -333,6 +346,10 @@ impl Board {
} }
} }
Some(Self { board, free_spots }) Some(Self {
board,
free_spots,
last_placed: None,
})
} }
} }