From 688a401a3d85fd8a1e4f9818759c9ef46e9bed3c Mon Sep 17 00:00:00 2001 From: stefiosif Date: Sun, 4 Aug 2024 11:28:08 +0300 Subject: [PATCH] Merge UciGame and SerachParameters into UciParameters --- src/uci.rs | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/src/uci.rs b/src/uci.rs index f11042c..408dc6d 100644 --- a/src/uci.rs +++ b/src/uci.rs @@ -57,16 +57,18 @@ impl fmt::Display for Response { } } -struct SearchParameters { +struct UciParameters { movetime: Option, depth: Option, + game: Option, } -impl SearchParameters { +impl UciParameters { const fn new() -> Self { Self { movetime: None, depth: None, + game: None, } } @@ -77,9 +79,13 @@ impl SearchParameters { fn add_depth(&mut self, depth: u8) { self.depth = Some(depth); } + + fn add_game(&mut self, game: Game) { + self.game = Some(game); + } } -impl Default for SearchParameters { +impl Default for UciParameters { fn default() -> Self { Self::new() } @@ -104,7 +110,7 @@ pub fn uci_position(mut position: SplitWhitespace) -> Result { const MAX_DEPTH: u8 = 5; pub fn uci_go(mut go: SplitWhitespace, game: &mut Game) -> Result { - let mut params = SearchParameters::new(); + let mut params = UciParameters::new(); while let Some(subcommand) = go.next() { match subcommand { "depth" => params.add_depth(go.next().unwrap().parse::().ok().unwrap()), @@ -117,32 +123,12 @@ pub fn uci_go(mut go: SplitWhitespace, game: &mut Game) -> Result Ok(Move::new(0, 0)) } -struct UciGame { - game: Option, -} - -impl UciGame { - const fn new() -> Self { - Self { game: None } - } - - fn set_game_state(&mut self, game: Game) { - self.game = Some(game); - } -} - -impl Default for UciGame { - fn default() -> Self { - Self::new() - } -} - pub fn uci_loop() -> Result<(), String> { let stdin = io::stdin(); let stdout = io::stdout(); let handle_in = stdin.lock(); let mut handle_out = stdout.lock(); - let mut uci_game = UciGame::new(); + let mut params = UciParameters::new(); for line in handle_in.lines() { let line_str = line.unwrap_or_else(|_| "quit".to_string()); @@ -152,11 +138,11 @@ pub fn uci_loop() -> Result<(), String> { Command::Uci => Response::UciOk, Command::IsReady => Response::ReadyOk, Command::Position => { - uci_game.set_game_state(uci_position(parts)?); + params.add_game(uci_position(parts)?); Response::Info("Initialized position.".to_string()) } Command::Go => { - if let Some(ref mut game) = uci_game.game { + if let Some(ref mut game) = params.game { let best_move = uci_go(parts, game)?; Response::BestMove(best_move.parse_into_str()) } else {