diff options
author | eug-vs <eugene@eug-vs.xyz> | 2024-01-25 11:24:36 +0100 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2024-01-25 11:24:36 +0100 |
commit | 746e3bf17463a377b6c54b291ebef9a736d6ceb7 (patch) | |
tree | a4e965669871084b98d3ce89ac95fa9d50131699 /src/grossmeister/UCI.rs | |
parent | 299c6d6dee96a50f9366955192f922d449d11f20 (diff) | |
download | chessnost-canary.tar.gz |
chore: autoformat codecanary
Use #[rustfmt:skip] to preserve aligned blocks
Diffstat (limited to 'src/grossmeister/UCI.rs')
-rw-r--r-- | src/grossmeister/UCI.rs | 118 |
1 files changed, 76 insertions, 42 deletions
diff --git a/src/grossmeister/UCI.rs b/src/grossmeister/UCI.rs index 0c3c158..e345943 100644 --- a/src/grossmeister/UCI.rs +++ b/src/grossmeister/UCI.rs @@ -1,6 +1,18 @@ -use std::{io::stdin, thread::{JoinHandle, spawn, sleep}, str::Split, time::Duration, sync::{Arc, atomic::{AtomicBool, Ordering}}}; +use std::{ + io::stdin, + str::Split, + sync::{ + atomic::{AtomicBool, Ordering}, + Arc, + }, + thread::{sleep, spawn, JoinHandle}, + time::Duration, +}; -use crate::{board::{Board, io::IO, color::Color}, moves::{Move, MoveKind}}; +use crate::{ + board::{color::Color, io::IO, Board}, + moves::{Move, MoveKind}, +}; use super::Grossmeister; @@ -15,12 +27,16 @@ impl Grossmeister { let mut cmd = String::new(); stdin().read_line(&mut cmd).unwrap(); if !self.parse_command(cmd, &mut search_handle) { - break + break; } } } - pub fn parse_command(&mut self, cmd: String, search_handle: &mut Option<JoinHandle<Self>>) -> bool { + pub fn parse_command( + &mut self, + cmd: String, + search_handle: &mut Option<JoinHandle<Self>>, + ) -> bool { let mut tokens = cmd.trim().split(' '); if let Some(token) = tokens.next() { match token { @@ -44,16 +60,17 @@ impl Grossmeister { "setoption" => { todo!() } - "ucinewgame" => { - *self = Self::default() - } + "ucinewgame" => *self = Self::default(), "position" => { - match tokens.next() { + match tokens.next() { Some("startpos") => { self.board = Board::new(); } Some("fen") => { - let fen = (0..6).filter_map(|_| tokens.next()).collect::<Vec<_>>().join(" "); + let fen = (0..6) + .filter_map(|_| tokens.next()) + .collect::<Vec<_>>() + .join(" "); self.board = Board::from_FEN(fen); } _ => panic!("Expected position"), @@ -62,27 +79,34 @@ impl Grossmeister { for token in tokens.by_ref() { let input_move = Move::from_notation(token.chars()); let moves = self.board.generate_pseudolegal_moves(); - if let Some(mov) = moves - .iter() - .find(|m| { - let promo_matches = match input_move.kind { - MoveKind::Promotion(piece) => match m.kind { - MoveKind::Promotion(another_piece) => piece.without_color() == another_piece.without_color(), - _ => false - }, - _ => true, - }; - promo_matches && m.source == input_move.source && m.target == input_move.target - }) { - self.board.make_move(*mov); - let repeated = self.board.positions.iter().filter(|&&p| p == self.board.hash).count(); - if self.board.hash == 13465997660506371065 { - self.board.print(); - dbg!(mov, repeated, self.board.hash, self.board.ply); + if let Some(mov) = moves.iter().find(|m| { + let promo_matches = match input_move.kind { + MoveKind::Promotion(piece) => match m.kind { + MoveKind::Promotion(another_piece) => { + piece.without_color() == another_piece.without_color() } - } else { - panic!("Illegal move: {}", input_move); - } + _ => false, + }, + _ => true, + }; + promo_matches + && m.source == input_move.source + && m.target == input_move.target + }) { + self.board.make_move(*mov); + let repeated = self + .board + .positions + .iter() + .filter(|&&p| p == self.board.hash) + .count(); + if self.board.hash == 13465997660506371065 { + self.board.print(); + dbg!(mov, repeated, self.board.hash, self.board.ply); + } + } else { + panic!("Illegal move: {}", input_move); + } } } } @@ -90,7 +114,9 @@ impl Grossmeister { // Before we go, let's join to the latest search if let Some(hand) = search_handle.take() { match hand.join() { - Ok(better_self) => self.transposition_table = better_self.transposition_table, + Ok(better_self) => { + self.transposition_table = better_self.transposition_table + } Err(err) => println!("info string error {:?}", err), } } @@ -98,20 +124,25 @@ impl Grossmeister { *search_handle = Some(self.parse_go(tokens, false)); } "stop" => { - self.should_halt.store(true, std::sync::atomic::Ordering::SeqCst); + self.should_halt + .store(true, std::sync::atomic::Ordering::SeqCst); } "ponderhit" => { // Since we were pondering without any scheduled halting previously, // in case of ponderhit we need to schedule a new halting based on clock // TODO: isn't the color flipped here? Might lead to time management bugs let color = self.board.color(); - let duration = (self.board.clock.time[color as usize] + self.board.clock.increment[color as usize]) / 20; + let duration = (self.board.clock.time[color as usize] + + self.board.clock.increment[color as usize]) + / 20; let halt_scheduled = self.schedule_halt(duration); // Join to the current pondering search if let Some(hand) = search_handle.take() { match hand.join() { - Ok(better_self) => self.transposition_table = better_self.transposition_table, + Ok(better_self) => { + self.transposition_table = better_self.transposition_table + } Err(err) => println!("info string error {:?}", err), } halt_scheduled.store(false, Ordering::SeqCst); // Cancel scheduled halting @@ -122,7 +153,7 @@ impl Grossmeister { "quit" => return false, // Non-UCI debug commands "print" => self.board.print(), - _ => {}, + _ => {} } } true @@ -134,7 +165,7 @@ impl Grossmeister { "searchmoves" => todo!(), "ponder" => { println!("info will start in pondering mode"); - return self.parse_go(tokens, true) + return self.parse_go(tokens, true); } "wtime" => { if let Some(time) = tokens.next() { @@ -151,13 +182,15 @@ impl Grossmeister { "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); + 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::Black as usize] = Duration::from_millis(time); + self.board.clock.increment[Color::Black as usize] = + Duration::from_millis(time); } } "movestogo" => {} @@ -166,7 +199,7 @@ impl Grossmeister { let depth: u8 = depth.parse().unwrap(); return self.create_search_thread(depth, Duration::MAX); } - }, + } "nodes" => todo!(), "mate" => todo!(), "movetime" => { @@ -174,12 +207,12 @@ impl Grossmeister { let time: u64 = time.parse().unwrap(); let duration = Duration::from_millis(time); - return self.create_search_thread(u8::MAX, duration) + return self.create_search_thread(u8::MAX, duration); } - }, + } "infinite" => { return self.create_search_thread(u8::MAX, Duration::MAX); - }, + } _ => {} } return self.parse_go(tokens, ponder); @@ -190,7 +223,8 @@ impl Grossmeister { let duration = if ponder { Duration::MAX } else { - (self.board.clock.time[color as usize] + self.board.clock.increment[color as usize]) / 20 + (self.board.clock.time[color as usize] + self.board.clock.increment[color as usize]) + / 20 }; self.create_search_thread(u8::MAX, duration) |