Add auxilary functions

This commit is contained in:
Baptiste Fouques 2023-01-16 17:08:06 +01:00
parent 08a39c258e
commit a65678343a
2 changed files with 53 additions and 27 deletions

View File

@ -32,8 +32,8 @@ use chess::{Game, Board, ChessMove};
/// uci.push_raw("quit\n");
/// ```
pub struct UciEngine<Fi: Read, Fo: Write> {
source: BufReader<Fi>,
destination: Fo,
source: Option<BufReader<Fi>>,
destination: Option<Fo>,
uciok: bool,
id: Id,
initial: Board,
@ -45,9 +45,9 @@ impl<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
/// 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<Fi, Fo> {
pub fn new(source: Option<Fi>, destination: Option<Fo>) -> UciEngine<Fi, Fo> {
UciEngine::<Fi, Fo>{
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<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
/// 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<String> {
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<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
pub fn piece_on(&self, square: Square) -> Option<Piece> {
self.game.current_position().piece_on(square)
}
pub fn color_on(&self, square: Square) -> Option<Color> {
self.game.current_position().color_on(square)
}
}
// LocalWords: uci

View File

@ -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: {}",