Add UCI communication example
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
use std::{
|
use std::{
|
||||||
io::{self, BufRead, Write},
|
io::{BufRead, Write},
|
||||||
str::SplitWhitespace,
|
str::SplitWhitespace,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -133,38 +133,34 @@ pub fn uci_go(go: &mut SplitWhitespace, game: &mut Game) -> Result<Move, String>
|
|||||||
Ok(Move::new(Square::B8, Square::C6))
|
Ok(Move::new(Square::B8, Square::C6))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uci_loop() -> Result<(), String> {
|
pub fn uci_loop<R: BufRead, W: Write>(input: R, mut output: W) -> Result<(), String> {
|
||||||
let stdin = io::stdin();
|
|
||||||
let stdout = io::stdout();
|
|
||||||
let handle_in = stdin.lock();
|
|
||||||
let mut handle_out = stdout.lock();
|
|
||||||
let mut params = UciParameters::new();
|
let mut params = UciParameters::new();
|
||||||
|
|
||||||
for line in handle_in.lines() {
|
for line in input.lines() {
|
||||||
let line_str = line.unwrap_or_else(|_| "quit".to_string());
|
let line_str = line.unwrap_or_else(|_| "quit".to_string());
|
||||||
let mut parts = line_str.split_whitespace();
|
let mut parts = line_str.split_whitespace();
|
||||||
let command = parse_command(&mut parts)?;
|
let command = parse_command(&mut parts)?;
|
||||||
let response = match command {
|
let response = match command {
|
||||||
Command::Uci => Response::UciOk,
|
Command::Uci => Response::UciOk,
|
||||||
Command::IsReady => Response::ReadyOk,
|
Command::IsReady => Response::ReadyOk,
|
||||||
Command::UciNewGame => Response::Info("Clear cached game.".to_string()),
|
Command::UciNewGame => Response::Info("Clear cache".to_string()),
|
||||||
Command::Position => {
|
Command::Position => {
|
||||||
params.add_game(uci_position(&mut parts)?);
|
params.add_game(uci_position(&mut parts)?);
|
||||||
Response::Info("Initialized position.".to_string())
|
Response::Info("Initialized position".to_string())
|
||||||
}
|
}
|
||||||
Command::Go => {
|
Command::Go => {
|
||||||
if let Some(ref mut game) = params.game {
|
if let Some(ref mut game) = params.game {
|
||||||
let best_move = uci_go(&mut parts, game)?;
|
let best_move = uci_go(&mut parts, game)?;
|
||||||
Response::BestMove(best_move.parse_into_str())
|
Response::BestMove(best_move.parse_into_str())
|
||||||
} else {
|
} else {
|
||||||
Response::Info("Going!".to_string())
|
Response::Info("Going?".to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Command::Quit => break,
|
Command::Quit => break,
|
||||||
};
|
};
|
||||||
|
|
||||||
write_response(&mut handle_out, response)?;
|
write_response(&mut output, response)?;
|
||||||
handle_out.flush().map_err(|e| e.to_string())?;
|
output.flush().map_err(|e| e.to_string())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -172,11 +168,16 @@ pub fn uci_loop() -> Result<(), String> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use std::io::Cursor;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
board::fen::from_fen,
|
board::{fen::from_fen, game::Game, square::Square},
|
||||||
interface::uci::{parse_command, Command},
|
interface::uci::{parse_command, Command},
|
||||||
|
movegen::r#move::Move,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use super::uci_go;
|
||||||
|
use super::uci_loop;
|
||||||
use super::uci_position;
|
use super::uci_position;
|
||||||
|
|
||||||
const FEN: [&str; 2] = [
|
const FEN: [&str; 2] = [
|
||||||
@@ -205,32 +206,37 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_uci_go() -> Result<(), String> {
|
fn test_uci_go() -> Result<(), String> {
|
||||||
let mut _game = from_fen(FEN[0])?;
|
let mut game = Game::new();
|
||||||
|
let command_go = "go depth 4";
|
||||||
|
let mut parts = command_go.split_whitespace();
|
||||||
|
let response = uci_go(&mut parts, &mut game)?;
|
||||||
|
|
||||||
|
assert_eq!(Move::new(Square::B8, Square::C6), response);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_uci_loop() -> Result<(), String> {
|
fn test_uci_loop() -> Result<(), String> {
|
||||||
let mut _game = from_fen(FEN[0])?;
|
let commands = b"uci\n\
|
||||||
|
ucinewgame\n\
|
||||||
|
position startpos moves e2e4 e7e5\n\
|
||||||
|
go\n\
|
||||||
|
quit";
|
||||||
|
let input = Cursor::new(commands);
|
||||||
|
let mut output: Vec<_> = vec![];
|
||||||
|
|
||||||
|
uci_loop(input, &mut output)?;
|
||||||
|
|
||||||
|
let expected_response = "id name ippos\n\
|
||||||
|
id author stefiosif\n\
|
||||||
|
uciok\n\
|
||||||
|
Clear cache\n\
|
||||||
|
Initialized position\n\
|
||||||
|
bestmove b8c6\n";
|
||||||
|
let actual_response = String::from_utf8(output).expect("Invalid UTF-8 in output");
|
||||||
|
|
||||||
|
assert_eq!(expected_response, actual_response);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
```
|
|
||||||
- CuteChess: `uci`
|
|
||||||
- Engine: `id name MyChessEngine`
|
|
||||||
- Engine: `id author John Doe`
|
|
||||||
- Engine: `option name Hash type spin default 32 min 1 max 512`
|
|
||||||
- Engine: `uciok`
|
|
||||||
- CuteChess: `setoption name Hash value 128`
|
|
||||||
- CuteChess: `ucinewgame`
|
|
||||||
- CuteChess: `position startpos moves e2e4 e7e5`
|
|
||||||
- CuteChess: `go wtime 600000 btime 600000 winc 0 binc 0`
|
|
||||||
- Engine: `info depth 1 score cp 20 nodes 15 nps 1500 time 10 pv e2e4`
|
|
||||||
- Engine: `bestmove e2e4`
|
|
||||||
- CuteChess: `quit`
|
|
||||||
```
|
|
||||||
*/
|
|
||||||
|
|||||||
10
src/main.rs
10
src/main.rs
@@ -1,16 +1,24 @@
|
|||||||
|
use std::io;
|
||||||
|
|
||||||
mod board;
|
mod board;
|
||||||
mod evaluation;
|
mod evaluation;
|
||||||
mod interface;
|
mod interface;
|
||||||
mod movegen;
|
mod movegen;
|
||||||
mod search;
|
mod search;
|
||||||
|
|
||||||
|
use interface::uci;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
movegen::attack::init_attacks();
|
movegen::attack::init_attacks();
|
||||||
|
|
||||||
let command = std::env::args().nth(1).unwrap_or(String::from("quit"));
|
let command = std::env::args().nth(1).unwrap_or(String::from("quit"));
|
||||||
match command.as_str() {
|
match command.as_str() {
|
||||||
"perft" => search::perft::perftree_script(),
|
"perft" => search::perft::perftree_script(),
|
||||||
"uci" => interface::uci::uci_loop().unwrap_or_else(|e| println!("{}", e)),
|
"uci" => {
|
||||||
|
let input = io::stdin().lock();
|
||||||
|
let output = io::stdout().lock();
|
||||||
|
uci::uci_loop(input, output).unwrap_or_else(|e| println!("{}", e));
|
||||||
|
}
|
||||||
"quit" => println!("Exiting.."),
|
"quit" => println!("Exiting.."),
|
||||||
_ => println!("Wrong command. Exiting.."),
|
_ => println!("Wrong command. Exiting.."),
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user