使用通用国际象棋界面

问题描述 投票:0回答:4

我正计划制作一个与 UCI 国际象棋引擎交互的程序。我一直在对此进行一些研究,但在更深入地了解它之前,我想获得更多信息。我想知道你们中是否有人可以提供一些 UCI 引擎和前端程序之间“交换”的示例。我并不真正关心实际的接口代码(例如发送/接收命令),这应该足够简单。我只是想获得一些小游戏的好例子和一些选项。我目前使用的是 stockfish 引擎,但我希望能够使用多个引擎。

所以无论如何,我正在寻找一些如何通过 UCI 玩游戏的示例。

linux interface chess uci
4个回答
41
投票

假设 GUI 正在促进人类用户和引擎之间的匹配。假设用户以

e2e4
开头。然后命令将类似于:

// GUI: tell the engine to use the UCI protocol
uci

// ENGINE: identify  
id name Chess Engine
id author John Smith

// ENGINE: send the options that can be changed
//         in this case the hash size can have a value from 1 to 128 MB
option name Hash type spin default 1 min 1 max 128

// ENGINE: sent all parameters and is ready
uciok

// GUI: set hash to 32 MB
setoption name Hash value 32

// GUI: waiting for the engine to finish initializing
isready

// ENGINE: finished setting up the internal values and is ready to start
readyok

// GUI: let the engine know if starting a new game
ucinewgame

// GUI: tell the engine the position to search
position startpos moves e2e4

// GUI: tell the engine to start searching
//      in this case give it the timing information in milliseconds
go wtime 122000 btime 120000 winc 2000 binc 2000

// ENGINE: send search information continuously during search
//         this includes depth, search value, time, nodes, speed, and pv line
info depth 1 score cp -1 time 10 nodes 26 nps 633 pv e7e6
info depth 2 score cp -38 time 22 nodes 132 nps 2659 pv e7e6 e2e4
info depth 3 score cp -6 time 31 nodes 533 nps 10690 pv d7d5 e2e3 e7e6
info depth 4 score cp -30 time 55 nodes 1292 nps 25606 pv d7d5 e2e3 e7e6 g1f3

// ENGINE: return the best move found
bestmove d7d5

我简化了交互的许多方面。功能齐全的 GUI 必须支持许多其他命令,您可以在 UCI 规范另一个来源)中找到这些命令。您还可以查看现有 GUI 的工作原理。例如,如果您使用Arena,您可以按F4查看命令交互的日志,


5
投票

对现有引擎/GUI 交互进行逆向工程

安装软件。在 Ubuntu 22.10 上测试:

sudo apt install stockfish scid moreutils

创建一个

stockfish
包装器:

我的鱼干

#!/usr/bin/env bash
rm -f /tmp/stockfish
tee -a >( ts %.S > /tmp/stockfish-in ) |
  stockfish "$@" |
  tee -a >( ts %.S > /tmp/stockfish-out )
sort -k2 -t ' ' --version-sort \
  <( sed -r 's/^/< /' /tmp/stockfish-in ) \
  <( sed -r 's/^/> /' /tmp/stockfish-out ) \
  >/tmp/stockfish

TODO:是否有更简单的方法来获得类似的输出(交错 I/O,如果是 I 或 O,则标记每一行)?我的 Bash-fu 还不够好!我试过:

tee -a >( sed -r 's/^/< /' >> /tmp/stockfish ) |
  stockfish "$@" |
  tee -a >( sed -r 's/^/> /' >> /tmp/stockfish )

但是 I/O 不再交错。总有一天我会明白的。

现在我们将 UCI 兼容 UI 指向帮助程序,例如对于

scid

  • 工具
  • 分析引擎

并选择

mystockfish
作为帮助程序(输入完整路径,或者如果您将其放入
PATH
中,则只需输入基本名称。)

现在当我们开始游戏时:

  • 严肃的游戏
  • 可以选择让事情进展得更快,以帮助测试对此设置的任何更改:固定深度:3

现在我将与

stockfish
进行以下比赛:

  • e2e4 e7e5
  • d2d3d7d5
  • f2f3 g8f6

文件

/tmp/stockfish
包含类似以下内容。我手动添加了一些明显的空行以提高可读性:

< 16.582386 uci
> 16.588033 Stockfish 14.1 by the Stockfish developers (see AUTHORS file)
> 16.639218 id name Stockfish 14.1
> 16.639301 id author the Stockfish developers (see AUTHORS file)
> 16.639331 
> 16.639359 option name Debug Log File type string default 
> 16.639387 option name Threads type spin default 1 min 1 max 512
> 16.639414 option name Hash type spin default 16 min 1 max 33554432
> 16.639440 option name Clear Hash type button
> 16.639467 option name Ponder type check default false
> 16.639494 option name MultiPV type spin default 1 min 1 max 500
> 16.639521 option name Skill Level type spin default 20 min 0 max 20
> 16.639547 option name Move Overhead type spin default 10 min 0 max 5000
> 16.639574 option name Slow Mover type spin default 100 min 10 max 1000
> 16.639619 option name nodestime type spin default 0 min 0 max 10000
> 16.639647 option name UCI_Chess960 type check default false
> 16.639674 option name UCI_AnalyseMode type check default false
> 16.639701 option name UCI_LimitStrength type check default false
> 16.639728 option name UCI_Elo type spin default 1350 min 1350 max 2850
> 16.639755 option name UCI_ShowWDL type check default false
> 16.639782 option name SyzygyPath type string default <empty>
> 16.639809 option name SyzygyProbeDepth type spin default 1 min 1 max 100
> 16.639836 option name Syzygy50MoveRule type check default true
> 16.639863 option name SyzygyProbeLimit type spin default 7 min 0 max 7
> 16.639890 option name Use NNUE type check default true
> 16.639917 option name EvalFile type string default nn-13406b1dcbe0.nnue
> 16.639944 uciok

< 18.825968 isready
> 18.826010 readyok

< 18.826234 position fen rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1
< 18.826317 go depth 3
> 18.826442 info string NNUE evaluation using nn-13406b1dcbe0.nnue enabled
> 18.826703 info depth 1 seldepth 1 multipv 1 score cp -19 nodes 22 nps 22000 tbhits 0 time 1 pv e7e5
> 18.826807 info depth 2 seldepth 2 multipv 1 score cp 4 nodes 43 nps 43000 tbhits 0 time 1 pv e7e5 a2a3
> 18.827150 info depth 3 seldepth 3 multipv 1 score cp -23 nodes 135 nps 67500 tbhits 0 time 2 pv e7e5 a2a3 g8e7
> 18.827231 bestmove e7e5 ponder a2a3

< 19.847364 isready
> 19.847471 readyok

< 19.848898 position fen rnbqkbnr/pppp1ppp/8/4p3/4P3/3P4/PPP2PPP/RNBQKBNR b KQkq - 0 2
< 19.849063 go depth 3
> 19.849119 info string NNUE evaluation using nn-13406b1dcbe0.nnue enabled
> 19.849387 info depth 1 seldepth 1 multipv 1 score cp 15 nodes 34 nps 34000 tbhits 0 time 1 pv b8c6
> 19.849475 info depth 2 seldepth 2 multipv 1 score cp 39 nodes 72 nps 72000 tbhits 0 time 1 pv b8c6 b1d2
> 19.849798 info depth 3 seldepth 3 multipv 1 score cp 13 nodes 205 nps 102500 tbhits 0 time 2 pv d7d5 e4d5 d8d5
> 19.849887 bestmove d7d5 ponder e4d5

< 20.868239 isready
> 20.868378 readyok

< 20.869825 position fen rnbqkbnr/ppp2ppp/8/3pp3/4P3/3P1P2/PPP3PP/RNBQKBNR b KQkq - 0 3
< 20.869973 go depth 3
> 20.870079 info string NNUE evaluation using nn-13406b1dcbe0.nnue enabled
> 20.870387 info depth 1 seldepth 1 multipv 1 score cp 174 nodes 63 nps 63000 tbhits 0 time 1 pv f8c5 e4d5
> 20.870691 info depth 2 seldepth 2 multipv 1 score cp 198 nodes 207 nps 207000 tbhits 0 time 1 pv g8e7 e4d5
> 20.871241 info depth 3 seldepth 4 multipv 1 score cp 124 nodes 462 nps 231000 tbhits 0 time 2 pv g8f6 b1c3 f8c5 e4d5
> 20.871299 bestmove g8f6 ponder b1c3

< 22.544435 stop
< 22.544537 quit
< r value false

所以从这里我们可以看到到底发生了什么。


3
投票

我创建了这个要点,这是我能找到的最好的文档——我发现的大多数链接都已失效

https://gist.github.com/aliostad/f4470274f39d29b788c1b09519e67372


0
投票

您可以使用 nochetoengi 来实现此目的。它有一个选项可以将与国际象棋 GUI 的通信记录到文件中。

例如,在 PyChess 中使用它作为引擎“nochetoengi --log-file /tmp/log.txt”,您将看到所有通信。

注意:仅适用于 Linux

© www.soinside.com 2019 - 2024. All rights reserved.