Chess engine’s anatomy
[This article was originally published in my personal blog. This is a new version.]
A chess engine is simply a console program that reads some input (a move, a command…) from standard input and write output (a move, a command…) in standard output. But in which way a computer can play to chess? Well, it isn’t simple, but with simple words we can say that a chess engine:
- it reads a move
-
it updates an internal representation of chess board
- from this position (root) it generates all possible moves
- for every moves: it generates all countermoves and so on…
- at the end it uses an evaluation function and measures the best position
- it writes (like output) the move that leads itself to the best position
Remember: this is a very simplified version! Now you can understand that to write a chess engine you need:
- Some way to represent a chess board in memory;
- Some way to update the chess board;
- Some way to generate moves;
- A technique to choose the move to make amongst all legal possibilities;
- An evaluation function;
- Some sort of user interface;
You can forget to program an user interface: we’ll use Winboard or Arena, two very popular freeware G.U.I. In that way we must write a simple console program that reads and writes from/to Standard Input/Output.
To start i suggest to schedule your work in this way:
- decide how to represent chessboard in memory
- write utility function to translate FEN position into your chessboard representation
- write moves generator
- write function to understand if a square is attacked by a color
- write functions to update your chessboard representation
- Main check point 1: stop, write a Perft function and debug!
- write a simple evaluation function
- write a search function
- write a function that drives this search
- write a simple function to talk with Winboard/Arena
- Main check point 2: stop and debug!
Remember it is better first to write a simple engine and then to improve it step by step.
You can find a lot of hints, info and papers in:
- Winboard Forum
- Computer Chess Club
- Gamedev articles: 1, 2, 3, 4, 5, 6
- Bruce Moreland’s web site
- Ed Schröder’ web site (only advanced users)