-
Notifications
You must be signed in to change notification settings - Fork 39
Chess board representation
In order to implement a chess game it is important to decide in what structure the board should be saved. This structure needs to address some requirements
- the state need to save all information to reconstruct the current state of the game
- it should be easy to determine if an move is invalid e.g. move outside of the board
- moving figures based on their move pattern should be easy to do
- the board should be saved memory efficient
The 0x88 board representation is described in : http://web.archive.org/web/20070909095550/http://www.seanet.com/~brucemo/topics/0x88.htm
The board is represented in an array holding two 8x8 boards -> 128 fields.
A | B | C | D | E | F | G | H | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 |
48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 |
80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 |
96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 |
112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 |
For representing the board, only the first board is needed. The second board helps for easier computation of the figure movement and positions. Moving from a Position inside of the board can easily be done by calculating the new index
| LEFT | _ | RIGHT |
--- | --- | --- | --- | |UP | -17 | -16 |-15 | | _ | -1 | 0 | 1 | | DOWN | 15 | 16 | 17 |
- Having this kind of board presentation allows us to easily see if a position(index) is inside the board -> 0x88 & index.
- The move patterns of a figure are now symmetric inside the board, meaning that a switch of the direction sign let us move in the opposite direction
The figures inside the board will be represented by the following table
B_K | B_Q | B_R | B_B | B_N | B_P | empty | W_P | W_N | W_B | W_R | W_Q | W_K |
---|---|---|---|---|---|---|---|---|---|---|---|---|
-6 | -5 | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
The color of the Player is encoded accordingly
Black | White |
---|---|
-1 | 1 |
Representing the board in this fashion allows us to easily see if
- what relationship two figure have to each other by multiplying them. This results in an easy query for detecting an enemy if(!figure_1*figure_2) -> if(is_enemy)
figure_1/ figure_2 | Black | White | empty | ---| ---| ---| ---| Black| 0>| <0| 0| White| <0| >0| 0| empty| 0| 0| 0
- Taking the absolute value of the figure value allows us to calculate possible move direction independent of color. The only exception is the Pawn, where we need to switch the sign of the direction, based on the color
There is a good explanation about it at FEN - mediocrechess.
The following flags are stored in the shadow field of the board (the right side). They can be found at these indices:
Flag | Indices | Meaning | Example |
---| ---| ---| ---|
MOVE_COUNT | 8 and 9| 8 = high 9 = low, if low is full -> 127 high gets increased by one. This allows us to store more than 127 in the move_count | 300 -> H=2 L=44 ( 2*128 +44 ) |
WHITE_KING_POS | 123 | The index of the position of the king should only relevant for the contract | .... |
BLACK_KING_POS | 11 | The index of the position of the king should only relevant for the contract | .... |
CURRENT_PLAYER | 56 | color of the current player white = 1, black = -1 | .... |
WHITE_LEFT_CASTLING | 78 | Flag if the castling to the left is possible (not if legal!) -1 False, 0 True | king or rook left move -> -1 |
WHITE_RIGHT_CASTLING| 79 | Flag if the castling to the right is possible (not if legal!) -1 False, 0 True| king or rook right move -> -1 |
BLACK_LEFT_CASTLING | 62 | Flag if the castling to the left is possible (not if legal!) -1 False, 0 True | king or rook left move -> -1 |
BLACK_RIGHT_CASTLING| 63 | Flag if the castling to the right is possible (not if legal!) -1 False, 0 True | king or rook right move -> -1 |
BLACK_EN_PASSANT | 61 | index to which the en passant hit is possible for the next turn, 0 if none| PB16->48 = 32|
WHITE_EN_PASSANT | 77 | index to which the en passant hit is possible for the next turn, 0 if none| PW97->65 = 82|