88board_banner

0x88 Board

In this post we’ll start looking at my reference chess board implementation. Remember, I want to keep this simple and maybe not spend too much time thinking about optimisation or even optimisable code. This is just a learning experience that will ease the future development of a much more efficient version.

The simplest implementation would seem to be just to define a structure that describes the contents of a square (I call them ‘cells’ in my code) and then define an array of 8×8 of these or simply:

These could be arrange any way you please but it makes sense that cells[0] is A1, cells [1] is A2, cells[8] is B1, etc.

However, a slightly larger, more complicated board array actually makes a lot of the chess rules engine code much easier. Therefore for this implementation I’m going to be using the common 0x88 board format. Much has been written about this format so I’m not going to duplicate it in my words here but basically the board is laid out as in the diagram below:

88board

What you’ll notices is that the indices for the cells follow a simple pattern; all indices for a particular file share the same low nibble and all indices for a particular rank share the same high nibble. Further, the top bit of each nibble is clear for all valid cells so we can easily check whether an index is valid using the following C code:

So now we can declare some structures that represent the data in a cell and create the board array.

 

Now we have our board structure we can start writing some functions.

Starting with a simple one just to clear all pieces from the board:

And here’s a useful function for debugging that prints an ASCII representation of the board:

Notice that the function above uses a macro ‘CELL_INDEX’ to convert from file and rank to an index. I’ve generated a number of useful macros like this:

And finally for now, a very scrappy, partial implementation of a FEN string parser just so I’m able to populate the board.

Putting all this together with a simple main() function containing the following code results in an 0x88 board that can be printed to the screen.

board_output

3 thoughts on “0x88 Board”

  1. Nice tutorial..thanks. I cannot compile the code though as I get errors and I’m a little shaky with C programming.

    gcc -Wall -c “chess.c” (in directory: C:\RPD_Computing_Programming\RPD_Chess_programming\www.chessengine.co.uk)
    chess.c:44:2: warning: parameter names (without types) in function declaration [enabled by default]
    chess.c:62:1: error: unknown type name ‘CHESS_CELL’
    chess.c:65:1: warning: data definition has no type or storage class [enabled by default]
    chess.c:65:1: warning: type defaults to ‘int’ in declaration of ‘invalid’ [-Wimplicit-int]
    chess.c:65:11: error: ‘index’ undeclared here (not in a function)
    chess.c: In function ‘print_board':
    chess.c:123:2: warning: implicit declaration of function ‘get_board’ [-Wimplicit-function-declaration]
    chess.c:123:22: warning: initialization makes pointer from integer without a cast [enabled by default]
    chess.c:127:24: error: ‘piece_char’ undeclared (first use in this function)
    chess.c:127:24: note: each undeclared identifier is reported only once for each function it appears in
    Compilation failed.

  2. Can you please explain piece_char
    ‘function’ i believe if im not mistaken(im java-nese).
    Because your code is looks somewhat incomplete for someone who still learn to code and because i only need the idea behind those little code. In fact ive been trying to port your code into java as a learning purposes. As you know the code on the net was highly optimized and hard to digest but yours is a friendly one.
    CHESS_PIECE = piece_char[……
    Those where i lost.

    1. Hi,
      I should really have posted a download for the full source to this code as the code on the website is just for demonstration and doesn’t contain the complete code. I’ll see if I can dig it out and post it.

      The purpose of piece_char[] is to provide a way for converting an enumerated piece (ROOK, KNIGHT, etc.) into a character that represents the piece on an ASCII board. For example, piece_char[ROOK] returns ‘R’. It is defined as:

      char piece_char[] = { ‘.’, ‘P’, ‘N’, ‘B’, ‘R’, ‘Q’, ‘K’, ‘#’ };

      I really must get around to updating this blog. I’ve actually got a lot further in the development than the blog reflects, having a fully working bitboard implementation. I’ll try to resume posting soon.

      Thanks for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">