A library to handle type-safe communication with USI-compatible shogi engines. USI protocol defines commands sent from either GUIs or engines. Detail about USI protocol can be found at http://www.geocities.jp/shogidokoro/usi.html.
GuiCommand and EngineCommand represents input/output commands defined in the protocol.
```rust use std::time::Duration; use usi::{GuiCommand, ThinkParams, EngineCommand, BestMoveParams};
// GuiCommand can be converted into the USI compliant string. let params = ThinkParams::new().btime(Duration::fromsecs(1)).wtime(Duration::fromsecs(2)); let cmd = GuiCommand::Go(params); asserteq!("go btime 1000 wtime 2000", cmd.tostring());
// EngineCommand can be parsed from the command string sent from the USI engine. let cmd = EngineCommand::parse("bestmove 7g7f ponder 8c8d").unwrap(); match cmd { EngineCommand::BestMove(BestMoveParams::MakeMove(ref m, Some(ref pm))) => { asserteq!("7g7f", *m); asserteq!("8c8d", *pm); }, _ => unreachable!(), } ```
UsiEngineHandler can be used to spawn the USI engine process. You can send GuiCommands and receive EngineCommand.
```rust use usi::{BestMoveParams, Error, EngineCommand, GuiCommand, UsiEngineHandler};
let mut handler = UsiEngineHandler::spawn("/path/to/usiengine", "/path/to/workingdir").unwrap();
// Get the USI engine information. let info = handler.getinfo().unwrap(); asserteq!("engine name", info.name());
// Set options. handler.sendcommand(&GuiCommand::SetOption("USIPonder".tostring(), Some("true".tostring()))).unwrap(); handler.prepare().unwrap(); handler.send_command(&GuiCommand::UsiNewGame).unwrap();
// Start listening to the engine output. // You can pass the closure which will be called // everytime new command is received from the engine. handler.listen(move |output| -> Result<(), Error> { match output.response() { Some(EngineCommand::BestMove(BestMoveParams::MakeMove( ref bestmovesfen, ref pondermove, ))) => { asserteq!("5g5f", bestmovesfen); } _ => {} } Ok(()) }).unwrap(); handler.send_command(&GuiCommand::Usi).unwrap(); ```
usi-rs
is licensed under the MIT license. Please read the LICENSE file in this repository for more information.