From 9e08b23ec7d8f553bfacf53d4ffe85482facc1b3 Mon Sep 17 00:00:00 2001 From: Baptiste Fouques Date: Thu, 23 Feb 2023 19:55:27 +0100 Subject: [PATCH] manage time --- chess_uci/src/lib.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/chess_uci/src/lib.rs b/chess_uci/src/lib.rs index 9b82147..56a675b 100644 --- a/chess_uci/src/lib.rs +++ b/chess_uci/src/lib.rs @@ -45,6 +45,7 @@ pub struct UciEngine { game: Game, /// white (total, incr), black (total, incr) timer: [(Option, Option); 2], + initial_timer: [(Option, Option); 2], /// white, black player: [Player; 2], } @@ -78,6 +79,7 @@ impl UciEngine { initial: Board::default(), game: Game::new(), timer: [DEFAULT_TIME; 2], + initial_timer: [DEFAULT_TIME; 2], player: [Player::Human { elo: None }; 2], } } @@ -90,21 +92,23 @@ impl UciEngine { color, value: self.timer[color.to_index()].0, }; - self.timer[color.to_index()].0 = value + self.timer[color.to_index()].0 = value; + self.initial_timer[color.to_index()].0 = value; } GameOption::Increment { color, value } => { old_value = GameOption::Increment { color, value: self.timer[color.to_index()].1, }; - self.timer[color.to_index()].1 = value + self.timer[color.to_index()].1 = value; + self.initial_timer[color.to_index()].1 = value; } GameOption::Player { color, value } => { old_value = GameOption::Player { color, value: self.player[color.to_index()], }; - self.player[color.to_index()] = value + self.player[color.to_index()] = value; } } @@ -134,9 +138,14 @@ impl UciEngine { /// Set a new board internally and tell the engine pub fn new_game(&mut self) { self.initial = Board::default(); + self.timer = self.initial_timer; self.game = Game::new_with_board(self.initial); self.push(GuiCommand::Stop); self.push(GuiCommand::UciNewGame); + self.push(GuiCommand::Position { + position: Some(self.initial), + moves: self.game.actions().to_vec(), + }); } /// Play a move @@ -404,7 +413,7 @@ impl UciEngine { self.timer[index] = match self.timer[index] { (Some(base), None) => (Some(base - elapsed), None), - (Some(base), Some(incr)) => (Some(base - elapsed + incr), Some(incr)), + (Some(base), Some(incr)) if base >= elapsed => (Some(base - elapsed + incr), Some(incr)), _ => (None, None) }; }