Unfortunately, decoding a mathematically optimal encoding quickly devolves to making a list of all possible board configurations and indexing into it, so I'd definitely believe that the proposal here is close to the smallest practically useful representation.
How to store a chess position in 26 bytes using bit-level magic (2022)
31–40 of 204 posts
Re: How to store a chess position in 26 bytes using bit-level magic (2022)
#32Earlier quoted context omitted.
Iy's not meant for humans to read directly. The compact representation helps performance amd storage a lot when it comes to looking at a lot of positions. If you were to store it as a json most of your time would be spent parsing.
This is illustrated very well in Sebastian Lague's video "Coding Adventure: Making a Better Chess Bot" ( https://www.youtube.com/watch?v=_vqlIPDR2TU ).
Most likely unrelated, I know! Just a wondering in passing.
Re: How to store a chess position in 26 bytes using bit-level magic (2022)
#33An interesting cognitive phenomenon: if you ask chess experts and novices to memorize and recall an arbitrary chessboard, the experts are significantly better than the novices _only if_ the board is a legal board that can be arrived at during play. If it is not a legal board, there's no particular difference between novices and experts. Original ref: Chase & Simon 1973, Perception in Chess. This is usually taken to m…
Re: How to store a chess position in 26 bytes using bit-level magic (2022)
#34An interesting cognitive phenomenon: if you ask chess experts and novices to memorize and recall an arbitrary chessboard, the experts are significantly better than the novices _only if_ the board is a legal board that can be arrived at during play. If it is not a legal board, there's no particular difference between novices and experts. Original ref: Chase & Simon 1973, Perception in Chess. This is usually taken to m…
Re: How to store a chess position in 26 bytes using bit-level magic (2022)
#35Re: How to store a chess position in 26 bytes using bit-level magic (2022)
#36There are two more things that should be considered. Both of them unlikely to influence a position, but they can matter - just like the en passant target. The fifty move rule[0] is quite simple, just store a number, fits in five bits. But the threefold repetition rule[1] is quite a pickle - it basically means that to know everything about a position you need to know every position that occurred before it. [0] https:/…
Another straightforward thing missing is the player's turn; this could determine whether the position is a stalemate or not.
Re: How to store a chess position in 26 bytes using bit-level magic (2022)
#37Original title is: > Compressing chess positions for fun and profit Which unlike that here is correct: you can store a board , the positions, in 26B; not an arbitrary length game!
Most games could possibly be encoded in less than 26 bytes. There are not that many legal moves each time and if you sort them by probability it may not require many bits to describe which one the players chose.
Maybe this paper says. I didn't read it.
https://www.researchgate.net/figure/Entropy-and-distribution...
Re: How to store a chess position in 26 bytes using bit-level magic (2022)
#38Earlier quoted context omitted.
Well, this is arguably a kind of compression, right? So you'd be trading CPU time for fewer bytes? Is that a desirable tradeoff at chess engine scales?
Is your assertion that it takes more time for a CPU to read values out of a 30 byte struct and do a couple shifts and branches than to parse a JSON representation?
JSON is for storing data as text. Not work with that text all the time.
Re: How to store a chess position in 26 bytes using bit-level magic (2022)
#39There are two more things that should be considered. Both of them unlikely to influence a position, but they can matter - just like the en passant target. The fifty move rule[0] is quite simple, just store a number, fits in five bits. But the threefold repetition rule[1] is quite a pickle - it basically means that to know everything about a position you need to know every position that occurred before it. [0] https:/…
Re: How to store a chess position in 26 bytes using bit-level magic (2022)
#40Store 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