3

I am trying to communicate with the Stockfish chess engine from a PHP script. For that I am using the UCI protocol, so I will need to send commands line by line. For example, this is what I would normally enter on the Windows Command Prompt:

Stockfish.exe
position startpos
go depth 16

Stockfish.exe is the 64-bit version of the Stockfish chess engine.

I failed to do it using exec(). Here is how I attempted it:

exec("stockfish.exe\nposition startpos\ngo depth 16");

The engine runs, but the commands are not executed, so I get:

Stockfish 10 64 by T. Romstad, M. Costalba, J. Kiiski, G. Linscott

where I should get something like:

info depth 1 seldepth 1 multipv 1 score cp 116 nodes 20 nps 10000 tbhits 0 time 2 pv e2e4  
info depth 2 seldepth 2 multipv 1 score cp 112 nodes 54 nps 27000 tbhits 0 time 2 pv e2e4 b7b6  
info depth 3 seldepth 3 multipv 1 score cp 148 nodes 136 nps 45333 tbhits 0 time 3 pv d2d4 d7d6 e2e4  
info depth 4 seldepth 4 multipv 1 score cp 137 nodes 247 nps 61750 tbhits 0 time 4 pv d2d4 e7e6 e2e4 c7c6  
bestmove d2d4 ponder e7e6

I've read many threads on related issues, but couldn't find a workaround. Is there any way to accomplish this?

Wais Kamal
  • 5,858
  • 2
  • 17
  • 36
  • Just to clarify... you don't actually want to run a REPL in PHP (like the Windows command prompt), you want to send and receive line-delimited data for a child process (`stockfish.exe`) over STDIO, correct? – Brad Feb 07 '19 at 14:56
  • 1
    Yes, but what's a REPL? – Wais Kamal Feb 07 '19 at 15:00
  • https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop – Brad Feb 07 '19 at 15:00
  • Thanks. I am OK with both. What matters for me is getting the same output I would get if I were using the command prompt. – Wais Kamal Feb 07 '19 at 15:03
  • maybe this helps to execute the commands in one exec command, use && https://stackoverflow.com/questions/7122742/execute-two-shell-commands-in-single-exec-php-statement – FatFreddy Feb 07 '19 at 15:06
  • @FatFreddy this doesn't work. I only get the text "Stockfish 10 64 by ...". – Wais Kamal Feb 07 '19 at 15:15
  • FWIW it seems to work correctly if the `position startpos` is left off. The following command returns the correct number of moves as expected `exec('stockfish.exe go depth 16' . "\r\n" . 'quit');` – Dave Feb 07 '19 at 15:48
  • It works because `startpos` is the default position, but sonner or later I will have to send something like `position startpos moves g1f3` followed by `go depth 16`, for instance. – Wais Kamal Feb 07 '19 at 15:53
  • And one thing to add: it only returns `bestmove e2e4 ponder e7e5` but omits the above lines. – Wais Kamal Feb 07 '19 at 16:01
  • When I ran it I got an array of 17 elements and the last one was the bestmove line. Had you included a variable in the call to exec? – Dave Feb 07 '19 at 16:13
  • I am not getting an array, only the above mentioned line (I am getting the same output with print_r instead of echo). And, I did not pass any variables in the call. I just did it the same way you did it. – Wais Kamal Feb 07 '19 at 16:18
  • Add `,$back);` after the 'quit' and you will see all of the moves. – Dave Feb 07 '19 at 16:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188066/discussion-between-wais-kamal-and-dave). – Wais Kamal Feb 07 '19 at 16:23

0 Answers0