使用通用国际象棋接口(UCI)

33
我计划制作一个与UCI国际象棋引擎交互的程序。我已经做了一些研究,但在深入了解之前,我想获得更多的信息。我想知道你们之中是否能提供一些UCI引擎和前端程序之间的“交流”示例。我不太关心实际接口代码(如发送/接收命令),那应该很简单。我只是想得到一些小游戏和一些选项的好例子。我目前正在使用Stockfish引擎,但我想能够使用多个引擎。
总之,我正在寻找一些在UCI上玩游戏的示例。

你有查看规格说明书吗?所有通信都通过标准输入和输出以文本命令的形式进行。 - raina77ow
我知道,我正在寻找一些文本示例来玩一个基本游戏。 - Nathan
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

我简化了许多交互的方面。一个完整功能的图形用户界面将要支持你可以在UCI规范 (另一个来源) 找到的其他命令。你也可以看一下现有的图形用户界面是如何工作的。例如,如果你使用Arena,你可以按F4键查看命令交互日志。


4
谢谢!我的主要问题是,下一步怎么办?国际象棋引擎是否维护了一个内部的棋盘状态,或者我的程序是否只需根据我的移动(如e2e4)和引擎建议的最佳移动更新FEN位置即可?如果它没有内部状态,我将手动处理FEN棋盘位置,然后调用例如“position CURRENT_POSITION d2d4”并等待最佳移动?还是说有一些内部棋盘状态可以更新?如果有,怎么更新?谢谢你的帮助,Zong Zheng Li! - Nathan
从一开始到现在的完整移动序列是什么? - Nathan
(我给出的两个命令在不同的行上,这里的注释格式不是最好的)无法提供翻译,因为原始文本没有给出。 - Nathan
3
@Nathan 我遇到了类似的问题,后来发现在 position startpos moves e2e4 中忘记了关键词 **moves**。之后输入 go 命令后就会回应一个黑方的移动。 - Frank S. Thomas
@FrankS.Thomas 对不起,我搞砸了。 - Zong
显示剩余3条评论

4

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

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

sudo apt install stockfish scid moreutils

创建一个 stockfish 包装器:

mystockfish

#!/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

待办事项:有没有更简单的方法来获得类似的输出(交错的I/O,每行标记为I或O)?我的Bash技能不够好!我尝试过:

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
  • d2d3 d7d5
  • 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

这只是官方的UCI规范,也可以在https://www.shredderchess.com/chess-features/uci-universal-chess-interface.html上找到。 - balu

0

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

例如,在PyChess中使用它作为引擎"nochetoengi --log-file /tmp/log.txt",你就可以看到所有的通信记录了。

注意:仅适用于Linux系统。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接