Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

efficient storage of a chess position

I've read tons of web hits related to this issue, and I still haven't come across any definitive answer.

What I'd like to do is to make a database of chess positions, capable of identifying transpositions (generally which pieces are on which squares).

EDIT: it should also be capable to identify similar (but not exactly identical) positions.

This is a discussion almost 20 years ago (when space was an issue): https://groups.google.com/forum/#!topic/rec.games.chess.computer/wVyS3tftZAA

One of the discussants talk about encoding pieces on a square matrix, using 4 x 64 bits plus some bits more for the additional information (castling, en passant etc): there are six pieces (Pawn, Rook, Knight, Bishop, Queen, King) plus an empty square, that would be 3 bits (2^3), and one more bit for the color of the piece.

In total, there would be 4 numbers of 64bits each, plus some additional information.

Question: is there any other, more efficient way of storing a chess position?

I should probably mention this question is database centric, not game centric (i.e. my sole interest is to efficiently store and retrieve, not to create any AI or to generate any moves).

Thanks, Adrian

like image 725
Adrian Avatar asked Jan 27 '14 07:01

Adrian


People also ask

How do you store a chess board?

As a solid wood chess board manufacturer, I believe it is best to store a board on a flat surface. If you have multiple boards you can put a soft cotton cloth, like an old tee shirt, between the boards. Never cover or wrap the board in plastic, the wood has to breath. The climate is more important than anything.

Which piece holds the most powerful position in chess?

The queen is known as the most powerful piece on the chess board, so the prospect of sacrificing it invokes an unparalleled excitement among chess enthusiasts. There is something inherently satisfying about giving up the strongest piece on the board in order to checkmate the enemy king.

How do you keep track of chess moves?

Keeping score is simple. All you do is write down each move that each player makes using a letter for the name of the piece and a square name (letter and number) for the square the piece ends up on. This way you can replay your games to show off your winning games and understand what happened if you lost.

How many bits would be needed to store the 64 locations of a chess board?

As far as I can think, since there are 64C2 = 2016 ways to represent two pieces on an 8x8 board, the minimum number of bits required should be 11.


1 Answers

If you do not need a decodable position representation for comparisons then you could look at Zobrist hashing. This is used by chess engines to produce a 64 bit oneway hash of a position for spotting transpositions in search trees. As it is a oneway hash you obviously cannot reverse the position from the hash. The size of the hash is tunable, but 64 bits seems to be the accepted minimum size that results in few collisions. It would be ideal as a database index key with a fixed length of just 8 bytes. As collisions, though infrequent, are possible you could do a second pass comparing the actual positions to filter out any positions that have hashed to the same value if it is a concern. I use Zobrist hashes in one of my own applications (using SQLite) that I use to manage my openings and it has no trouble in finding transpositions.

like image 64
Stephen Avatar answered Sep 21 '22 05:09

Stephen