From 6f9ab43fffccaf450c6a092b415080785de92cd5 Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 4 Mar 2024 21:01:53 -0800 Subject: [PATCH] Minor UI improvement --- src/board/board.rs | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/board/board.rs b/src/board/board.rs index 0f220a6..539090c 100644 --- a/src/board/board.rs +++ b/src/board/board.rs @@ -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, } 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, + }) } }