1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
#[macro_use]
extern crate log;
use std::process::{Child, Command, Stdio};
use std::io::Write;
use std::io::{self, Read};
use std::fmt;
use std::thread;
use std::time::Duration;
use std::cell::RefCell;
pub struct Engine {
engine: RefCell<Child>,
movetime: u32,
}
const DEFAULT_TIME: u32 = 100;
impl Engine {
/// Create a new [`Engine`] instance.
///
/// # Arguments
///
/// * `path` - The path to the engine executable.
///
/// # Panics
///
/// * Panics if the engine couldn't be spawned (path is invalid, execution permission denied, etc.)
///
/// # Errors
///
/// Returns an `EngineError` if there's an errors while communicating with the engine.
///
/// [`Engine`]: struct.Engine.html
pub fn new(path: &str) -> Result<Engine> {
let cmd = Command::new(path)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.expect("Unable to run engine");
let res = Engine {
engine: RefCell::new(cmd),
movetime: DEFAULT_TIME,
};
res.read_line()?;
res.command("uci")?;
Ok(res)
}
/// Changes the amount of time the engine spends looking for a move
///
/// # Arguments
///
/// * `new_movetime` - New timelimit in milliseconds
#[must_use]
pub fn movetime(mut self, new_movetime: u32) -> Engine {
self.movetime = new_movetime;
self
}
/// Asks the engine to play the given moves from the initial position on it's internal board.
///
/// # Arguments
///
/// * `moves` - A list of moves for the engine to play. Uses Coordinate notation
///
/// # Errors
///
/// Returns `EngineError` if there's an error while communicating with the engine.
///
/// # Examples
///
/// ```
/// let engine = uci::Engine::new("stockfish").unwrap();
/// let moves = vec!["e2e4".to_string(), "e7e5".to_string()];
/// engine.make_moves(&moves).unwrap();
/// ```
pub fn make_moves(&self, moves: &[String]) -> Result<()> {
self.write_fmt(format_args!(
"position startpos moves {}\n",
moves.join(" ")
))
}
/// Asks the engine to use the position represented by the given FEN string
///
/// # Errors
///
///
///
/// # Examples
///
/// ```
/// let engine = uci::Engine::new("stockfish").unwrap();
/// engine.set_position("2k4R/8/3K4/8/8/8/8/8 b - - 0 1").unwrap();
/// assert_eq!(engine.bestmove().unwrap(), "c8b7");
/// ```
pub fn set_position(&self, fen: &str) -> Result<()> {
let moves: Vec<String> = vec![];
self.make_moves_from_position(fen, &moves)
}
/// Asks the engine to use the position represented by the given FEN string
/// and then play the given moves from that position
///
/// # Errors
///
/// Returns an `EngineError` if there's an error while communicating with the engine.
pub fn make_moves_from_position(&self, fen: &str, moves: &[String]) -> Result<()> {
self.write_fmt(format_args!(
"position fen {} moves {}\n",
fen,
moves.join(" ")
))
}
/// Returns the best move in the current position according to the engine
/// # Errors
/// Returns an error if the engine is not ready to return a move
pub fn bestmove(&self) -> Result<String> {
self.write_fmt(format_args!("go movetime {}\n", self.movetime))?;
loop {
let s = self.read_line()?;
debug!("{}", s);
if s.starts_with("bestmove") {
return Ok(s.split(' ').collect::<Vec<&str>>()[1].trim().to_string());
}
}
}
/// Sets an engine specific option to the given value
///
/// # Arguments
///
/// * `name` - Name of the option
/// * `value` - New value for the option
///
/// # Errors
///
/// Returns an `EngineError` if the engine doesn't support the option
///
/// # Examples
///
/// ```
/// let engine = uci::Engine::new("stockfish").unwrap();
/// engine.set_option("Skill Level", "5").unwrap();
/// ```
pub fn set_option(&self, name: &str, value: &str) -> Result<()> {
self.write_fmt(format_args!("setoption name {name} value {value}\n"))?;
let error_msg = self.read_left_output()?;
if error_msg.trim().is_empty() {
Ok(())
} else {
Err(EngineError::UnknownOption(name.to_string()))
}
}
/// Sends a command to the engine and returns the output
///
/// # Errors
///
/// Returns an `EngineError` if there was an error while sending the command to the engine
///
/// # Examples
///
/// ```
/// let engine = uci::Engine::new("stockfish").unwrap();
/// let analysis = engine.command("go depth 10").unwrap();
/// println!("{}", analysis);
/// ```
pub fn command(&self, cmd: &str) -> Result<String> {
self.write_fmt(format_args!("{}\n", cmd.trim()))?;
thread::sleep(Duration::from_millis(100));
self.read_left_output()
}
fn read_left_output(&self) -> Result<String> {
let mut s: Vec<String> = vec![];
self.write_fmt(format_args!("isready\n"))?;
loop {
let next_line = self.read_line()?;
match next_line.trim() {
"readyok" => return Ok(s.join("\n")),
other => s.push(other.to_string()),
}
}
}
fn write_fmt(&self, args: fmt::Arguments) -> Result<()> {
info!("Command: {:?}", fmt::format(args));
self.engine
.borrow_mut()
.stdin
.as_mut()
.unwrap()
.write_fmt(args)?;
Ok(())
}
fn read_line(&self) -> Result<String> {
let mut s = String::new();
let mut buf: Vec<u8> = vec![0];
loop {
let _ = self
.engine
.borrow_mut()
.stdout
.as_mut()
.unwrap()
.read(&mut buf)?;
s.push(buf[0] as char);
if buf[0] == b'\n' {
break;
}
}
Ok(s)
}
}
/// The error type for any errors encountered with the engine.
#[derive(Debug)]
pub enum EngineError {
/// Wrapper around any io errors encountered while trying to communicate with the engine.
Io(io::Error),
/// Engine doesn't recognize the specified option.
UnknownOption(String),
}
use self::EngineError::{Io, UnknownOption};
impl fmt::Display for EngineError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Io(ref err) => write!(f, "IO error: {err}"),
UnknownOption(ref option) => write!(f, "No such option: '{option}'"),
}
}
}
impl From<io::Error> for EngineError {
fn from(err: io::Error) -> EngineError {
Io(err)
}
}
/// A Result type which uses [`EngineError`] for representing errors.
///
/// [`EngineError`]: enum.EngineError.html
pub type Result<T> = std::result::Result<T, EngineError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let engine = Engine::new("stockfish").unwrap().movetime(200);
engine.set_option("Skill Level", "15").unwrap();
let t = engine.bestmove().unwrap();
println!("{t}");
}
}