aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2023-01-29 06:29:31 +0300
committereug-vs <eugene@eug-vs.xyz>2023-01-29 07:19:22 +0300
commit688461af14f75e488b3a96ae991d85199c8cdc93 (patch)
treeb2f5ef71b90f837e670c723e040332d459b7add0 /src/main.rs
parent3667da48bfc5c742275cb9d8493242171aab155c (diff)
downloadchessnost-688461af14f75e488b3a96ae991d85199c8cdc93.tar.gz
feat: add time-control awareness
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs28
1 files changed, 24 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs
index 61f2cd3..13af134 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,10 +3,18 @@ use std::{f32::INFINITY, time::{Duration, Instant}};
use chessnost::board::Board;
fn main() {
+ let increment = Duration::from_secs(0);
+ let mut time_left = Duration::from_secs(60 * 10);
+ let manual_decrement = Duration::from_secs(7); // Time to sync moves with the game
+
let mut board = Board::new();
board.print();
+
loop {
+ time_left += increment;
+ time_left -= manual_decrement;
+
let mov = match board.read_move() {
Ok(m) => m,
Err(e) => {
@@ -18,9 +26,21 @@ fn main() {
board.make_move(mov);
board.print();
- let start = Instant::now();
- let (score, pv) = board.iterative_deepening(10, Duration::from_secs(7));
- println!("Finished in {:?}: score={:?}", start.elapsed(), score);
+ let allowed_move_duration = time_left / 20;
+ let max_depth = 6 + (board.ply as i32 / 15) as u8;
+ println!("~{:?} left, allocating {:?} for a move", time_left, allowed_move_duration);
+
+ let move_start = Instant::now();
+ let (score, pv) = board.iterative_deepening(max_depth, allowed_move_duration);
+ let elapsed = move_start.elapsed();
+
+ if time_left >= elapsed {
+ time_left -= elapsed;
+ } else {
+ println!("You are probably out of time. I will keep going anyway...");
+ }
+
+ println!("Finished in {:?}: score={:?}", elapsed, score);
let mov = pv[0];
println!("{:?}", mov);
@@ -29,6 +49,6 @@ fn main() {
println!("Score for {:?} is now: {}", board.color(), board.quiscence(-INFINITY, INFINITY));
println!("\nPondering for 3 seconds...");
- board.iterative_deepening(5, Duration::from_secs(3));
+ board.iterative_deepening(max_depth - 1, Duration::from_secs(3));
}
}