In order to represent the state of a 2D board game, I am using bitboards with 16bits unsigned integers. The state is encoded with 1 for piece presence and 0 else.
What is the bitboard way to count the number of pieces having at least one adjacent piece horizontally, vertically or diagonally ?
The algorithm I found (very simplified) is:
total = 0
for each bitIndex in bitscanForward(bitboard)
total += bitPopCount(bitboard & (ADJACENT_MASK << bitIndex))
return total
- bitScanForward function returns index of first bit and set it to 0
- bitPopCount function returns the number of bits
The only constraint is that boards are m rows x m columns with m <= 8.
Is there a way to accomplish that without looping over bits ?