Chessboard Representation


The Chessboard

There are many ways to represent the chessboard, KKFChess uses a "bordered" array board to represent the chessboard.  Bitboards are also used for storing where the pieces are in the board.  The chessboard actually sits on its side with the white pieces on the RIGHT HAND SIDE and the black pieces on the left.  This is what the chessboard looks like in memory:

+0 X X X X X X X X X X
+10 X X X X X X X X X X
+20 X h8 h7 h6 h5 h4 h3 h2 h1 X
+30 X g8 g7 g6 g5 g4 g3 g2 g1 X
+40 X f8 f7 f6 f5 f4 f3 f2 f1 X
+50 X e8 e7 e6 e5 e4 e3 e2 e1 X
+60 X d8 d7 d6 d5 d4 d3 d2 d1 X
+70 X c8 c7 c6 c5 c4 c3 c2 c1 X
+80 X b8 b7 b6 b5 b4 b3 b2 b1 X
+90 X a8 a7 a6 a5 a4 a3 a2 a1 X
+100 X X X X X X X X X X
+110 X X X X X X X X X X
  +0 +1 +2 +3 +4 +5 +6 +7 +8 +9
+0 X X X X X X X X X X
+10 X X X X X X X X X X
+20 X 63 62 61 60 59 58 57 56 X
+30 X 55 54 53 52 51 50 49 48 X
+40 X 47 46 45 44 43 42 41 40 X
+50 X 39 38 37 36 35 34 33 32 X
+60 X 31 30 29 28 27 26 25 24 X
+70 X 23 22 21 20 19 18 17 16 X
+80 X 15 14 13 12 11 10 9 8 X
+90 X 7 6 5 4 3 2 1 0 X
+100 X X X X X X X X X X
+110 X X X X X X X X X X
  +0 +1 +2 +3 +4 +5 +6 +7 +8 +9
"Bordered" array board Bitboard bit number mapping

The X's are squares that are off-limit to pieces.  This value is used to detect if a piece has moved off the board during move generation.  I could have use a normal looking board with a another bitboard mapping but I would like the bitboard map to be as "normal" looking as possible - helps to visualize the bit patterns when generating moves.

For example, a knight at c1 (offset +78) moving ENE (+1-10+1 = -8) will end up in an off-board square at offset +70 (=78-8).

With this arrangement, KKFChess checks the piece that's on the target square and not the target square's "coordinates" for determining if a move goes off the board (c.f. 0x88 board).  No checks are performed nor required on the "coordinates" of the target square.


The Pieces

Each piece of each colour is represented by a different value, empty squares are 0 and off-board markers are -1 (all ones for unsigned):

Empty WP WN WB WR WQ WK BP BN BB BR BQ BK Off-board
0 1 2 3 4 5 6 9 10 11 12 13 14 -1

Each piece type has it's own bitboard to tell the engine which piece is in which square.  When no pieces of such type is on the board, it's bitboard value will be zero.

Using bitboards to hold the pieces' position removes the need to scan the board for pieces or use an piece position array during move generation but requires special code to handle bit patterns.


Other Position Information

A chess position is more that just the static board.  Information like which side to move, the en passant square, castling rights, the fifty moves counter, the number of repetitions, etc. are also needed.

In crazyhouse games we need to store the pieces captured.  When a promoted piece is taken, it will revert back into a pawn, meaning that promoted pieces for this variant needs to be tracked.

In atomic chess, capturing can cause several pieces to be removed form the board, these pieces need to be stored as well.