【问题标题】:Running Stockfish chess engine under php script在 php 脚本下运行 Stockfish 国际象棋引擎
【发布时间】:2015-10-27 07:44:12
【问题描述】:

我正在考虑制作一个 php 脚本,它可以打开 stockfish 国际象棋引擎 CLI,发送一些命令并取回输出。 我想我可以通过使用带有管道数组的proc_open 来实现这一点,但我不知道如何等待整个输出......如果有另一个更好的解决方案,不胜感激!

这是我的代码:

// ok, define the pipes
$descr = array(

    0 => array("pipe", "r"),
    1 => array("pipe", "w"),
    2 => array("pipe", "w")
);

$pipes = array();

// open the process with those pipes
$process = proc_open("./stockfish", $descr, $pipes);

// check if it's running
if (is_resource($process)) {

    // send first universal chess interface command
    fwrite($pipes[0], "uci");
    // send analysis (5 seconds) command
    fwrite($pipes[0], "go movetime 5000");

    // close read pipe or STDOUTPUT can't be read
    fclose($pipes[0]);

    // read and print all output comes from the pipe
    while (!feof($pipes[1])) {

        echo fgets($pipes[1]);

    }

    // close the last opened pipe
    fclose($pipes[1]);

    // at the end, close the process
    proc_close($process);

}

进程似乎开始了,但是我发送给进程的第二个 STDINPUT 不能等到它完成,因为第二个命令会产生大约 5 秒的分析行,并且它立即打印出结果。 怎么办?

CLI link, CLI documentation link

如果您需要,请向我询问有关此引擎的更多信息。

谢谢!

【问题讨论】:

    标签: php bash shell chess


    【解决方案1】:
    fwrite($pipes[0], "uci/n");
    fwrite($pipes[0], "go movetime 5000/n");
    

    没有 /n Stockfish 将其视为一个命令(ucigo movetime 5000)并且无法识别它。

    【讨论】:

      【解决方案2】:

      实际上,您的代码有效。 $pipes[1] 包含了 stockfish 的所有输出...

      你可能需要换行

      position startpos moves 5000
      

      到不同的数字,因为 5000 意味着 5000 毫秒 = 5 秒,即。发动机停止的时间。尝试 10000 和 10 秒后引擎停止等。

      【讨论】:

        【解决方案3】:

        您需要删除:fclose($pipes[0]); 并在 while 循环中检查 bestmove,如果找到 bestmove - 打破循环,然后只输入 @987654322 @。这对我有用。并在命令末尾添加 \n 分隔符。

        感谢代码!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-05-02
          • 1970-01-01
          • 2022-01-25
          • 1970-01-01
          • 2022-01-11
          • 2022-01-05
          • 1970-01-01
          相关资源
          最近更新 更多