manage time

This commit is contained in:
Baptiste Fouques 2023-02-23 19:55:27 +01:00
parent 71a166c927
commit 9e08b23ec7
1 changed files with 13 additions and 4 deletions

View File

@ -45,6 +45,7 @@ pub struct UciEngine<Fi: Read, Fo: Write> {
game: Game,
/// white (total, incr), black (total, incr)
timer: [(Option<Duration>, Option<Duration>); 2],
initial_timer: [(Option<Duration>, Option<Duration>); 2],
/// white, black
player: [Player; 2],
}
@ -78,6 +79,7 @@ impl<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
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<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
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<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
/// 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<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
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)
};
}