【发布时间】:2020-04-01 17:39:04
【问题描述】:
我在 Windows 10 上使用 Visual Studio 2019,使用 boost.process 库。我正在尝试下象棋,并且我正在使用 stockfish 引擎作为单独的可执行文件。我需要引擎在整个游戏中运行,因为这就是它的设计用途。
目前我在 ChessGame.h
class ChessGame
{
public:
void startStockFish();
void beginGame();
void parseCommand(std::string cmd);
private:
boost::process::child c;
boost::process::ipstream input;
boost::process::opstream output;
}
在 ChessGame.cpp 中
#include ChessGame.h
void ChessGame::startStockFish()
{
std::string exec = "stockfish_10_x32.exe";
std::vector<std::string> args = { };
boost::process::child c(exec, args, boost::process::std_out > input,
boost::process::std_in < output);
//c.wait()
}
void ChessGame::beginGame()
{
parseCommand("uci");
parseCommand("ucinewgame");
parseCommand("position startpos");
parseCommand("go");
}
void ChessGame::parseCommand(std::string cmd)
{
output << cmd << std::endl;
std::string line;
while (std::getline(input, line) && !line.empty())
{
std::cout << line << std::endl;
}
}
在 main.cpp 中
ChessGame chessGame = ChessGame(isWhite); //isWhite is a boolean that control who the player is, irrelevent to the question
//std::thread t(&ChessGame::startStockFish, chessGame);
chessGame.startStockFish();
chessGame.beginGame();
问题是我相信一旦函数 startStockFish 完成它就会终止 c,因为如上所述没有任何内容输出到终端,但是如果我在 startStockFish() 中使用 beginGame(),它会按预期输出。此外,如果我取消注释 c.wait() 行并且函数等待 stockfish 退出,它会卡住,因为 stockfish 永远不会获得退出命令。如果我尝试在 main 中的单独线程上运行 startStockFish(如上所示),我 得到以下两个错误:
功能测试宏的参数必须是一个简单的标识符。
在文件 'boost\system\detail\config.hpp' 第 51 行
和
'std::tuple::tuple':没有重载函数需要 2 个参数。
在文件“内存”第 2042 行
另外,我不想使用线程,因为我可以想象到输入和输出流会有自己的问题。
那么有没有办法让我在这个函数之外保持进程活跃,还是我需要以其他方式重新组织我的代码?我相信在 main 中调用该进程会起作用,但我真的不想这样做,因为我想将所有与国际象棋相关的代码保留在 ChessGame.cpp 中。
【问题讨论】: