time control

This commit is contained in:
Baptiste Fouques 2023-02-21 16:46:27 +01:00
parent 4dd643f66b
commit 26a15985a8
1 changed files with 23 additions and 9 deletions

View File

@ -12,7 +12,7 @@ use std::time::Duration;
use chess::{Board, ChessMove, Game, Action};
const DEFAULT_TIME: (Option<Duration>, Option<Duration>) =
(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<Fi: Read, Fo: Write> {
initial: Board,
game: Game,
/// white (total, incr), black (total, incr)
time: [(Option<Duration>, Option<Duration>); 2],
timer: [(Option<Duration>, Option<Duration>); 2],
/// white, black
player: [Player; 2],
}
@ -77,7 +77,7 @@ impl<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
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<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
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<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
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<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
pub fn side_to_move(&self) -> Color {
self.game.side_to_move()
}
pub fn clock(&self, color: Color) -> Option<Duration> {
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