An implementation of the Universal Chess Interface in Rust.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.7 KiB

//! UCI is a simple library to allow people to ignore the lower-level protocol needed to create chess engines.
7 years ago
//! The engine is fully generic. By specifying a valid reader and writing you can send the messages to STDIN
//! and STDOUT, per the standard - or to memory for testing.
mod commands;
7 years ago
use std::io::{BufRead, Write};
7 years ago
#[derive(Debug)]
7 years ago
pub struct Engine<'a, R, W> {
pub name: &'a str,
pub author: &'a str,
7 years ago
pub reader: R,
pub writer: W,
}
/// Notes to delete later:
///
7 years ago
///
/// A way to make this comfortable for users to use is this provides easy access to commands as well as a
/// loop and stuff they can use. This way they don't have to override anything - they just include this
/// in their code for their engine to make the communication portion easy.
///
/// Think of this in the manner of a game engine. They don't provide the loop and everything, but they do
/// provide convenience functions and stuff to make _building_ that loop easier.
///
/// In other words, the engine writer still needs to know how the standard works. They just don't need to
/// implement all the nasty details.
/// For example, a start up function calls the correct "boot up" code for the engine so they only have to
/// worry about calling "boot up" prior to their main engine loop.
///
/// Additionally this code should have a parser. You call it with the reader supplied and it waits for a command
/// and parses it into a tuple of some kind for the user.
7 years ago
impl<'a, R, W> Engine<'a, R, W>
where
R: BufRead,
W: Write,
{
7 years ago
pub fn new(name: &'a str, author: &'a str, reader: R, writer: W) -> Engine<'a, R, W> {
7 years ago
// TODO: This should also take an EngineOptions thing that indicates which options are supported
// so that when called it can send them easily.
7 years ago
// Engine options are all optional and appeared to be fixed by the UCI standard.
7 years ago
Engine {
name: name,
author: author,
7 years ago
reader: reader,
writer: writer,
}
}
7 years ago
/// Sends identification messages to the writer for the GUI to pick up
7 years ago
pub fn identify(&mut self) {
7 years ago
let name_id: String = format!("{} {} {}\n", commands::ID, commands::NAME, self.name);
let author_id: String = format!("{} {} {}\n", commands::ID, commands::AUTHOR, self.author);
7 years ago
// For these two writes we can panic - there's no possibility of recovery if the engine fails at this stage
write!(&mut self.writer, "{}", name_id).expect("failed to send name identification to writer");
write!(&mut self.writer, "{}", author_id).expect("failed to send author identification to writer");
}
/// Sends all available options in the options configuration and a final UCIOK meaning we are reading to go.
pub fn send_available_options(&mut self) {
// NOTE: Options can be represented as a hashmap with keys that are constant strings in the options.rs
// file and values are the associated options.
// This function will construct the proper string from the objects.
// How can you make this as easy as possible for the user...give it some thought. The value has to be
// some sort of object that can store various optiobns that change on the option.
// Since options are fixed it would be nice to type check the key values. Maybe use a trait or something
// that is actually a wrapped string, but typecheckable so not just any string can be put in.
// TODO: This needs to be tested majorly
// Again this command must complete before we can say the engine is connected, so panicking at this stage is ok
write!(&mut self.writer, "{}", commands::UCIOK).expect("failed to send `uciok` command");
}
7 years ago
pub fn ready(&mut self) {
// TODO: TESTS TO MAKE SURE IT SENDS READYOK
}
}