Question

In a 8X8 chessboard, I was wondering how to implement the symmetry of the board.

A lot of positions are just mirrors or rotations of one another (with no pawns or castling capabilities left the directions are indistinguishable).

By using a combination of vertical, horizontal and diagonal mirrorings of the board it is always possible to fix the position of a piece within the a1-d1-d4 triangle.

How to implement these symmetries on chess board? Does it depends on choice of board representation chosen(Bitboards, 0x88, 8x8 array, and so on) ?

Edit 1: The goal is to implement generation of the endgame tables and their compression.

Was it helpful?

Solution

If you are looking to compress the boards, you can generate a canonical representation of each board. An older answer by @DocBrown expresses this well:

To make this more efficient, you can work with a "canonical representation" of each board, defined as follows. Generate all symmetric boards of a given one, pack each one of it into a byte array, and among those arrays keep the array which, interpreted as a big number, has the minimum value. This packed representation is a unique identifier of the symmetry class of each board and can be easily put in a dictionary / hash table, which makes testing if that symmetry class already appeared very efficient.

This question is in reference to the N-queens problem where a good deal of symmetry can be found since each queen is indistinguishable. For end-game boards, this is (rarely) the case so I'm not sure how much of a savings you'll get.

OTHER TIPS

Firstly, bear in mind that modelling this symmetry is an optimisation. You don't need to capture everything, it's a trade-off between effort, complexity of representation and the benefits achieved.

Also, it doesn't help to be able to localise each individual piece in the a1-d1-d4 triangle. You need to worry about all the pieces on the board at once and how they relate to each other. It doesn't make sense to treat two rooks on a1 and h1 as actually both being on a1.

What you can do is to "normalise" entire boards with respect to the symmetry. You need some algorithm for deciding what the normal form is. For example you might decide to first transform the white king into that triangle, then do any remaining transformations that keep the white king there and move the black king into a standard location (I chose the kings because they are guaranteed to be on the board and to be unique). As you note, many transformations will only be valid if there are no pawns left and castling is invalid, which restricts the possibilities substantially anyway.

Along with that normalisation you need to track the operations you did to transform the board into its normal form, so that you can undo the transformation later. For example if you are presenting a sequence of winning moves to the user, it had better be on the real board, not the transformed one.

This normalisation is then useful in conjunction with any existing database of positions that you build up. If you know some property (such as winning moves) of board X, and board Y normalises to board X, then you know that property of board Y as well, subject to undoing the necessary transformations. When adding positions to the database, you should do this based on a normalised board.

I don't think that the underlying representation of the chessboard is particularly important to the usefulness of this technique.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top