From 26a15985a888e01c4ecfef3dba171e26f9846347 Mon Sep 17 00:00:00 2001 From: Baptiste Fouques Date: Tue, 21 Feb 2023 16:46:27 +0100 Subject: [PATCH] time control --- chess_uci/src/lib.rs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/chess_uci/src/lib.rs b/chess_uci/src/lib.rs index f36acfd..a6d3454 100644 --- a/chess_uci/src/lib.rs +++ b/chess_uci/src/lib.rs @@ -12,7 +12,7 @@ use std::time::Duration; use chess::{Board, ChessMove, Game, Action}; const DEFAULT_TIME: (Option, Option) = - (Some(Duration::from_secs(120 * 60)), None); + (Some(Duration::from_secs(90 * 60)), None); /// Structure used to manage a chess game with a uci engine. /// @@ -44,7 +44,7 @@ pub struct UciEngine { initial: Board, game: Game, /// white (total, incr), black (total, incr) - time: [(Option, Option); 2], + timer: [(Option, Option); 2], /// white, black player: [Player; 2], } @@ -77,7 +77,7 @@ impl UciEngine { uciok: false, initial: Board::default(), game: Game::new(), - time: [DEFAULT_TIME; 2], + timer: [DEFAULT_TIME; 2], player: [Player::Human { elo: None }; 2], } } @@ -88,16 +88,16 @@ impl UciEngine { GameOption::TotalTime { color, value } => { old_value = GameOption::TotalTime { color, - value: self.time[color.to_index()].0, + value: self.timer[color.to_index()].0, }; - self.time[color.to_index()].0 = value + self.timer[color.to_index()].0 = value } GameOption::Increment { color, value } => { old_value = GameOption::Increment { color, - value: self.time[color.to_index()].1, + value: self.timer[color.to_index()].1, }; - self.time[color.to_index()].1 = value + self.timer[color.to_index()].1 = value } GameOption::Player { color, value } => { old_value = GameOption::Player { @@ -359,8 +359,8 @@ impl UciEngine { option: Opt::UCIElo { value: elo }, }); } - let (wtime, wincr) = self.time[Color::White.to_index()]; - let (btime, bincr) = self.time[Color::Black.to_index()]; + let (wtime, wincr) = self.timer[Color::White.to_index()]; + let (btime, bincr) = self.timer[Color::Black.to_index()]; self.push(GuiCommand::Go { wtime, wincr, @@ -397,5 +397,19 @@ impl UciEngine { pub fn side_to_move(&self) -> Color { self.game.side_to_move() } + + pub fn clock(&self, color: Color) -> Option { + self.timer[if color == Color::White {0} else {1}].0 + } + + pub fn update_clock(&mut self, elapsed: Duration) { + let index = if self.side_to_move() == Color::White {0} else {1}; + self.timer[index] = + match self.timer[index] { + (Some(base), None) => (Some(base - elapsed), None), + (Some(base), Some(incr)) => (Some(base - elapsed + incr), Some(incr)), + _ => (None, None) + }; + } } // LocalWords: uci