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 search_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" => { // Clone current self and move it // into thread to analyze a position let mut gm = self.clone(); let builder = Builder::new(); search_handle = Some(builder.spawn(move || { gm.analyze(board); gm }).unwrap()); }, _ => todo!() } }, "stop" => { self.should_halt.store(true, std::sync::atomic::Ordering::Relaxed); if let Some(hand) = search_handle.take() { // Search returns new SELF that is more powerful // because he evaluated many new positions and // his transposition table is more saturated *self = hand.join().unwrap(); } self.should_halt.store(false, std::sync::atomic::Ordering::Relaxed); } _ => {}, } } } }