![]() |
LaMoSca v0.01The representation of the chessboard |
Objective
For the time being the program's interface is the text-mode Windows console of the computer. The only aim of this version is to check that the software development environment works before starting on more complex parts. The alternativesHow the chessboard is represented on the computer is a particularly important choice, because much of the software's efficiency in generating and analysing the position of the pieces, and therefore in choosing the moves to play in a game of chess, depends on it. The most obvious representation, an 8x8 matrix, is rarely used, because scanning the squares would require the software to update two indices (rank and file). More common (e.g. GNU Chess) is a representation with two arrays of 64 elements, one for the chess pieces and one for the colours of the squares.
Because of the way the knight moves, two border squares are needed beyond the 8 regular ones, which brings the number of rows to 12 (two above and two below). As for the columns, when the array is scanned sequentially the right-hand and left-hand border columns are crossed one after the other, so they can be merged into just two extra columns, for a total of 12 rows by 10 columns. For efficiency it is also possible to use 16-column representations, which make the data multiples of 8 bits and so ease data transfer between the processor and the computer's memory, as well as the operations inside the processor itself. Chess programs with an 8x8 representation check the validity of the destination square by temporarily converting to 10x12 when they verify the legality of the moves during the game, as shown later. |
|
Another representation uses bitboards (e.g. Crafty). Bitboards do not represent the 64 squares of the board, but the 12 chess pieces (pawn, rook, knight, bishop, queen and king, for both White and Black). By assigning 64 bits to each piece (one per square) and marking the relevant squares with "1", the same information as the conventional representation can be stored in a very compact way. For example, a bitboard for the white knights is shown alongside. To make the program more efficient, other bitboards are usually added to the 12 piece bitboards, for instance to store the position of all the white pieces or of all the black ones. With this representation complex information can be obtained very quickly with single logical operations. For example, to find out whether a piece is attacked by another at some point in the game, it is enough to AND the two bitboards together: on a 64-bit processor this takes a single machine cycle. |
00000000 00000000 00000000 00000000 00000000 00000000 01000010 |
The codeThe software is written to run as a "Win32 Console Application". The structure of the program is therefore conventional, with "main", standard input/output and so on: you can find references on this in any introductory book on the "C" language, starting with the classic "The C Programming Language" by Kernighan and Ritchie. The main function only reads the string from the command line, checks whether it is one of the engine's commands and finally tries to interpret it as the coordinates of a move to play. To store the position, an array of 64 records is used, which gives each square separate values for the colour and for the piece that may occupy it. This is certainly not the most efficient representation, but it was chosen to keep the code easy to read. After downloading and compiling the software, you can type a few commands to see if there are any signs of life: for example, you can try the fascinating opening d1e8 ;-) |
DownloadTo do
... practically everything :-( |
Links |
| Back | Index | Next |