Reputation: 3
Recently, I've been studying magic bit boards in the hope of trying to develop my own Chess engine. I've come across the concept of bit boards. One of the helpful topics was from the site http://www.rivalchess.com/magic-bitboards/ but the example given there are only for Rook and Bishop movement. Is there by chance anybody who could share the concept to generate the occupancy mask for the Queen? I paste here the code that was used for Rook and Bishop:
void generateOccupancyMasks()
{
int i, bitRef;
uint64_t mask;
for (bitRef=0; bitRef<=63; bitRef++)
{
mask = 0;
for (i=bitRef+8; i<=55; i+=8) mask |= (((uint64_t)1) << i);
for (i=bitRef-8; i>=8; i-=8) mask |= (((uint64_t)1) << i);
for (i=bitRef+1; i%8!=7 && i%8!=0 ; i++) mask |= (((uint64_t)1) << i);
for (i=bitRef-1; i%8!=7 && i%8!=0 && i>=0; i--) mask |= (((uint64_t)1) << i);
occupancyMaskRook[bitRef] = mask;
mask = 0;
for (i=bitRef+9; i%8!=7 && i%8!=0 && i<=55; i+=9) mask |= (((uint64_t)1) << i);
for (i=bitRef-9; i%8!=7 && i%8!=0 && i>=8; i-=9) mask |= (((uint64_t)1) << i);
for (i=bitRef+7; i%8!=7 && i%8!=0 && i<=55; i+=7) mask |= (((uint64_t)1) << i);
for (i=bitRef-7; i%8!=7 && i%8!=0 && i>=8; i-=7) mask |= (((uint64_t)1) << i);
occupancyMaskBishop[bitRef] = mask;
}
}
Upvotes: 0
Views: 441
Reputation: 16540
The posted code contains some errors in the logic for generating the masks
The following code is for examining the masks.
I have also included some example bad masks.
#include <stdio.h>
#include <stdint.h>
uint64_t occupancyMaskRook[64];
uint64_t occupancyMaskBishop[64];
void generateOccupancyMasks()
{
int i;
int bitRef;
uint64_t mask;
for (bitRef=0; bitRef<=63; bitRef++)
{
mask = 0;
for (i=bitRef+8; i<=55; i+=8) mask |= (((uint64_t)1) << i);
for (i=bitRef-8; i>=8; i-=8) mask |= (((uint64_t)1) << i);
for (i=bitRef+1; i%8!=7 && i%8!=0 ; i++) mask |= (((uint64_t)1) << i);
for (i=bitRef-1; i%8!=7 && i%8!=0; i--) mask |= (((uint64_t)1) << i);
occupancyMaskRook[bitRef] = mask;
mask = 0;
for (i=bitRef+9; i%8!=7 && i%8!=0 && i<=55; i+=9) mask |= (((uint64_t)1) << i);
for (i=bitRef-9; i%8!=7 && i%8!=0 && i>=8; i-=9) mask |= (((uint64_t)1) << i);
for (i=bitRef+7; i%8!=7 && i%8!=0 && i<=55; i+=7) mask |= (((uint64_t)1) << i);
for (i=bitRef-7; i%8!=7 && i%8!=0 && i>=8; i-=7) mask |= (((uint64_t)1) << i);
occupancyMaskBishop[bitRef] = mask;
}
printf( "\n the possible Rook masks\n");
for( bitRef=0; bitRef < 64; bitRef++ )
{
for( i = 0; i<8; i++ )
{
for( int j=0; j<8; j++ )
{
putchar( occupancyMaskRook[bitRef] & (((uint64_t)1) << (i*8+j))? '1' : '0');
}
putchar( '\n' );
}
puts( "\n" );
}
printf( "\n the possible bishop masks\n");
for( bitRef=0; bitRef < 64; bitRef++ )
{
for( i = 0; i<8; i++ )
{
for( int j=0; j<8; j++ )
{
putchar( occupancyMaskBishop[bitRef] & (((uint64_t)1) << (i*8+j))? '1' : '0');
}
putchar( '\n' );
}
puts("\n");
}
}
int main( void )
{
generateOccupancyMasks();
return 0;
}
the following bad mask is for a bishop in row 6, column 1
Note the missing coverage in the row 8, column 3 and the missing coverage in row 1, column 6
00000000
00001000
00010000
00100000
01000000
00000000
01000000
00000000
The above is just an example, there are LOTS more bad masks
Upvotes: 0
Reputation: 30136
I think that you can simply take the bit-wise OR'ing of these two masks:
for (bitRef=0; bitRef<=63; bitRef++)
{
occupancyMaskQueen[bitRef] = occupancyMaskRook[bitRef] | occupancyMaskBishop[bitRef];
}
Upvotes: 5