Linux Format

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 calculatin­g 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 complicate­d game – all the pieces move differentl­y depending on their circumstan­ces, there are various extraordin­ary 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-programmin­g 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 Kasparovbe­ating 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 inconsider­able 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. Furthermor­e, 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

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 ( seeInstall­ingXbounda­ndInterfac­ing withSunfis­h) to see how to enjoy graphical play. Moves are inputted by specifying the starting and ending coordinate­s, 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

 ??  ??
 ??  ??

Newspapers in English

Newspapers from Australia