Minor UI improvement
parent
ffae0342f1
commit
6f9ab43fff
|
@ -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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue