Recommand · October 14, 2021 0

Multiple commands in _popen not working properly

I’m trying to make a program that runs stockfish 14. For this to work properly I’d need to create a pipe to an executable (which in this case it’d be ‘stockfish.exe’). In order to get stockfish to move, stockfish needs the command ‘go depth 10’ which gives me back the best move. This is relatively simple:

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("stock/stockfish go depth 10");
}

output:
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 25000 tbhits 0 time 2 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 114000 tbhits 0 time 4 pv g1f3 d7d5 d2d4 c7c6
info depth 5 seldepth 5 multipv 1 score cp 22 nodes 1409 nps 156555 tbhits 0 time 9 pv c2c4 e7e5 d2d4 e5d4 d1d4
info depth 6 seldepth 6 multipv 1 score cp 25 nodes 1973 nps 164416 tbhits 0 time 12 pv g1f3 c7c5 c2c4 g8f6 d2d4 c5d4 f3d4
info depth 7 seldepth 7 multipv 1 score cp 39 nodes 2804 nps 175250 tbhits 0 time 16 pv g1f3 d7d5 e2e3 e7e6 c2c4 d5c4 f1c4
info depth 8 seldepth 10 multipv 1 score cp 49 nodes 8782 nps 195155 tbhits 0 time 45 pv e2e4 c7c5 b1c3 d7d6 g1e2 e7e5 c3d5
info depth 9 seldepth 12 multipv 1 score cp 36 nodes 17265 nps 215812 tbhits 0 time 80 pv e2e4 c7c6 d2d4 d7d5 b1d2 d5e4 d2e4 g8f6 e4f6 e7f6
info depth 10 seldepth 13 multipv 1 score cp 47 nodes 21380 nps 222708 tbhits 0 time 96 pv e2e4 c7c5 b1c3 d7d6 g1e2 e7e5 e2g3 c8e6 c3d5
bestmove e2e4 ponder c7c5

So as you can see this works. The only problem I’m having however is when I’m trying to input previous moves into stockfish. Because that’s how stockfish works. So basically I have to show stockfish the list of moves from the start of the game to the current move for it to play. To show stockfish the moves in the game it takes the command ‘position startpos moves (and then the moves)’. So for example:

void main() {
    std::cout << exec("stock/stockfish position startpos moves e2e4 e7e5"); //e4 e5 for an example.
}

output:
Stockfish 091021 by the Stockfish developers (see AUTHORS file)

It doesn’t give me back the ‘bestmove’ after e7e5. Why? Because I haven’t also typed in ‘go depth 10’. And this is where the problem arises. I actually need to type in TWO commands. So I’ve tried:

void main() {
    std::cout << exec("stock/stockfish position startpos moves e2e4 e7e5 & stock/stockfish 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 10000 tbhits 0 time 2 pv d2d4
info depth 2 seldepth 2 multipv 1 score cp 82 nodes 50 nps 25000 tbhits 0 time 2 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 219222 tbhits 0 time 9 pv g1f3 c7c5 c2c4 g8f6 d2d4 c5d4 f3d4
info depth 7 seldepth 7 multipv 1 score cp 39 nodes 2804 nps 215692 tbhits 0 time 13 pv g1f3 d7d5 e2e3 e7e6 c2c4 d5c4 f1c4
info depth 8 seldepth 10 multipv 1 score cp 49 nodes 8782 nps 219550 tbhits 0 time 40 pv e2e4 c7c5 b1c3 d7d6 g1e2 e7e5 c3d5
info depth 9 seldepth 12 multipv 1 score cp 36 nodes 17265 nps 218544 tbhits 0 time 79 pv e2e4 c7c6 d2d4 d7d5 b1d2 d5e4 d2e4 g8f6 e4f6 e7f6
info depth 10 seldepth 13 multipv 1 score cp 47 nodes 21380 nps 213800 tbhits 0 time 100 pv e2e4 c7c5 b1c3 d7d6 g1e2 e7e5 e2g3 c8e6 c3d5
bestmove e2e4 ponder c7c5

The bestmove is still e2e4 implying it is ignoring my position declaration. Stockfish is being run twice as well, indicated by the two ‘Stockfish 091021 by the Stockfish developers (see AUTHORS file)’

void main() {
    std::cout << exec("stock/stockfish position startpos moves e2e4 e7e5\ngo depth 10\n");
}

output:
Stockfish 091021 by the Stockfish developers (see AUTHORS file)

Here it just ignores my ‘go depth 10’. The commands I typed in isn’t being saved to stockfish. Is there a way to type in two commands where the first command doesn’t become disregarded after the second command was entered?