From 4dd643f66baf9fef5c7cd661291c0ee548d23b07 Mon Sep 17 00:00:00 2001 From: Baptiste Fouques Date: Mon, 20 Feb 2023 17:25:24 +0100 Subject: [PATCH] add command --- chess_uci/src/lib.rs | 40 +++++++++++++++++++++--------------- chess_uci/src/uci_command.rs | 4 ++-- chess_uci_demo/src/main.rs | 4 ++-- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/chess_uci/src/lib.rs b/chess_uci/src/lib.rs index f49963b..f36acfd 100644 --- a/chess_uci/src/lib.rs +++ b/chess_uci/src/lib.rs @@ -153,8 +153,8 @@ impl UciEngine { /// /// uci.make_move(ChessMove::from_str("e2e4").expect("error converting e2e4")); /// ``` - pub fn make_move(&mut self, chess_move: ChessMove) -> bool { - if self.game.make_move(chess_move) { + pub fn make_move(&mut self, chess_move: &ChessMove) -> bool { + if self.game.make_move(*chess_move) { self.push(GuiCommand::Position { position: Some(self.initial), moves: self.game.actions().to_vec(), @@ -181,7 +181,7 @@ impl UciEngine { self.new_game(); for action in actions { if let Action::MakeMove(chessmove) = action { - self.make_move(chessmove); + self.make_move(&chessmove); } } @@ -224,23 +224,29 @@ impl UciEngine { pub fn exec(&mut self, command: &str) -> Result { let uci_command = parse(&mut command.to_string()); match uci_command.clone() { - Ok(EngineCommand::Id { id }) => self.update(&id), - Ok(EngineCommand::UciOk) => self.uciok(), - Ok(EngineCommand::Opt { options: _ }) => {} - Ok(EngineCommand::Info { infos: _ }) - | Ok(EngineCommand::BestMove { - best_move: _, - ponder: _, - }) - | Ok(EngineCommand::ReadyOk) => {} - Ok(EngineCommand::Registration) | Ok(EngineCommand::CopyProtection) => { - unimplemented!("command not implemented") - } + Ok(command) => self.exec_command(&command), Err(_) => warn!("Not a command"), }; uci_command } + pub fn exec_command(&mut self, command: &EngineCommand) { + match command { + EngineCommand::Id { id } => self.update(&id), + EngineCommand::UciOk => self.uciok(), + EngineCommand::Opt { options: _ } => {} + EngineCommand::Info { infos: _ } + | EngineCommand::BestMove { + best_move: _, + ponder: _, + } + | EngineCommand::ReadyOk => {} + EngineCommand::Registration | EngineCommand::CopyProtection => { + unimplemented!("command not implemented") + } + } + } + /// Retrieve a line from the uci engine input stream and parse it pub fn pull(&mut self) -> Option { if let Some(source) = &mut self.source { @@ -327,11 +333,11 @@ impl UciEngine { self.uciok } - pub fn human_play(&mut self, chess_move: ChessMove) -> Result { + pub fn human_play(&mut self, chess_move: &ChessMove) -> Result { match self.player[self.side_to_move().to_index()] { Player::Human { .. } => { if self.make_move(chess_move) { - Ok(chess_move) + Ok(*chess_move) } else { Err("Invalid move for human player") } diff --git a/chess_uci/src/uci_command.rs b/chess_uci/src/uci_command.rs index 79762b8..a999bdd 100644 --- a/chess_uci/src/uci_command.rs +++ b/chess_uci/src/uci_command.rs @@ -4,7 +4,7 @@ use std::{str::FromStr, time::Duration}; use chess::{Action, Board, ChessMove}; -#[derive(Clone, Default)] +#[derive(Clone, Default, Debug)] pub struct Id { name: Option, author: Option, @@ -63,7 +63,7 @@ impl fmt::Display for Opt { } } -#[derive(Clone)] +#[derive(Clone, Debug)] pub enum EngineCommand { Id { id: Id, diff --git a/chess_uci_demo/src/main.rs b/chess_uci_demo/src/main.rs index 4b5a8c8..1c25436 100644 --- a/chess_uci_demo/src/main.rs +++ b/chess_uci_demo/src/main.rs @@ -58,7 +58,7 @@ pub fn main() { uci.new_game(); uci.push_raw("d\n"); - uci.human_play(ChessMove::from_str("e2e4").expect("error converting e2e4")) + uci.human_play(&ChessMove::from_str("e2e4").expect("error converting e2e4")) .expect("can not make human move"); uci.push_raw("d\n"); @@ -68,7 +68,7 @@ pub fn main() { loop { match uci.pull() { Some(EngineCommand::BestMove { best_move, .. }) => { - uci.make_move(best_move); + uci.make_move(&best_move); break; } Some(EngineCommand::Info { .. }) => continue,