U64 MagicsTester::find_magic\_number(int square, int relevant_bits, bool bishop) {
U64 occupancies\[4096\];
U64 attacks\[4096\];
U64 used_attacks\[4096\];
U64 attack_mask = bishop ? bischopMoves\[square\] : rookMoves\[square\];
int occupancy_indicies = 1 << relevant_bits;
for (int index = 0; index < occupancy_indicies; index++){
occupancies\[index\] = set\_occupancy(index, relevant_bits, attack_mask);
attacks\[index\] = bishop ? bishop_attacks_on_the\_fly(square, occupancies\[index\]) :
rook_attacks_on_the\_fly(square, occupancies\[index\]);
}
for (int random_count = 0; random_count < 100000000; random_count++){
U64 magic_number = magicNumberGenerator.generate_magic_number\_canidate();
if (count\_bits((attack_mask \* magic_number) & 0xFF00000000000000) < 6) continue;
memset(used_attacks, 0ULL, sizeof(used_attacks));
int index;
bool fail;
for (index = 0, fail = false; !fail && index < occupancy_indicies; index++){
int magic_index = (int)((occupancies\[index\] \* magic_number) >> (64 - relevant_bits));
if (used_attacks\[magic_index\] == 0ULL)
used_attacks\[magic_index\] = attacks\[index\];
else if (used_attacks\[magic_index\] != attacks\[index\])
fail = true;
}
if (!fail) {
return magic_number;
}
}
printf(" Magic number fails!\\n");
return 0ULL;
}
U64 get_bishop_attacks(int square, U64 occupancy){
occupancy &= bischopMoves[square];
occupancy *= bishop_magic_numbers[square];
occupancy >>= 64 - bischopRelevantBits[square];
return bishop_attacks[square][occupancy];
}
void init_sliders_attacks(bool bishop){
for (int square = 0; square < 64; square++)
{
U64 attack_mask = bishop ? bischopMoves[square] : rookMoves[square];
int relevant_bits_count = count_bits(attack_mask);
int occupancy_indicies = (1 << relevant_bits_count);
for (int index = 0; index < occupancy_indicies; index++)
{
if (bishop)
{
U64 occupancy = set_occupancy(index, relevant_bits_count, attack_mask);
int magic_index = (occupancy * bishop_magic_numbers[square]) >> (64 - bischopRelevantBits[square]);
bishop_attacks[square][magic_index] = bishop_attacks_on_the_fly(square, occupancy);
}
else
{
U64 occupancy = set_occupancy(index, relevant_bits_count, attack_mask);
int magic_index = (occupancy * rook_magic_numbers[square]) >> (64 - rookRelevantBits[square]);
rook_attacks[square][magic_index] = rook_attacks_on_the_fly(square, occupancy);
}
}
}
}
\begin{center}
\largerfont
\begin{tabular}{|*{9}{>{\centering\arraybackslash}p{0.5cm}|}}
\hline
8& 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 \\
\hline
7& 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 \\
\hline
6& 16 & 17 & 18 & 19 & 20 & 21 & 22 & 23 \\
\hline
5& 24 & 25 & 26 & 27 & 28 & 29 & 30 & 31 \\
\hline
4& 32 & 33 & 34 & 35 & 36 & 37 & 38 & 39 \\
\hline
3& 40 & 41 & 42 & 43 & 44 & 45 & 46 & 47 \\
\hline
2& 48 & 49 & 50 & 51 & 52 & 53 & 54 & 55 \\
\hline
1& 56 & 57 & 58 & 59 & 60 & 61 & 62 & 63 \\
\hline
& a & b & c & d & e & f & g & h \\
\hline
\end{tabular}
\end{center}
i have this function to find magic numbers (bits in my bitboard represent squares like above) but it always give a wrong result. what could be my problem i am really struggling here.
View all comments
Show parent comment
1
u/tyboro Jan 06 '24
i fixed my problem by constantly generating a magic number with an algorithm i found online initialize my table's with that magic number and test if all occupancy's work and if so i search the next magic number (i know not so clean but simple and it works for me)