2

I'm working with 24-bit bitboards in Java representing a game board of the following form:

00 01 02 03 04
05 06 07 08 09
10 11 XX 12 13
14 15 16 17 18
19 20 21 22 23

Note that the board has a hole in the middle indicated by the 'XX'. I've generated a list of legal bitboards, but because the board has the symmetries of a square I can throw away a big amount of bitboards who are already represented by their symmetry cousins in the list.

To check these symmetries I need functions that are able to rotate the board by 90, 180 and 270 degrees and mirror horizontally, vertically and diagonally (over both diagonals). Obviously I would like to make use of bit operations, but this is where I'm stuck. I've found some information on how to do this for chess boards, but I can't wrap my head around the concept - let alone how to apply it to my own board situation.

Can anyone show me, with some explanation, how to efficiently rotate/mirror bitboards?

Arthelais
  • 606
  • 1
  • 7
  • 17
  • possible duplicate of [How do you rotate a two dimensional array?](http://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array) – David Titarenco Jul 17 '14 at 21:37
  • 2
    I'm specifically looking for a way to do this efficiently with bit operations to minimalize required CPU for large amounts of calculations. – Arthelais Jul 17 '14 at 21:53

2 Answers2

2

I found the compilation of the most efficient methods here:

http://chessprogramming.wikispaces.com/Flipping+Mirroring+and+Rotating.

For example, vertical flip of 64 bit board is only 13 operations, and without any if statements. Rotation can be accomplished through combinations of flipping and mirroring.

Some of these algorithms look like magic the first time you look at them. To really understand them, it helps to dump the state of the board after each operation.

FarmerBob
  • 1,314
  • 8
  • 11
0

I hadn't come across bitboards until I read this question. Interesting concept!

I wonder if you are misunderstanding something. I suspect you are not expected to dynamically generate rotate algorithms of bitboards depending on their topology, I would expect you to hard-code rotate algorithms.

So I would code a rotate as something like:

00 -> 04
01 -> 09
02 -> 13
03 -> 18
04 -> 23
...

You could then code this in a method/routine that takes a bitboard as a parameter, performs the transform and returns the result. You may find that some of the transform could be encoded using binary logic operators but that should be let as an optimization of your algorithm rather than a fundamental concept.

There is a good example of the use of bitboards here where a clearly knowledgeable developer is discussing bitboard use in checkers games.

Alternatively you could choose a different representation such as perhaps:

00 10 20 30 40
01 11 21 31 41
02 12 XX 32 42
03 13 23 33 43
04 14 24 34 44

with the obviously much simpler transforms for flip and rotate and a very reasonable tradeoff in bit use.

The representation you choose really depends on the kind of moves you plan to make in your game and which ones should be easy to calculate.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213