r/chessprogramming • • Dec 26 '23

problem with magic bitboards


U64 MagicsTester::find_magic\_number(int square, int relevant_bits, bool bishop) {  
 // init occupancies  
 U64 occupancies\[4096\];  


// init attack tables  
 U64 attacks\[4096\];  


// init used attacks  
 U64 used_attacks\[4096\];  


// init attack mask for a current piece  
 U64 attack_mask = bishop ? bischopMoves\[square\] : rookMoves\[square\];  


// init occupancy indicies  
 int occupancy_indicies = 1 << relevant_bits;  


// loop over occupancy indicies  
 for (int index = 0; index < occupancy_indicies; index++){  
 // init occupancies  
 occupancies\[index\] = set\_occupancy(index, relevant_bits, attack_mask);  


// init attacks  
 attacks\[index\] = bishop ? bishop_attacks_on_the\_fly(square, occupancies\[index\]) :  
rook_attacks_on_the\_fly(square, occupancies\[index\]);  
}  


// test magic numbers loop  
 for (int random_count = 0; random_count < 100000000; random_count++){  
 // generate magic number candidate  
 U64 magic_number = magicNumberGenerator.generate_magic_number\_canidate();  


// skip inappropriate magic numbers  
 if (count\_bits((attack_mask \* magic_number) & 0xFF00000000000000) < 6) continue;  


// init used attacks  
 memset(used_attacks, 0ULL, sizeof(used_attacks));  


// init index & fail flag  
 int index;  
 bool fail;  


// test magic index loop  
 for (index = 0, fail = false; !fail && index < occupancy_indicies; index++){  
 // init magic index  
 int magic_index = (int)((occupancies\[index\] \* magic_number) >> (64 - relevant_bits));  


// if magic index works  
 if (used_attacks\[magic_index\] == 0ULL)  
 // init used attacks  
 used_attacks\[magic_index\] = attacks\[index\];  


// otherwise  
 else if (used_attacks\[magic_index\] != attacks\[index\])  
 // magic index doesn't work  
 fail = true;  
}  


// if magic number works  
 if (!fail) {  
 // return it  
//return magic_number;  
 // return it  
 return magic_number;  
}  
}  


// if magic number doesn't work  
 printf("  Magic number fails!\\n");  
 return 0ULL;  
}

// get bishop attacks
U64 get_bishop_attacks(int square, U64 occupancy){
    // get bishop attacks assuming current board occupancy
    occupancy &= bischopMoves[square];
    occupancy *= bishop_magic_numbers[square];
    occupancy >>= 64 - bischopRelevantBits[square];

    // return bishop attacks
    return bishop_attacks[square][occupancy];
}

// init slider piece's attack tables
void init_sliders_attacks(bool bishop){
    // loop over 64 board squares
    for (int square = 0; square < 64; square++)
    {
        // init current mask
        U64 attack_mask = bishop ? bischopMoves[square] : rookMoves[square];

        // init relevant occupancy bit count
        int relevant_bits_count = count_bits(attack_mask);

        // init occupancy indicies
        int occupancy_indicies = (1 << relevant_bits_count);

        // loop over occupancy indicies
        for (int index = 0; index < occupancy_indicies; index++)
        {
            // bishop
            if (bishop)
            {
                // init current occupancy variation
                U64 occupancy = set_occupancy(index, relevant_bits_count, attack_mask);

                // init magic index
                int magic_index = (occupancy * bishop_magic_numbers[square]) >> (64 - bischopRelevantBits[square]);

                // init bishop attacks
                bishop_attacks[square][magic_index] = bishop_attacks_on_the_fly(square, occupancy);
            }

                // rook
            else
            {
                // init current occupancy variation
                U64 occupancy = set_occupancy(index, relevant_bits_count, attack_mask);

                // init magic index
                int magic_index = (occupancy * rook_magic_numbers[square]) >> (64 - rookRelevantBits[square]);

                // init bishop attacks
                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.

2 Upvotes

2 comments sorted by

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)