chess_uci/chess_uci/src/lib.rs

402 lines
12 KiB
Rust
Raw Normal View History

#![allow(dead_code)]
/// Manage UCI messages and link into chess component
2023-01-24 10:57:33 +01:00
pub mod uci_command;
2023-01-30 15:56:01 +01:00
use crate::uci_command::*;
2023-01-11 17:18:15 +01:00
use log::{info, warn};
2023-01-11 12:07:28 +01:00
use std::io::*;
2023-01-27 14:59:13 +01:00
use std::result::Result;
2023-01-30 15:56:01 +01:00
use std::time::Duration;
2023-02-03 13:55:31 +01:00
use chess::{Board, ChessMove, Game, Action};
2023-01-11 18:20:30 +01:00
2023-01-30 15:56:01 +01:00
const DEFAULT_TIME: (Option<Duration>, Option<Duration>) =
(Some(Duration::from_secs(120 * 60)), None);
2023-01-27 14:59:13 +01:00
2023-01-13 11:54:28 +01:00
/// Structure used to manage a chess game with a uci engine.
///
/// It needs a in / out communication channel to read / push line by line uci commands
///c
/// ```rust
/// use chess_uci::UciEngine;
/// use std::process::{Command, Stdio};
/// use std::io::BufReader;
///
/// 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(BufReader::new(sf_out), sf_in);
/// // .. uci.init(); ..
/// uci.push_raw("quit\n");
/// ```
pub struct UciEngine<Fi: Read, Fo: Write> {
2023-01-16 17:08:06 +01:00
source: Option<BufReader<Fi>>,
destination: Option<Fo>,
2023-01-11 12:07:28 +01:00
uciok: bool,
id: Id,
2023-01-11 18:20:30 +01:00
initial: Board,
game: Game,
2023-01-26 17:21:00 +01:00
/// white (total, incr), black (total, incr)
2023-01-27 14:59:13 +01:00
time: [(Option<Duration>, Option<Duration>); 2],
2023-01-26 17:21:00 +01:00
/// white, black
2023-01-27 14:59:13 +01:00
player: [Player; 2],
2023-01-26 17:21:00 +01:00
}
pub enum UciOption {
2023-01-30 15:56:01 +01:00
Ponder { value: bool },
UCIElo { value: Option<u32> },
2023-01-26 17:21:00 +01:00
}
2023-01-31 13:37:49 +01:00
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
2023-01-30 15:56:01 +01:00
pub enum Player {
Human { elo: Option<u32> },
2023-01-31 13:37:49 +01:00
Engine { elo: Option<u32> },
2023-01-30 15:56:01 +01:00
}
2023-01-26 17:21:00 +01:00
pub enum GameOption {
2023-02-03 13:55:31 +01:00
TotalTime { color: Color, value: Option<Duration> },
Increment { color: Color, value: Option<Duration> },
Player {color: Color, value: Player},
2023-01-11 12:07:28 +01:00
}
2023-01-30 15:56:01 +01:00
impl<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
2023-01-13 11:54:28 +01:00
/// Create new game manager
///
/// Requires line by line input and output streams to communicate with uci engine
2023-01-16 17:08:06 +01:00
pub fn new(source: Option<Fi>, destination: Option<Fo>) -> UciEngine<Fi, Fo> {
2023-01-30 15:56:01 +01:00
UciEngine::<Fi, Fo> {
2023-01-30 13:24:22 +01:00
source: source.map(|x| BufReader::new(x)),
destination,
2023-01-30 13:24:22 +01:00
id: Id::default(),
2023-01-11 18:20:30 +01:00
uciok: false,
initial: Board::default(),
2023-01-26 17:21:00 +01:00
game: Game::new(),
2023-01-27 14:59:13 +01:00
time: [DEFAULT_TIME; 2],
2023-01-30 15:56:01 +01:00
player: [Player::Human { elo: None }; 2],
2023-01-11 18:20:30 +01:00
}
2023-01-11 12:07:28 +01:00
}
2023-01-26 17:21:00 +01:00
pub fn game_option(&mut self, option: GameOption) -> GameOption {
let old_value: GameOption;
match option {
2023-02-03 13:55:31 +01:00
GameOption::TotalTime { color, value } => {
old_value = GameOption::TotalTime {
color,
value: self.time[color.to_index()].0,
2023-01-30 15:56:01 +01:00
};
2023-02-03 13:55:31 +01:00
self.time[color.to_index()].0 = value
2023-01-30 15:56:01 +01:00
}
2023-02-03 13:55:31 +01:00
GameOption::Increment { color, value } => {
old_value = GameOption::Increment {
color,
value: self.time[color.to_index()].1,
2023-01-30 15:56:01 +01:00
};
2023-02-03 13:55:31 +01:00
self.time[color.to_index()].1 = value
2023-01-30 15:56:01 +01:00
}
2023-02-03 13:55:31 +01:00
GameOption::Player { color, value } => {
old_value = GameOption::Player {
color,
value: self.player[color.to_index()],
2023-01-30 15:56:01 +01:00
};
2023-02-03 13:55:31 +01:00
self.player[color.to_index()] = value
2023-01-30 15:56:01 +01:00
}
2023-01-26 17:21:00 +01:00
}
old_value
}
2023-01-31 13:37:49 +01:00
pub fn get_player(&self, color: Color) -> Player {
self.player[color.to_index()]
}
2023-01-13 11:54:28 +01:00
/// Launch uci engine initialisation
///
/// Retrieve data from uci engine (until uciok command from engine)
2023-01-30 15:56:01 +01:00
pub fn init(&mut self) {
2023-01-11 17:18:15 +01:00
self.push(GuiCommand::Uci);
// Consume commands until uciok messages
2023-01-24 10:57:33 +01:00
if self.source.is_some() {
while !self.uciok {
self.pull();
}
2023-01-11 17:18:15 +01:00
}
}
2023-01-13 11:54:28 +01:00
/// Initialize a new game
///
/// Set a new board internally and tell the engine
2023-01-11 17:18:15 +01:00
pub fn new_game(&mut self) {
2023-01-11 18:20:30 +01:00
self.initial = Board::default();
self.game = Game::new_with_board(self.initial);
2023-01-31 13:37:49 +01:00
self.push(GuiCommand::Stop);
2023-01-11 18:20:30 +01:00
self.push(GuiCommand::UciNewGame);
}
2023-01-13 11:54:28 +01:00
/// Play a move
///
/// Update internal game reference and tell the uci engine
///
/// ```rust
/// use chess::ChessMove;
/// use std::str::FromStr;
/// use chess_uci::*;
/// use std::io;
///
/// let mut uci = UciEngine::new(io::empty(), io::sink());
///
/// uci.make_move(ChessMove::from_str("e2e4").expect("error converting e2e4"));
/// ```
2023-02-20 17:25:24 +01:00
pub fn make_move(&mut self, chess_move: &ChessMove) -> bool {
if self.game.make_move(*chess_move) {
2023-01-30 15:56:01 +01:00
self.push(GuiCommand::Position {
position: Some(self.initial),
moves: self.game.actions().to_vec(),
});
2023-01-19 17:01:34 +01:00
true
} else {
false
}
2023-01-11 17:18:15 +01:00
}
2023-02-03 13:55:31 +01:00
pub fn back_move(&mut self) -> Option<ChessMove> {
let mut actions = self.game.actions().clone();
let mut last_move = None;
loop{
match actions.pop() {
Some(Action::MakeMove(last)) => {
last_move = Some(last);
break;
},
None => break,
_ => continue,
}
}
self.new_game();
for action in actions {
if let Action::MakeMove(chessmove) = action {
2023-02-20 17:25:24 +01:00
self.make_move(&chessmove);
2023-02-03 13:55:31 +01:00
}
}
last_move
}
2023-01-13 11:54:28 +01:00
fn terminate(&mut self, reason: &str) {
2023-01-11 17:18:15 +01:00
self.uciok = false;
info!("UCI termination: {}", reason);
}
2023-01-13 11:54:28 +01:00
/// Provides the name of the uci engine
2023-01-11 12:07:28 +01:00
pub fn name(&self) -> Option<String> {
self.id.name()
}
2023-01-13 11:54:28 +01:00
/// Provides the author sof the uci engine
2023-01-11 12:07:28 +01:00
pub fn author(&self) -> Option<String> {
self.id.author()
}
2023-01-30 15:56:01 +01:00
fn update(&mut self, id: &Id) {
if self.is_uciok() {
warn!("Engine info should not be updated now (uciok)");
}
2023-01-11 12:07:28 +01:00
self.id.update(id);
}
2023-01-11 17:18:15 +01:00
2023-01-30 15:56:01 +01:00
fn update_from_str(&mut self, name: Option<&str>, author: Option<&str>) {
if self.is_uciok() {
warn!("Engine info should not be updated now (uciok)");
}
2023-01-11 12:07:28 +01:00
self.id.update_from_str(name, author);
}
2023-01-13 11:54:28 +01:00
/// Execute a uci command
///
2023-01-13 11:54:28 +01:00
/// if `EngineCommand::Id`: update name or authorship of the engine
/// if `EngineCommand::UciOk`: end engine initialization phase
2023-01-27 14:59:13 +01:00
pub fn exec(&mut self, command: &str) -> Result<EngineCommand, &'static str> {
2023-01-30 15:56:01 +01:00
let uci_command = parse(&mut command.to_string());
match uci_command.clone() {
2023-02-20 17:25:24 +01:00
Ok(command) => self.exec_command(&command),
2023-01-11 12:07:28 +01:00
Err(_) => warn!("Not a command"),
2023-01-20 17:17:58 +01:00
};
uci_command
}
2023-02-20 17:25:24 +01:00
pub fn exec_command(&mut self, command: &EngineCommand) {
match command {
EngineCommand::Id { id } => self.update(&id),
EngineCommand::UciOk => self.uciok(),
EngineCommand::Opt { options: _ } => {}
EngineCommand::Info { infos: _ }
| EngineCommand::BestMove {
best_move: _,
ponder: _,
}
| EngineCommand::ReadyOk => {}
EngineCommand::Registration | EngineCommand::CopyProtection => {
unimplemented!("command not implemented")
}
}
}
2023-01-13 11:54:28 +01:00
/// Retrieve a line from the uci engine input stream and parse it
2023-01-20 17:17:58 +01:00
pub fn pull(&mut self) -> Option<EngineCommand> {
2023-01-16 17:08:06 +01:00
if let Some(source) = &mut self.source {
let mut command = String::new();
match source.read_line(&mut command) {
2023-01-30 15:56:01 +01:00
Ok(0) => {
self.terminate("Chess engine closed connection.");
None
}
2023-01-16 17:08:06 +01:00
Ok(_) => {
info!("← {}", command);
2023-01-20 17:17:58 +01:00
self.exec(&command).ok()
2023-01-30 15:56:01 +01:00
}
Err(reason) => {
warn!("Unable to read from engine: {reason}");
None
}
2023-01-16 17:08:06 +01:00
}
} else {
info!("❌ No connected uci engine");
2023-01-20 17:17:58 +01:00
None
2023-01-11 17:18:15 +01:00
}
}
2023-01-13 11:54:28 +01:00
/// Read a line from the uci engine input stream (do not parse)
2023-01-12 17:27:17 +01:00
pub fn pull_raw(&mut self) -> Option<String> {
2023-01-16 17:08:06 +01:00
if let Some(source) = &mut self.source {
let mut data = String::new();
match source.read_line(&mut data) {
2023-01-30 15:56:01 +01:00
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
}
2023-01-16 17:08:06 +01:00
}
} else {
info!("❌ No connected uci engine");
None
2023-01-12 17:27:17 +01:00
}
}
2023-01-13 11:54:28 +01:00
/// Push a Uci Gui command to the engine
2023-01-30 13:24:22 +01:00
pub fn push(&mut self, command: GuiCommand) {
2023-01-16 17:08:06 +01:00
if let Some(destination) = &mut self.destination {
let command_str = command.to_string();
2023-01-30 15:56:01 +01:00
match destination.write(command_str.as_bytes()) {
2023-01-16 17:08:06 +01:00
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");
2023-01-11 17:18:15 +01:00
}
}
2023-01-12 17:27:17 +01:00
2023-01-13 11:54:28 +01:00
/// Push string (not a game manager integrated command) to the Uci engine output stream
2023-01-30 15:56:01 +01:00
pub fn push_raw(&mut self, data: &str) {
2023-01-16 17:08:06 +01:00
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");
2023-01-12 17:27:17 +01:00
}
}
}
2023-01-13 11:54:28 +01:00
2023-01-30 15:56:01 +01:00
impl<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
fn uciok(&mut self) {
2023-01-27 14:59:13 +01:00
self.uciok = true;
}
/// Tell whether the uci engine has initialized
pub fn is_uciok(&self) -> bool {
self.uciok
}
2023-02-20 17:25:24 +01:00
pub fn human_play(&mut self, chess_move: &ChessMove) -> Result<ChessMove, &'static str> {
2023-01-27 14:59:13 +01:00
match self.player[self.side_to_move().to_index()] {
2023-01-30 15:56:01 +01:00
Player::Human { .. } => {
2023-01-27 14:59:13 +01:00
if self.make_move(chess_move) {
2023-02-20 17:25:24 +01:00
Ok(*chess_move)
2023-01-27 14:59:13 +01:00
} else {
Err("Invalid move for human player")
}
2023-01-30 15:56:01 +01:00
}
2023-01-31 13:37:49 +01:00
Player::Engine { .. } => Err("Not a human to play for current color."),
2023-01-27 14:59:13 +01:00
}
}
pub fn go(&mut self) -> Result<(), &'static str> {
match self.player[self.side_to_move().to_index()] {
2023-01-30 15:56:01 +01:00
Player::Human { .. } => Err("Not a machine to play for current color."),
2023-01-31 13:37:49 +01:00
Player::Engine { elo } => {
2023-01-27 14:59:13 +01:00
if self.is_uciok() {
if let Some(elo) = elo {
2023-01-30 15:56:01 +01:00
self.push(GuiCommand::SetOption {
option: Opt::UCILimitStrength { value: true },
});
self.push(GuiCommand::SetOption {
option: Opt::UCIElo { value: elo },
});
2023-01-27 14:59:13 +01:00
}
let (wtime, wincr) = self.time[Color::White.to_index()];
let (btime, bincr) = self.time[Color::Black.to_index()];
2023-01-30 15:56:01 +01:00
self.push(GuiCommand::Go {
wtime,
wincr,
btime,
bincr,
});
2023-01-27 14:59:13 +01:00
Ok(())
} else {
2023-01-30 15:56:01 +01:00
Err("UCI engine not ready")
2023-01-27 14:59:13 +01:00
}
}
}
}
2023-01-30 15:56:01 +01:00
pub fn stop(&mut self) -> Result<(), &'static str> {
2023-01-31 17:41:46 +01:00
self.push(GuiCommand::Stop);
Ok(())
2023-01-30 15:56:01 +01:00
}
2023-01-27 14:59:13 +01:00
}
2023-01-30 15:56:01 +01:00
use chess::{Color, Piece, Square};
impl<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
2023-01-16 17:08:06 +01:00
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)
}
2023-01-17 14:22:43 +01:00
pub fn current_position(&self) -> Board {
self.game.current_position()
}
2023-01-27 14:59:13 +01:00
pub fn side_to_move(&self) -> Color {
self.game.side_to_move()
}
2023-01-16 17:08:06 +01:00
}
2023-01-13 11:54:28 +01:00
// LocalWords: uci