我正在考虑制作一个php脚本,它可以打开stockfish象棋引擎CLI,发送fews命令并返回输出。我想我可以通过使用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);
}这个过程似乎已经开始了,但是我发送给process的第二个STDINPUT不能等到它完成,因为第二个命令会产生大约5秒的分析行,并且它打印出来的结果是立即的。我怎么才能上车?
CLI link,CLI documentation link
如果你需要的话,请向我询问更多关于这个引擎的信息。
谢谢!
发布于 2016-12-01 20:57:37
fwrite($pipes[0], "uci/n");
fwrite($pipes[0], "go movetime 5000/n");如果没有/n,Stockfish将其视为一个命令(ucigo Movetime5000),并且无法识别它。
发布于 2015-08-17 00:18:42
实际上,您的代码可以正常工作。$pipes1包含了stockfish的所有输出...
您可能需要更改行
position startpos moves 5000设置为不同的数字,因为5000表示5000 ms =5秒,即。引擎停止的时间。尝试10000,引擎在10秒后停止,等等。
发布于 2021-07-01 16:54:02
你需要删除:fclose($pipes[0]);并在while循环中检查bestmove,如果找到bestmove -中断循环,然后只放fclose($pipes[0]);。这对我很管用。然后在命令末尾添加\n分隔符。
谢谢你的代码!
https://stackoverflow.com/questions/31817146
复制相似问题