我正在写一个国际象棋引擎(Stockfish)的前端。引擎是用C ++编写的,所以我的程序运行可执行的stockfish.exe。然后,我需要向引擎提供命令行指令并检索其输出。代码我已成功获取我发送的第一条指令(“uci”)的输出并将其输出到java系统输出。这很好,但程序然后挂起readLine上的while循环,因为引擎的输出已完成,并且没有遇到空字符串。因此,打印“完成”的行永远不会被执行。 关于如何解决这个问题的任何想法都非常赞赏。
public static void main(String args[]) {
String line = "";
try {
ProcessBuilder pb = new ProcessBuilder("d://Stockfish/stockfish- 7-win/Windows/stockfish7.exe");
Process pr = pb.start();
String[] cmd = new String[2];
cmd[0] = "uci" ;
cmd[1] = "isready" ;
PrintWriter writer = new PrintWriter(pr.getOutputStream()) ;
InputStreamReader stream = new InputStreamReader(pr.getInputStream()) ;
BufferedReader input = new BufferedReader(stream);
writer.println(cmd[0]) ;
writer.flush() ;
while ((line = input.readLine()) != null) {
System.out.println(line);
System.out.println("got to here") ;
}
System.out.println("done") ;
writer.println(cmd[1]) ;
writer.flush() ;