Add auxilary functions
This commit is contained in:
parent
08a39c258e
commit
a65678343a
|
@ -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,8 +148,9 @@ 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) {
|
||||
|
||||
if let Some(source) = &mut self.source {
|
||||
let mut command = String::new();
|
||||
match self.source.read_line(&mut command) {
|
||||
match source.read_line(&mut command) {
|
||||
Ok(0) => self.terminate("Chess engine closed connection."),
|
||||
Ok(_) => {
|
||||
info!("← {}", command);
|
||||
|
@ -157,36 +158,61 @@ impl<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
|
|||
},
|
||||
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> {
|
||||
if let Some(source) = &mut self.source {
|
||||
let mut data = String::new();
|
||||
match self.source.read_line(&mut data) {
|
||||
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) {
|
||||
if let Some(destination) = &mut self.destination {
|
||||
let command_str = command.to_string();
|
||||
match self.destination.write(&command_str.as_bytes()){
|
||||
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()) {
|
||||
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
|
||||
|
|
|
@ -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: {}",
|
||||
|
|
Loading…
Reference in New Issue