【发布时间】:2021-10-22 15:41:51
【问题描述】:
我正在使用 io 包来处理在我的 PATH 中定义的可执行文件。 该可执行文件称为“Stockfish”(国际象棋引擎),显然可以通过命令行工具使用。
为了让引擎搜索最佳移动,您使用“go depth n” - 深度越高 - 搜索所需的时间越长。 使用我的命令行工具,它使用 20 的深度搜索大约 5 秒,它看起来像这样:
go depth 20
info string NNUE evaluation using nn-3475407dc199.nnue enabled
info depth 1 seldepth 1 multipv 1 score cp -161 nodes 26 nps 3714 tbhits 0 time 7 pv e7e6
info depth 2 seldepth 2 multipv 1 score cp -161 nodes 51 nps 6375 tbhits 0 time 8 pv e7e6 f1d3
info depth 3 seldepth 3 multipv 1 score cp -161 nodes 79 nps 7900 tbhits 0 time 10 pv e7e6 f1d3 g8f6
info depth 4 seldepth 4 multipv 1 score cp -161 nodes 113 nps 9416 tbhits 0 time 12 pv e7e6 f1d3 g8f6 b1c3
[...]
bestmove e7e6 ponder h2h4
现在,使用 io.WriteString 它在几毫秒后完成,无需任何(可见)计算: (这也是下面代码的输出)
Stockfish 14 by the Stockfish developers (see AUTHORS file)
info string NNUE evaluation using nn-3475407dc199.nnue enabled
bestmove b6b5
这是我使用的代码:
func useStockfish(commands []string) string {
cmd := exec.Command("stockfish")
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
for _, cmd := range commands {
writeString(cmd, stdin)
}
err = stdin.Close()
if err != nil {
log.Fatal(err)
}
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
return string(out)
}
func writeString(cmd string, stdin io.WriteCloser) {
_, err := io.WriteString(stdin, cmd)
if err != nil {
log.Fatal(err)
}
这是我如何使用它的一个例子。第一个命令是设置位置,第二个是计算下一个最好的移动,深度为 20。结果如上所示。
func FetchComputerMove(game *internal.Game) {
useStockfish([]string{"position exmaplepos\n", "go depth 20"})
}
【问题讨论】: