Python running Stockfish and Chessboard talking serialy to an Arduino sketch controlling the board.
There are two programs, one running on the Arduino and the other on the Raspberry PI.
The Arduino is a real time computer with good connectivity and a wide range of connectable devices with supporting libraries. It is ideal for controlling all the I/O but it has limited memory and there are not many software programs for chess available.
There are a few chess programs written for the Arduino see: http://chessprogramming.wikispaces.com/Arduino But Stockfish and other powerful chess engines will not run on an Arduino.
The Raspeberry PI is not real time, has very limited device connectivity, but it does run a version of Linux and as such there are chess engines like Stockfish available and very flexible programming languages like Python.
The Arduino Program
The Arduino carries out most of the input & output, in particular scanning the reed switches to see if the state of squares changes, indicating a move and then flashing LEDs to indicate when the computer wants to move from & to.
The main functions are:
Scan the board:
The computer holds a simple 64 byte list corresponding to each square. 0 indicates and empty square, 11, 12, 13, 14, 15, 16 indicate white King, Queen, Bishop, Knight, Rook & Pawn respectively. Black pieces are 21, 22, etc. This numbering system allows easily manipulation for operations like pawn promotion (new piece = pawn – 1 to 4).
When a new game begins the board is scanned to ensure there are 32 pieces on the the first and last two rows and infers their value from the known starting position.
With the Centipede board the scan uses a simple HIGH/LOW test to determine if a square is occupied.
Other New Game functions allow the user to chhose Level, Agressiveness, Cowardice, Time per move, Play Black etc from a menu shown on the display. Responses are given using one of four coloured buttons. When a number greater than four is required, e.g. to set a playing level from 0 to 20, the user is invited to move a piece, so for example moving a piece on A4 signals level 3.
These options are communicated to the Pi using various transaction codes. eg L3
In play, after a board move the Arduino sends a transaction of the form Me2e4. The board knows about special moves and makes them based on the first part of the move. When castling the player makes the King move and the board signals the Rook move with the LEDs. Only the King move is sent to the Pi
After sending a move, the Arduino waits for a reply from the PI. The reply will be one of the following transactions:
- e: error in board move “e”+ reson code + eroneous move
x: Board move results in Checkmate = “x”+Game Result code
t: text message from board “t”text e.g. “tCheck on “+colour for check as a result of board move or other text message, or the file name of the pgn file
m: move ma1a2xyb1b2, where a1a2 is move x is a pawn promotion char, y = “h” if its check or if mate: 0 to 4 result code , b1b2 is hint
For a normal move “m” code, the Arduino will flash a light under the piece to be moved, wait for a lift (Reed switch opens) the signal the move to square by flashing and again wait for an opening or closing of the reed switch depending on wether the square is occupied.
Other codes result in combinations of board scans, displays on the screen and waiting for button presses.
The Raspberry PI programs
The PI’s job is to take transactions from the Arduino validate them, determine changes in game state (Check, checkmate, promotion, resaignation etc) then get a move from the Stockfish game engine, validate again and send to the board.
The Python program also handles serial communications with the Arduino and writes out the pgn file if required at the end of the game as well as other houskeeping tasks.
Why the StockFish Chess Engine?
I wanted a chess engine that was:
Powerful: Why build a wimpy? chess computer, If I am going to spend a lot of time on this project I want one that will beat a Grand Master. (On a bad day)
Configurable: Its no fun to get beaten all the time, so I want to be able to set the level of difficulty / timing easily.
Free: I’d rather not pay for it, so it has to be open source.
Easy to use: It needs to use UCI or another well defined interface
Robust: I don’t want it falling over. I have enough trouble fixing my own bugs without worrying other people.
Runs on a sensible platform: It needs to run on a small, cheap computer I can build into a chess computer.
As far as I know Stockfish is the only Chess engine that meets all this needs. It is the second most powerful chess engine. According to Computerchess.org with an ELO rating of 3312. Houdini is 3345 but you have to buy it. To put this in context Magnus Carlsen the current World No 1 has a rating of 2882.
It runs using the UCI interface and runs on almost any Unix or PC type machine, including the Raspberry PI, but not of course an Arduino. It is widely used throughout the world an actively supported so it works. The UCI standard lets you configure lots of parameters via your own user interface, In my case the board.
To install Stockfish on your Raspberry Pi, go to the Debian command line and type:
sudo apt-get install stockfish
Stockfish is the Dirty Harry of chess engines:
“I know what you’re thinking, punk. You’re thinking “is it mate in six moves or only five?” Now to tell you the truth I forgot myself in all this excitement. But being this is a 64 bit Stockfish, the most powerful chess engine in the world and will blow you defence clean off, you’ve gotta ask yourself a question: “Do I feel lucky?” Well, do ya, punk?”
I have written a demonstration program that runs stockfish and can serve as foundation for communication with a board or anything else. It actually plays chess at a very strong level.
I have listed the source here.