chess:programming:magic_bitboards:calculate_magic_numbers
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
chess:programming:magic_bitboards:calculate_magic_numbers [2021/10/27 22:57] – [Chess - Programming - Magic BitBoards - Calculate Magic Numbers] peter | chess:programming:magic_bitboards:calculate_magic_numbers [2021/10/28 00:00] (current) – [Determine Magic Numbers] peter | ||
---|---|---|---|
Line 40: | Line 40: | ||
* The edge squares do not need to be a part of that, because the piece cannot move further past that square anyway. | * The edge squares do not need to be a part of that, because the piece cannot move further past that square anyway. | ||
- | * The number of 1s in this BitBoard determine how large of a lookup table is needed for this piece & square | + | * The number of 1s in this BitBoard determine how large of a lookup table is needed for this Piece & Square |
* In this case, there are 10 ones, so there are 2^10 (1024) possible permutations of pieces that can block an e4 rook. | * In this case, there are 10 ones, so there are 2^10 (1024) possible permutations of pieces that can block an e4 rook. | ||
Line 48: | Line 48: | ||
* In this example, there are pieces on b4, c4, e2, e5, and e7. | * In this example, there are pieces on b4, c4, e2, e5, and e7. | ||
* These pieces are both enemy and friendly pieces. | * These pieces are both enemy and friendly pieces. | ||
- | * A Blocker Board is always a subset of the blocker mask (it need not show pieces on other squares. | + | * A Blocker Board is always a subset of the blocker mask (it need not show pieces on other squares). |
* blockers = occupancy & blockermask. | * blockers = occupancy & blockermask. | ||
- | The **Move Board** is the resulting available moves for your piece, for a given Blocker Board. | + | The **Move Board** is the resulting available moves for the Piece, for a given Blocker Board. |
- | * This includes possible captures for this piece. | + | * This includes possible captures for this Piece. |
* This also includes capturing own pieces (but these can easily be removed by doing an AND with a NOT of own piece locations). | * This also includes capturing own pieces (but these can easily be removed by doing an AND with a NOT of own piece locations). | ||
Line 60: | Line 60: | ||
---- | ---- | ||
- | ====== Generate a Blocker Mask for all squares | + | ===== Generate a Blocker Mask for all squares ===== |
- | A Blocker Mask is needing | + | A Blocker Mask is needed |
<WRAP info> | <WRAP info> | ||
Line 70: | Line 70: | ||
---- | ---- | ||
- | ====== Generate all possible Blocker Boards for all squares | + | ===== Generate all possible Blocker Boards for all squares ===== |
A Blocker Board is needed to be generate for each square, for both the Rook and Bishop. | A Blocker Board is needed to be generate for each square, for both the Rook and Bishop. | ||
Line 83: | Line 83: | ||
---- | ---- | ||
- | ====== Determine Magic Numbers | + | ===== Determine Magic Numbers ===== |
For each Square/ | For each Square/ | ||
Line 90: | Line 90: | ||
<code cpp> | <code cpp> | ||
- | return | + | magical_index = ((blockerboard*magic) >> (64-bits)); |
</ | </ | ||
<WRAP info> | <WRAP info> | ||
**NOTE: | **NOTE: | ||
+ | |||
+ | * **magic** is the random 64-bit number. | ||
+ | * At this time this is done by trial-and-error, | ||
+ | * It is just luck/ | ||
+ | * Ideally no gaps should be between each index, i.e. a perfect hash. | ||
* This should result in 64 magic rook numbers and 64 magic bishop numbers. | * This should result in 64 magic rook numbers and 64 magic bishop numbers. | ||
+ | * The index returned by the magic formula is always an integer less than the total number of BlockerBoards. | ||
+ | |||
+ | * These indexes will be used to determine where to store each BlockerBoards corresponding MoveBoard within an array. | ||
+ | * The MoveBoard are just stored at the index calculated by these pre-programmed good magics. | ||
+ | |||
+ | * While this example of the Rook at e4 has 1024 BlockerBoards, | ||
+ | * For two different BlockerBoards that have the same MoveBoard, the magic could calculate two different indices or it could calculate the same index for both. | ||
+ | * As long as the MoveBoards are the same, it is okay for the index to collide. | ||
</ | </ | ||
Line 103: | Line 116: | ||
**WARNING: | **WARNING: | ||
- | * For a certain Piece/ | + | * For a certain Piece/ |
</ | </ | ||
Line 113: | Line 126: | ||
All of the Blocker Masks, Blocker Boards, and Move Boards should be initialized. | All of the Blocker Masks, Blocker Boards, and Move Boards should be initialized. | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | * Each BB has a corresponding MoveBoard (MB). | ||
+ | * When searching for magics, each MB is being calculated | ||
+ | * At engine startup, the MBs are recalculated with the slow algorithm and then they are just stored at the index calculated by your pre-programmed good magics. | ||
+ | |||
+ | </ | ||
---- | ---- | ||
Line 124: | Line 146: | ||
<code cpp> | <code cpp> | ||
// Retrieves the Move Board for the given square and occupancy board. | // Retrieves the Move Board for the given square and occupancy board. | ||
- | uint64_t magic_move_rook | + | uint64_t magic_move_rook(int8_t square, uint64_t occupancy) |
{ | { | ||
// Remove occupants that are not in the Blocker Mask for this square. | // Remove occupants that are not in the Blocker Mask for this square. | ||
Line 136: | Line 158: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | * **index** points to the right spot in the moveboard array. | ||
+ | |||
+ | </ | ||
---- | ---- | ||
chess/programming/magic_bitboards/calculate_magic_numbers.1635375464.txt.gz · Last modified: 2021/10/27 22:57 by peter