diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-02-26 23:26:51 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-02-26 23:27:20 +0300 |
commit | 8e7ac2814c4a92eba79f9c626ad14ac0b7bfef3f (patch) | |
tree | da30e36423d5b1aa4aaaa2666b985e9674680850 /src/grossmeister | |
parent | 54e72754d0fae2b2768b66c39c72e9afaba0cba8 (diff) | |
download | chessnost-8e7ac2814c4a92eba79f9c626ad14ac0b7bfef3f.tar.gz |
feat: add clock and time management to UCI
Diffstat (limited to 'src/grossmeister')
-rw-r--r-- | src/grossmeister/UCI.rs | 66 | ||||
-rw-r--r-- | src/grossmeister/search.rs | 1 |
2 files changed, 53 insertions, 14 deletions
diff --git a/src/grossmeister/UCI.rs b/src/grossmeister/UCI.rs index d5e67a1..2c466a1 100644 --- a/src/grossmeister/UCI.rs +++ b/src/grossmeister/UCI.rs @@ -1,6 +1,6 @@ -use std::{io::stdin, thread::{Builder, JoinHandle}, str::Split}; +use std::{io::stdin, thread::{Builder, JoinHandle, spawn, sleep}, str::Split, time::Duration}; -use crate::{board::{Board, io::IO}, moves::Move}; +use crate::{board::{Board, io::IO, color::Color}, moves::Move}; use super::Grossmeister; @@ -91,7 +91,6 @@ impl Grossmeister { // his transposition table is more saturated *self = hand.join().unwrap(); } - self.should_halt.store(false, std::sync::atomic::Ordering::Relaxed); } "ponderhit" => todo!(), "quit" => break, @@ -106,10 +105,30 @@ impl Grossmeister { match token { "searchmoves" => todo!(), "ponder" => todo!(), - "wtime" => {} - "btime" => {} - "winc" => {} - "binc" => {} + "wtime" => { + if let Some(time) = tokens.next() { + let time: u64 = time.parse().unwrap(); + self.board.clock.time[Color::White as usize] = Duration::from_millis(time); + } + } + "btime" => { + if let Some(time) = tokens.next() { + let time: u64 = time.parse().unwrap(); + self.board.clock.time[Color::Black as usize] = Duration::from_millis(time); + } + } + "winc" => { + if let Some(time) = tokens.next() { + let time: u64 = time.parse().unwrap(); + self.board.clock.increment[Color::White as usize] = Duration::from_millis(time); + } + } + "binc" => { + if let Some(time) = tokens.next() { + let time: u64 = time.parse().unwrap(); + self.board.clock.increment[Color::White as usize] = Duration::from_millis(time); + } + } "movestogo" => {} "depth" => { if let Some(depth) = tokens.next() { @@ -119,7 +138,15 @@ impl Grossmeister { }, "nodes" => todo!(), "mate" => todo!(), - "movetime" => todo!(), + "movetime" => { + if let Some(time) = tokens.next() { + let time: u64 = time.parse().unwrap(); + let duration = Duration::from_millis(time); + + self.create_terminator_thread(duration); + return self.create_search_thread(u8::MAX) + } + }, "infinite" => { return self.create_search_thread(u8::MAX); }, @@ -127,19 +154,30 @@ impl Grossmeister { } return self.parse_go(tokens) } - self.create_search_thread(6) + + let color = self.board.color(); + let duration = self.board.clock.time[color as usize] / 20 + self.board.clock.increment[color as usize]; + + self.create_terminator_thread(duration); + self.create_search_thread(u8::MAX) + } + + fn create_terminator_thread(&self, duration: Duration) -> JoinHandle<()> { + let should_halt = self.should_halt.clone(); + spawn(move || { + println!("info string terminating search in {:?}", duration); + sleep(duration); + should_halt.store(true, std::sync::atomic::Ordering::Relaxed); + }) } fn create_search_thread(&self, depth: u8) -> JoinHandle<Self> { // Clone current self and move it // into thread to analyze a position let mut gm = self.clone(); - let builder = Builder::new(); - - - builder.spawn(move || { + spawn(move || { gm.iterative_deepening(depth); gm - }).unwrap() + }) } } diff --git a/src/grossmeister/search.rs b/src/grossmeister/search.rs index def4305..f0805cb 100644 --- a/src/grossmeister/search.rs +++ b/src/grossmeister/search.rs @@ -215,6 +215,7 @@ impl Grossmeister { if self.debug { println!("info string aborting"); } + self.should_halt.store(false, std::sync::atomic::Ordering::Relaxed); break; } |