From 4cac2dc414890eb03fa1fa3f181bb5e7bb3612de Mon Sep 17 00:00:00 2001 From: Baptiste Fouques Date: Thu, 12 Jan 2023 17:27:17 +0100 Subject: [PATCH] implement moves --- src/lib.rs | 19 ++++++++++++++++++- src/main.rs | 11 +++++++++++ src/uci_command.rs | 10 ++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index ee621f1..eefd09d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -143,12 +143,29 @@ impl UciEngine { } } + pub fn pull_raw(&mut self) -> Option { + let mut data = String::new(); + match self.source.read_line(&mut data) { + Ok(0) => {self.terminate("Chess engine closed connection."); None}, + Ok(_) => {info!("↜ {}", data); Some(data.clone())}, + Err(reason) => {warn!("Unable to read from engine: {reason}"); None}, + } + } + pub fn push(&mut self, command: GuiCommand) { let command_str = command.to_string(); match self.destination.write(&command_str.as_bytes()){ - Ok(n) if n == command_str.len() => info!("→ gui: {command_str} "), + Ok(n) if n == command_str.len() => info!("→ gui: {command_str}"), Ok(n) => warn!("⚠ gui: {command_str} truncated at {n}"), Err(reason) => warn!("Unable to send command {command_str}: {reason}"), } } + + pub fn push_raw(&mut self, data: &str){ + match self.destination.write(data.as_bytes()) { + Ok(n) if n == data.len() => info!("↝ raw: {data}"), + Ok(n) => warn!("⚠ raw: {data} truncated at {n}"), + Err(reason) => warn!("Unable to send raw {data}: {reason}"), + } + } } diff --git a/src/main.rs b/src/main.rs index d48d2b6..1e3a8aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,11 @@ extern crate chess_uci; +use chess::ChessMove; use chess_uci::*; use std::process::{Command, Stdio}; use std::io::BufReader; +use std::str::FromStr; pub fn main(){ env_logger::init(); @@ -26,4 +28,13 @@ pub fn main(){ println!("Engine: {} \nby: {}", if let Some(name) = uci.name() {name} else {"Not defined".to_string()}, if let Some(author) = uci.author() {author} else {"Not defined".to_string()}); + + uci.new_game(); + uci.make_move(ChessMove::from_str("e2e4").expect("error converting e2e4")); + + uci.push_raw("d\n"); + + uci.push_raw("quit\n"); + while uci.pull_raw() != None {} + } diff --git a/src/uci_command.rs b/src/uci_command.rs index 4c7e20c..9e76bf1 100644 --- a/src/uci_command.rs +++ b/src/uci_command.rs @@ -101,10 +101,20 @@ pub fn parse(message: &mut String) -> Result { } impl fmt::Display for GuiCommand { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result{ match self { GuiCommand::Uci => write!(f, "uci\n"), GuiCommand::UciNewGame => write!(f, "ucinewgame\n"), + GuiCommand::Position { position, moves } => { + write!(f, "position {} moves {}\n", + match position { None => "startpos".to_string(), + Some(board) if *board == Board::default() => "startpos".to_string(), + Some(board) =>"fen ".to_string() + &board.to_string()}, + join(moves.into_iter().map(|x| if let Action::MakeMove(chessmove) = x + {chessmove.to_string()} else {"".to_string()}), "") + ) + } a => unimplemented!("{:?}", a), }