From 10a68da1249931a6220ca06affa063c187836bbd Mon Sep 17 00:00:00 2001 From: eug-vs Date: Fri, 24 Feb 2023 17:35:35 +0300 Subject: feat: add initial multi-threaded UCI impl --- src/grossmeister/UCI.rs | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/grossmeister/UCI.rs (limited to 'src/grossmeister/UCI.rs') diff --git a/src/grossmeister/UCI.rs b/src/grossmeister/UCI.rs new file mode 100644 index 0000000..2c09a7b --- /dev/null +++ b/src/grossmeister/UCI.rs @@ -0,0 +1,71 @@ +use std::{io::stdin, thread::Builder}; + +use crate::{board::{Board, io::IO}, moves::Move, player::Player}; + +use super::Grossmeister; + +const NAME: &str = "Chessnost"; +const AUTHOR: &str = "Eugene Sokolov"; + +impl Grossmeister { + pub fn start(&mut self) { + let mut board = Board::new(); + let mut handle = None; + + loop { + let mut cmd = String::new(); + stdin().read_line(&mut cmd).unwrap(); + let tokens: Vec<&str> = cmd.trim().split(' ').collect(); + println!("Command: {:?}, tokens: {:?}", cmd, tokens); + + match tokens[0] { + "uci" => { + println!("id name {}", NAME); + println!("id author {}", AUTHOR); + println!("uciok"); + } + "isready" => { + println!("readyok"); + } + "position" => { + match tokens[1] { + "startpos" => { + board = Board::new(); + } + _ => { + todo!("Parse FEN") + } + } + assert!(tokens[2] == "moves"); + for move_token in tokens.iter().skip(3) { + let mov = Move::from_notation(move_token.chars()); + board.make_move(mov); + println!("{:?}", mov); + board.print(); + } + } + "go" => { + match tokens[1] { + "infinite" => { + let mut gm = self.clone(); + let builder = Builder::new(); + + handle = Some(builder.spawn(move || { + gm.analyze(board); + }).unwrap()); + }, + _ => todo!() + } + }, + "stop" => { + self.should_halt.store(true, std::sync::atomic::Ordering::Relaxed); + if let Some(hand) = handle.take() { + hand.join().unwrap(); + } + self.should_halt.store(false, std::sync::atomic::Ordering::Relaxed); + } + _ => {}, + } + } + } +} -- cgit v1.2.3