1

我想明确一点:我想将我的国际象棋 GUI 连接到国际象棋 UCI。图形用户界面 -> 引擎。通用国际象棋 UCI 将标准输出一个“bestmove”,然后可以由国际象棋 GUI 执行。但我遇到的唯一问题是让我的 GUI 将自己附加到这个单独的程序并获取 stdout 调用。我试过 _popen 和 system 但这两个函数会在调用某些东西时关闭程序。这就是我的意思:

system(stockfish.exe position startpos moves e2e4);
system(stockfish.exe go depth 10);

output:
Stockfish 091021 by the Stockfish developers (see AUTHORS file)
Stockfish 091021 by the Stockfish developers (see AUTHORS file)
info string NNUE evaluation using nn-13406b1dcbe0.nnue enabled
info depth 1 seldepth 1 multipv 1 score cp 38 nodes 20 nps 6666 tbhits 0 time 3 pv d2d4
info depth 2 seldepth 2 multipv 1 score cp 82 nodes 50 nps 12500 tbhits 0 time 4 pv e2e4 a7a6
info depth 3 seldepth 3 multipv 1 score cp 65 nodes 122 nps 24400 tbhits 0 time 5 pv g1f3 h7h6 d2d4
info depth 4 seldepth 4 multipv 1 score cp 41 nodes 456 nps 65142 tbhits 0 time 7 pv g1f3 d7d5 d2d4 c7c6
info depth 5 seldepth 5 multipv 1 score cp 22 nodes 1409 nps 117416 tbhits 0 time 12 pv c2c4 e7e5 d2d4 e5d4 d1d4
info depth 6 seldepth 6 multipv 1 score cp 25 nodes 1973 nps 123312 tbhits 0 time 16 pv g1f3 c7c5 c2c4 g8f6 d2d4 c5d4 f3d4
info depth 7 seldepth 7 multipv 1 score cp 39 nodes 2804 nps 140200 tbhits 0 time 20 pv g1f3 d7d5 e2e3 e7e6 c2c4 d5c4 f1c4
info depth 8 seldepth 10 multipv 1 score cp 49 nodes 8782 nps 141645 tbhits 0 time 62 pv e2e4 c7c5 b1c3 d7d6 g1e2 e7e5 c3d5
info depth 9 seldepth 12 multipv 1 score cp 36 nodes 17265 nps 140365 tbhits 0 time 123 pv e2e4 c7c6 d2d4 d7d5 b1d2 d5e4 d2e4 g8f6 e4f6 e7f6
info depth 10 seldepth 13 multipv 1 score cp 47 nodes 21380 nps 149510 tbhits 0 time 143 pv e2e4 c7c5 b1c3 d7d6 g1e2 e7e5 e2g3 c8e6 c3d5
bestmove e2e4 ponder c7c5

stockfish 运行了两次,由两个“Stockfish 开发人员的 Stockfish 091021(参见 AUTHORS 文件)”指示,我的输出是 e2e4,清楚地表明它忽略了我的起始位置。我也试过这个我在stackoverflow上找到的函数,但遇到了同样的问题。

std::string exec(const char* cmd) {
    char buffer[128];
    std::string result = "";
    FILE* pipe = _popen(cmd, "r");
    if (!pipe) throw std::runtime_error("popen() failed!");
    try {
        while (!feof(pipe)) {
            if (fgets(buffer, 128, pipe) != NULL)
                result += buffer;
        }
    }
    catch (...) {
        _pclose(pipe);
        throw;
    }
    _pclose(pipe);
    return result;
}

void main() {
    std::cout << exec("C:/Users/kurtk/source/Chess/Project1/Project1/stock/stockfish.exe position startpos moves e2e4");
    std::cout << exec("C:/Users/kurtk/source/Chess/Project1/Project1/stock/stockfish.exe go depth 10");
}

output:
Stockfish 091021 by the Stockfish developers (see AUTHORS file)
Stockfish 091021 by the Stockfish developers (see AUTHORS file)
info string NNUE evaluation using nn-13406b1dcbe0.nnue enabled
info depth 1 seldepth 1 multipv 1 score cp 38 nodes 20 nps 20000 tbhits 0 time 1 pv d2d4
info depth 2 seldepth 2 multipv 1 score cp 82 nodes 50 nps 50000 tbhits 0 time 1 pv e2e4 a7a6
info depth 3 seldepth 3 multipv 1 score cp 65 nodes 122 nps 61000 tbhits 0 time 2 pv g1f3 h7h6 d2d4
info depth 4 seldepth 4 multipv 1 score cp 41 nodes 456 nps 152000 tbhits 0 time 3 pv g1f3 d7d5 d2d4 c7c6
info depth 5 seldepth 5 multipv 1 score cp 22 nodes 1409 nps 201285 tbhits 0 time 7 pv c2c4 e7e5 d2d4 e5d4 d1d4
info depth 6 seldepth 6 multipv 1 score cp 25 nodes 1973 nps 197300 tbhits 0 time 10 pv g1f3 c7c5 c2c4 g8f6 d2d4 c5d4 f3d4
info depth 7 seldepth 7 multipv 1 score cp 39 nodes 2804 nps 200285 tbhits 0 time 14 pv g1f3 d7d5 e2e3 e7e6 c2c4 d5c4 f1c4
info depth 8 seldepth 10 multipv 1 score cp 49 nodes 8782 nps 168884 tbhits 0 time 52 pv e2e4 c7c5 b1c3 d7d6 g1e2 e7e5 c3d5
info depth 9 seldepth 12 multipv 1 score cp 36 nodes 17265 nps 169264 tbhits 0 time 102 pv e2e4 c7c6 d2d4 d7d5 b1d2 d5e4 d2e4 g8f6 e4f6 e7f6
info depth 10 seldepth 13 multipv 1 score cp 47 nodes 21380 nps 156058 tbhits 0 time 137 pv e2e4 c7c5 b1c3 d7d6 g1e2 e7e5 e2g3 c8e6 c3d5
bestmove e2e4 ponder c7c5

我还尝试将程序作为一行运行,并将 '\n' 放在单独的命令之间,但这也不起作用。我错过了什么?

4

0 回答 0