Python: Sunfish chess engine
Jonni Bidwell analyses the innards of a small but perfectly formed chess engine that bests him with alarming regularity.
Legend tells of one Sissa ibn Dahir who invented the game of Chess for an Indian king. So impressed was that king that he offered Sissa anything he desired as a reward. Being of a calculating bent, Sissa replied “Then I wish that one grain of wheat shall be put on the first square of the chessboard, two on the second, and that the number of grains shall be doubled until the last square is reached: whatever the quantity this might be, I desire to receive it”. The king soon realised that there was not enough wheat in the world to fulfil this demand, and once again was impressed. There are various endings to this story, in one Sissa is given a position within the king’s court, in another he is executed for being a smart arse. Hopefully this tutorial’s chess treatment will feature neither execution nor LXF towers being buried in mountains of wheat.
Chess is a complicated game – all the pieces move differently depending on their circumstances, there are various extraordinary moves (eg en passent pawn capture, castling) and pawns get promoted if they make it all the way to the other side. As a result, a surfeit of pitfalls present themselves to the chess-programming dilettante, so rather than spending a whole tutorial falling into traps we’re going to borrow the code from Thomas Ahle’s Sunfish – a complete chess engine programmed in Python. There’s no shortage of chess engines: from the classic GNU Chess to the Kasparovbeating Deep Blue (1997) to the pack-leading Stockfish. Chess engines on their own generally do not come with their own GUI, their code being mostly devoted to the not inconsiderable problem of finding the best move for a given position. Some (including Sunfish) allow you to play via a text console, but most will talk to an external GUI, such as xboard, via a protocol such as the Universal Chess Interface (UCI) or WinBoard. Besides providing nice pictures of the action, this enables us to play against different engines from a single program. Furthermore, we can pit engine against engine and enjoy chess as a spectator sport.
The Sunfish engine
We’ll assume that you know how to play chess, but if you don’t you can practice by playing against Thomas’s Sunfish engine. You’ll find the code on the LXFDVD in the Tutorials/ Chess directory. Copy this directory to your home folder, and then run it with: $ cd ~/Chess $ python sunfish.py
The program uses Unicode glyphs to display the pieces in the terminal, making it look a little more chess-like than GNU Chess. Check the box ( seeInstallingXboundandInterfacing withSunfish) to see how to enjoy graphical play. Moves are inputted by specifying the starting and ending coordinates, so the aggressive opening which moves the king’s pawn to e4 would be inputted e2e4. Note that this is slightly longer than the more common algebraic notation (in which the previous move would be written e4 ), but makes it much easier for