From a65678343a38e5b480a5faad3cf2486f0117d235 Mon Sep 17 00:00:00 2001 From: Baptiste Fouques Date: Mon, 16 Jan 2023 17:08:06 +0100 Subject: [PATCH] Add auxilary functions --- chess_uci/src/lib.rs | 78 +++++++++++++++++++++++++------------- chess_uci_demo/src/main.rs | 2 +- 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/chess_uci/src/lib.rs b/chess_uci/src/lib.rs index da288b0..381edeb 100644 --- a/chess_uci/src/lib.rs +++ b/chess_uci/src/lib.rs @@ -32,8 +32,8 @@ use chess::{Game, Board, ChessMove}; /// uci.push_raw("quit\n"); /// ``` pub struct UciEngine { - source: BufReader, - destination: Fo, + source: Option>, + destination: Option, uciok: bool, id: Id, initial: Board, @@ -45,9 +45,9 @@ impl UciEngine { /// Create new game manager /// /// Requires line by line input and output streams to communicate with uci engine - pub fn new(source: Fi, destination: Fo) -> UciEngine { + pub fn new(source: Option, destination: Option) -> UciEngine { UciEngine::{ - source: BufReader::new(source), + source: if let Some(source) = source {Some(BufReader::new(source))} else {None}, destination, id: Id::new(), uciok: false, @@ -148,45 +148,71 @@ impl UciEngine { /// Retrieve a line from the uci engine input stream and parse it pub fn pull(&mut self) { - let mut command = String::new(); - match self.source.read_line(&mut command) { - Ok(0) => self.terminate("Chess engine closed connection."), - Ok(_) => { - info!("← {}", command); - self.exec(&command) - }, - Err(reason) => warn!("Unable to read from engine: {reason}"), + if let Some(source) = &mut self.source { + let mut command = String::new(); + match source.read_line(&mut command) { + Ok(0) => self.terminate("Chess engine closed connection."), + Ok(_) => { + info!("← {}", command); + self.exec(&command) + }, + Err(reason) => warn!("Unable to read from engine: {reason}"), + } + } else { + info!("❌ No connected uci engine"); } } /// Read a line from the uci engine input stream (do not parse) 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}, + if let Some(source) = &mut self.source { + let mut data = String::new(); + match 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}, + } + } else { + info!("❌ No connected uci engine"); + None } } /// Push a Uci Gui command to the engine 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) => warn!("⚠ gui: {command_str} truncated at {n}"), - Err(reason) => warn!("Unable to send command {command_str}: {reason}"), + if let Some(destination) = &mut self.destination { + let command_str = command.to_string(); + match destination.write(&command_str.as_bytes()){ + 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}"), + } + } else { + info!("❌ No connected uci engine"); } } /// Push string (not a game manager integrated command) to the Uci engine output stream 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}"), + if let Some(destination) = &mut self.destination { + match 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}"), + } + } else { + info!("❌ No connected uci engine"); } } } +use chess::{Square, Piece, Color}; +impl UciEngine { + pub fn piece_on(&self, square: Square) -> Option { + self.game.current_position().piece_on(square) + } + pub fn color_on(&self, square: Square) -> Option { + self.game.current_position().color_on(square) + } +} // LocalWords: uci diff --git a/chess_uci_demo/src/main.rs b/chess_uci_demo/src/main.rs index db3cd31..2ee41bb 100644 --- a/chess_uci_demo/src/main.rs +++ b/chess_uci_demo/src/main.rs @@ -22,7 +22,7 @@ pub fn main(){ }; let (sf_in,sf_out) = (process.stdin.expect("Program stdin"), process.stdout.expect("Program stdout")); - let mut uci = UciEngine::new(sf_out, sf_in); + let mut uci = UciEngine::new(Some(sf_out), Some(sf_in)); uci.init(); println!("Engine: {} \nby: {}",