Sven Schüle wrote:lucasart wrote:and as for your algorithm itself, i don't think it's correct.
Why do you think so?
For each rank those patterns do the following, assuming a1=0 and h1=7:
Code: Select all
For << 17 and >> 15, 7F masks out bit 7 (h-file).
For << 10 and >> 6, F3 masks out bits 0+1 (a/b-file).
For << 15 and >> 17, FE masks out bit 0 (a-file).
For << 6 and >> 10, FC masks out bits 6+7 (g/h-file).
Seems ok to me.
Sven
Ypou're right, I didn'# pay close enough attention to the "bit filters" ensuring no shifting was goind "through" the edge of the board.
I use a less subtile but simple and generic code however, allowing me to generate at once king, knoght and pawn attacks.
Code: Select all
static void init_KNP_attacks()
{
const int K_dir[8][2] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
const int N_dir[8][2] = {{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
const int P_dir[NB_COLOR][2][2] = { {{1,-1},{1,1}}, {{-1,-1},{-1,1}} };
for (int sq = A1; sq <= H8; sq++) {
int r = rank(sq), f = file(sq);
KAttacks[sq] = NAttacks[sq] = 0ULL;
for (int i = 0; i < 8; i++) {
int dr = K_dir[i][0], df = K_dir[i][1];
if (rank_file_ok(r+dr,f+df))
set_bit(&KAttacks[sq], square(r+dr,f+df));
dr = N_dir[i][0], df = N_dir[i][1];
if (rank_file_ok(r+dr,f+df))
set_bit(&NAttacks[sq], square(r+dr,f+df));
}
PAttacks[White][sq] = PAttacks[Black][sq] = 0ULL;
for (int i = 0; i < 2; i++)
for (int c = White; c <= Black; c++) {
int dr = P_dir[c][i][0], df = P_dir[c][i][1];
if (rank_file_ok(r+dr,f+df))
set_bit(&PAttacks[c][sq], square(r+dr,f+df));
}
}
}
I might rewrite my code to use the same tricks. They can also be turned into a more generic function allowing king knoght and pawn attackes to be generated.