#shogi #usi #sfen

usi

A library to handle type-safe communication with USI-compatible shogi engines

7 releases (breaking)

0.6.0 Oct 3, 2021
0.5.0 Oct 3, 2021
0.4.1 Aug 15, 2021
0.4.0 Nov 14, 2020
0.1.0 May 13, 2017

#259 in Game dev

Download history 59/week @ 2021-08-11 8/week @ 2021-08-18 2/week @ 2021-08-25 1/week @ 2021-09-01 5/week @ 2021-09-08 23/week @ 2021-09-15 6/week @ 2021-09-22 64/week @ 2021-09-29 4/week @ 2021-10-06 8/week @ 2021-10-13 12/week @ 2021-10-20 3/week @ 2021-10-27 11/week @ 2021-11-03 8/week @ 2021-11-10 21/week @ 2021-11-17 13/week @ 2021-11-24

53 downloads per month
Used in usi-run

MIT license

41KB
911 lines

usi-rs

Github Actions Coverage Status crates.io docs.rs

A library to handle type-safe communication with USI-compatible shogi engines.

Documentation

Usage

[dependencies]
usi = "0.6"

License

usi-rs is licensed under the MIT license. Please read the LICENSE file in this repository for more information.


lib.rs:

A module for working with USI protocol in a type-safe way.

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.

Data types representing commands defined in USI protocol.

GuiCommand and EngineCommand represents input/output commands defined in the protocol.

Examples

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::from_secs(1)).wtime(Duration::from_secs(2));
let cmd = GuiCommand::Go(params);
assert_eq!("go btime 1000 wtime 2000", cmd.to_string());

// 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))) => {
        assert_eq!("7g7f", *m);
        assert_eq!("8c8d", *pm);
    },
    _ => unreachable!(),
}

Working with a USI engine process

UsiEngineHandler can be used to spawn the USI engine process. You can send GuiCommands and receive EngineCommand.

Examples

use usi::{BestMoveParams, Error, EngineCommand, GuiCommand, UsiEngineHandler};

let mut handler = UsiEngineHandler::spawn("/path/to/usi_engine", "/path/to/working_dir").unwrap();

// Get the USI engine information.
let info = handler.get_info().unwrap();
assert_eq!("engine name", info.name());

// Set options.
handler.send_command(&GuiCommand::SetOption("USI_Ponder".to_string(), Some("true".to_string()))).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 best_move_sfen,
                     ref ponder_move,
                ))) => {
                    assert_eq!("5g5f", best_move_sfen);
                }
        _ => {}
    }
    Ok(())
}).unwrap();
handler.send_command(&GuiCommand::Usi).unwrap();

Dependencies

~0.6–1.1MB
~25K SLoC