diff options
Diffstat (limited to 'src/grossmeister/UCI.rs')
| -rw-r--r-- | src/grossmeister/UCI.rs | 66 | 
1 files changed, 52 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() +        })      }  }  |