Chess Programming Tutorial...[]

Part 1: Getting started

A chess engine is a program that receives a board position as input and calculates a probably best move for that board with a given amount of possible effort (in most cases a time limit).

Don't start your chess program as a combination of graphical user interface and calculation engine in one program (executable). This will not allow your engine to play against other engines and in some point in time you want to do that.

What is the best programming language for an engine?

Use the one you know best or the one you want to learn, whatever your objectives are starting such a project.

A lot of engines out there are written in C or C++, but there are also ones written in Delphi, Pascal or Free Pascal (search for Lazarus) and Java. All those work well. I would not recommend to do it in PROLOG (might be interesting however) or Lisp, but feel free to try. One thing you might consider however is the ability to use inline assembler; this might become handy later.

For the code snippets I present here on this site I use C++ syntax as this language is very common, but like stated use whatever language you like.

Arena GUI

How does an engine operate?

An engine receives its command via standard in and outputs its responses to standard out. So it has no graphical user interface, no mouse input, no pictures, just a plain console window. You get the fancy graphic when you plug the engine into an external GUI so as a chess engine developer you don't have to try to paint a nice picture of a knight. The GUI will take care of that and there are good free ones available. (e.g. Arena GUI)

In order to be able to communicate with an external GUI your engine must implement a communication protocol. There are two possible choices, the x-Board and the UCI (Universal Chess Interface) protocol. UCI is the more modern protocol and the one I recommend.

Mate in 9

Below is a typical communication log of an UCI engine.

It starts with a uci command that tells the engine to identify itself. It then receives a board position in FEN notation and is told to spend 2000 ms to search for a best move. It starts its search and considers Bxf3 (pv hf53) as best move winning a rook for a bishop.

It keeps this move while searching deeper as best move and after 406 ms reaching depth 6 it changes its mind and considers now Rb7 (pv b4b7) as best move announcing a forced Mate in 9. It then searches further for an even better move, doesn't find one and when the time is up it outputs Rb7 as best move.

The list after pv is called the principle variation. This is the sequence of moves for both sides which the engine expects to be played. You can play them and learn a very nice mate sequence.

Don't worry if you don't know what a FEN notation is or what those UCI commands mean. We cover that later. This listing shall only provide a very first impression what a chess engine looks like.

iCE 0.1 build 1120 [2011.3.4] uci id name iCE 0.1 build 1120 id author Thomas Petzke option name OwnBook type check default true uciok position fen 3Q4/5q1k/4ppp1/2Kp1N1B/RR6/3P1r2/4nP1b/3b4 w - - go movetime 2000 info depth 1 seldepth 6 time 15 nodes 141 pv h5f3 nps 9400 score cp 150 hashfull 0 tbhits 0 info depth 2 seldepth 12 time 0 nodes 480 pv h5f3 g6f5 nps 479999 score cp 150 hashfull 0 tbhits 0 info depth 3 seldepth 16 time 16 nodes 2776 pv h5f3 g6f5 b4h4 h7g7 nps 173499 score cp 259 hashfull 0 tbhits 0 info depth 4 seldepth 18 time 31 nodes 11514 pv h5f3 e2f4 b4f4 d1a4 nps 371419 score cp 300 hashfull 0 tbhits 0 info depth 5 seldepth 25 time 47 nodes 19413 pv h5f3 d5d4 b4b7 f7b7 f3b7 nps 413042 score cp 467 hashfull 0 tbhits 0 info depth 6 seldepth 30 time 406 nodes 330625 pv b4b7 f7b7 h5g6 h7g6 d8g8 g6f5 g8g4 f5e5 g4h5 f3f5 f2f4 h2f4 h5e2 d1e2 a4e4 d5e4 d3d4 nps 814347 score mate 9 hashfull 5 tbhits 0 info depth 7 seldepth 16 time 860 nodes 929273 pv b4b7 f7b7 h5g6 h7g6 d8g8 g6f5 g8g4 f5e5 g4h5 f3f5 f2f4 h2f4 h5e2 d1e2 a4e4 d5e4 d3d4 nps 1080549 score mate 9 hashfull 15 tbhits 0 info depth 8 seldepth 16 time 640 nodes 650002 pv b4b7 f7b7 h5g6 h7g6 d8g8 g6f5 g8g4 f5e5 g4h5 f3f5 f2f4 h2f4 h5e2 d1e2 a4e4 d5e4 d3d4 nps 1015628 score mate 9 hashfull 23 tbhits 0 bestmove b4b7 quit

So let's have a look at the System Context Diagram for our chess engine. It is rather simple. We have two actors, a human chess player that enters command via the console windows or a chess GUI that sends commands to the standard input pipe of our engine. Both receive best move solutions and search information.

We have two optional external systems we might access with our engine, one is an opening book that contains common opening lines and one is an endgame table base that contains endgame scores for positions with only a few pieces left.


Now we know what a chess engine is and how it interacts with the outside world. It's time to have a look inside the blue bubble and move onto the chess engine architectural overview (AOD).