chess_uci/chess_uci_demo/src/main.rs

84 lines
2.0 KiB
Rust

extern crate chess;
extern crate chess_uci;
use chess::{ChessMove, Color};
use chess_uci::uci_command::EngineCommand;
use chess_uci::*;
use std::process::{Command, Stdio};
use std::str::FromStr;
pub fn main() {
env_logger::init();
println!("Launching hardcoded stockfish program (should be in PATH)");
// launch chess program
let process = match Command::new("stockfish")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
{
Err(why) => panic!("couldn't spawn stockfish: {}", why),
Ok(process) => process,
};
let (sf_in, sf_out) = (
process.stdin.expect("Program stdin"),
process.stdout.expect("Program stdout"),
);
let mut uci = UciEngine::new(Some(sf_out), Some(sf_in));
uci.game_option(GameOption::Player {
color: Color::White,
value: Player::Human { elo: None },
});
uci.game_option(GameOption::Player {
color: Color::Black,
value: Player::Engine { elo: Some(1500) },
});
uci.init();
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.push(uci_command::GuiCommand::SetOption {
option: uci_command::Opt::SlowMover { value: 11 },
});
uci.new_game();
uci.push_raw("d\n");
uci.human_play(&ChessMove::from_str("e2e4").expect("error converting e2e4"))
.expect("can not make human move");
uci.push_raw("d\n");
uci.go();
loop {
match uci.pull() {
Some(EngineCommand::BestMove { best_move: Some(best_move), .. }) => {
uci.make_move(&best_move);
break;
}
Some(EngineCommand::Info { .. }) => continue,
_ => continue,
}
}
uci.push_raw("d\n");
uci.push_raw("quit\n");
while uci.pull_raw().is_some() {}
}