MOBMAT wrote: ↑Sun Aug 11, 2019 11:56 pm
I want to be able to identify a pattern of 4 pieces forming a square anywhere on a board, such as:
Code: Select all
- - - - - - -
- x x - - - -
- x x - - - -
- - - - - - -
- - - - - - -
The board could be any size. The pattern can occur anywhere on the board, so, depending on the size of the board, there are a lot of possible occurrences of the pattern.
one way to find the pattern is to create an array of every possible pattern and then loop through them, comparing them against the board. This is very inefficient.
I haven't been able to locate any code that can apply this test across an entire bitboard. Does anyone have a solution they would like to share?
The requirement is not yet clear to me. Are you talking about four pieces of possibly different types and colors? That would make sense but then the scope is not to find four set bits in one bitboard (which is a trivial task with two nested loops) but only slightly more complex (and does not even require loops but more preparation). Example: you want to find out whether the current board has any occurrence of
- a white pawn
- blocked by a black pawn ("north")
- with another black pawn to the right of the white pawn ("east")
- and a white knight north-east to the white pawn.
To solve a problem like in this example I would
- define XSize and YSize as board dimensions,
- provide code that returns four bitboards bb, bbNorth, bbEast, bbNorthEast derived from the four piece types and colors given in the pattern,
- prepare those four bitboards such that subsequent shifting will not include "wrong" bits (e.g. mask out file A and/or rank 1 appropriately),
- combine those four prepared bitboards into one by
Code: Select all
bb & (bbNorth >> YSize) & (bbEast >> 1) & (bbNorthEast >> (YSize+1))
- and then test whether the resulting bitboard is non-zero (if it is then you have a match).