Home Projects Bot-vinnik Introduction Computer Chess Stockfish Twitch III CTC Filter Teaching Pendulum Outdoors Leatherworking About Me

Stockfish Interface

Part of the project involves consulting with the open source chess engine Stockfish to determine the robot's next move. Eventually I'd like to have a standalone program handling the image processing, engine consultation, and motor instruction delivery, but for now the first test was getting the engine running.

That was easy.

When you download Stockfish you get an .exe that produces a little command line window when run. It turns out there's a standardized protocol for communicating with chess engines called UCI. In the example above, I gave the engine a position to consider with the line "position startpos," in this case the starting position of chess. I then entered "go depth 10," asking it to determine the best move by looking at scenarios up to 10 moves in the future. The engine spits out a bunch of its calculations along the way, but ultimately what we're interested in is the move suggestion given in the final line "bestmove g1f3 ponder d7d5".

The chess board is an 8x8 grid, and both computers and players refer to squares on the grid by their coordinates: the letters a-h represent the x-coordinate and the numbers 1-8 represent the y-coordinate. So when Stockfish says "bestmove g1f3 ponder d7d5", it thinks white should move its knight from the g1 square to the f3 square, and that in response black should probably move his pawn from d7 to d5.

The starting chess position with the square coordinates shown

I had Stockfish working from the command line, but ultimately I'd like to query it from another program. I wrote a short script in Python (chosen because of nice support with OpenCV and my modest familiarity with the language) that spawns Stockfish as a subprocess and lets the user play a game of chess against it.

Playing text based chess against the computer in a Python console. I will lose.

I'll call this module a partial success for now. My implementation is synchronous, so in order to give the engine a few seconds to think I have to put my script to sleep. This isn't ideal because in the end project the computer has other jobs to do, specifically image processing and communicating with the robot microcontroller.

The script is available here