implement moves
This commit is contained in:
parent
44c0a23252
commit
4cac2dc414
19
src/lib.rs
19
src/lib.rs
|
@ -143,12 +143,29 @@ impl<Fi: BufRead, Fo: Write> UciEngine<Fi, Fo> {
|
|||
}
|
||||
}
|
||||
|
||||
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},
|
||||
}
|
||||
}
|
||||
|
||||
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}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
11
src/main.rs
11
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 {}
|
||||
|
||||
}
|
||||
|
|
|
@ -101,10 +101,20 @@ pub fn parse(message: &mut String) -> Result<EngineCommand,&'static str> {
|
|||
}
|
||||
|
||||
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),
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue