by timerol · 3y ago · 0 comments
Store one 64 bit bitboard - a set bit means that a piece is present at that place. An unset bit means that no piece is after that position. After the bitboard, store a list of 32 4 bit integers, where the order of the pieces in the list corresponds to the order of the bits set. If there are less than 16 bits set in the bitboard, ignore the last items in the list.
0000 - 0x0 - black pawn
0001 - 0x1 - black pawn (can be en-passant'd)
0010 - 0x2 - black knight
0011 - 0x3 - black bishop
0100 - 0x4 - black rook (castling unavailable)
0101 - 0x5 - black rook (castling available)
0110 - 0x6 - black king
0111 - 0x7 - black queen
1000 - 0x8 - white pawn
1001 - 0x9 - white pawn (can be en-passant'd)
1010 - 0xA - white knight
1011 - 0xB - white bishop
1100 - 0xC - white rook (castling unavailable)
1101 - 0xD - white rook (castling available)
1110 - 0xE - white king
1111 - 0xF - white queen
I think that covers all possibilities to store a chess position in 64 + 32 * 4 = 192 bits, or 24 bytes exactly.The starting position would be represented with a bitboard of 0xFFFF00000000FFFF, with a list of [0xD, 0xA, 0xB, 0xF, 0xE, 0xB, 0xA, 0xD, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5, 0x2, 0x3, 0x7, 0x6, 0x3, 0x2, 0x5], using the same position-to-number scheme in the blog post
Edit: 32 pieces, not 16. Thanks to the peanut gallery for catching it quickly
Edit2: To store which player is next: do nothing for white. For black, if there are 32 pieces, flip the bitboard upside down. (To check if it's black's turn, verify that black pawns are "below" white pawns, which is illegal before captures are made.) If there are less than 32 pieces and it's black's turn, invert the bitboard. (To check, count the number of set bits.) This is entirely taken from https://news.ycombinator.com/item?id=37526484
It is necessary to find another way to represent en-passant opportunities (swap an en-passantable pawn with the piece in the 1st or last rank as mentioned https://news.ycombinator.com/item?id=37526523 ).
2 rook representations aren't needed (encode a king with castling opportunities as a knight, bishop or rook, and when decoding, if there are no kings of a given color, look at the piece in the king's starting position) so another 4 bits could be saved by making rook, bishop or knight representations shorter.
000 - black pawn
001 - black knight
0100 - black bishop
0101 - black rook
0110 - black king
0111 - black queen
100 - white pawn
101 - white knight
1100 - white bishop
1101 - white rook
1110 - white king
1111 - white queen
Going with "a pawn in the first row is actually en-passantable, and should be swapped with the piece in the appropriate position if it exists", and "kings are represented as knights when they can castle in either direction, bishops when they can only castle kingside, and rooks when they can only castle queenside", that gets the initial representation down to 64 + 16 * 3 (pawns) + 6 * 3 (knights and kings) + 10 * 4 = 170 bits, with a worst case of 172 bits (when kings lose some castling rights), if I understand it correctly.Huffman encoding for 21.5 bytes seems to win the day.
Can you explain this in more detail? Curious as to how you could save space with this.
Here's a good example image for huffman coding: https://i.ytimg.com/vi/hOabRMHzpo8/hqdefault.jpg
So before any captures are made, you have 32 pieces, half of which use 3 bits and half of which use 4 bits. 14 bytes, plus the 8 bytes storing the bitboard.
When you promote a piece it goes up in size from 3 to 4 bits, but you can guarantee there have been enough captures to offset that, so you never need more space than you started with.
I like the "no kings" idea for castleable kings - though I think with smaller pawn sizes, that gives another opportunity for compression:
- king that can't castle: king
- king that can castle queenside: any other piece in king's position, no king of that color on board
- king that can castle kingside: black pawn on 1st or 8th rank
- king that can castle: white pawn on 1st or 8th rank
Because you are often going to have positions where the king can castle kingside and positions where the king can castle either way, this should maximize how often you manage to save space w/ the pawns.
Another thought I had (which might contain other problems, not sure yet) is to use pawns on the 1st or 8th rank to denote pieces which are in their starting position - the decompression algorithm can then derive what piece it is based on the known starting position. Once we start having data saving because of pawns taking less bits, we want to be able to use them as much as possible to save space.
The bit to indicate whose turn it is isn't accounted for here. But it probably could probably represented by the binary negation of the bitboard -- if there are 32 or fewer bits set, then it is white's turn; if there are 33 or more bits set, then it is black's turn and you can negate the bitboard prior to determining which squares are occupied.
Taking things further, it probably can be compressed even more with (much) more complex logic, as the castling available bit must only be present on the corner positions, and the pawn en-passant capabilities are only available on the middle rows, so those bits are meaningless in other positions.
(The 16->32 was edited as you wrote this comment.)
[1] log(13^32)/log(2) = 118.4
This doesn't quite work because if there are exactly 32 bits set, inverting it leaves 32 bits set. You could fix this by marking all pawns belonging to the player who's turn it is as capturable via en-passant (if a player has no pawns left, at least 1 piece has been captured, so inverting the bitboard works).
edit: You can then use the inverted/flipped board trick to store one bit of your piece list, down to 191 bits.
edit2: You only need one "king (my turn)" ID, which represents the whichever color you didn't use for the other king. You can use the extra value for another "my turn" piece that also sets a bit. 190 bits!
If instead you're trying to store every competition chess game in history, then it depends on what you're trying to do with them. Look for similar board positions?
If you're trying to allow inmates in a Dumas-inspired prison secretly play chess against each other over a covert channel, then detection is the problem. Which might mean compression (fewer signals to hear) or masking the signal as random noise.
"" is the beginning position
"0" is E4
"1" is D4
"2" is C4
"3" is E4 E5
"4" is D4 D5
"5" is G3
One could do a multi byte version where the order of popular positions is replaced with a crappy chess computer.
If there is no en-passant, no promoted pawn and castling is allowed you can use representations slightly shorter than 24.
0 - encodes no piece in the square; at least 32 of them, so 32 bits (4 bytes)
1xxxx - xxxx being your encoding: encodes remaining pieces; at most 32 of them, so max 32 x 5 bits (20 bytes)
* If there are less than 32 bits set in the bitboard, the item list is shorter (rather than ignored).
* Probably not worth it, but: When there are 32 bits set on the bitboard, stop processing the bit-board.. however, if you adjusted the ordering of the lines (0,7,1,2,...), it's optimised for the first 1 or 2 positions
* Maybe this is cheating, but if the size of the game data will be known ahead of processing, then you could leave off the last item in the list if it's a king or a rook
* Starting the item list with 2 white kings can be a special case for "starting position", and if the items are listed before the board then only 1 byte is needed :)
Using this approach, you would need to store castling available/unavailable for the kings as well.