User Tools

Site Tools


chess:programming:magic_bitboards:calculate_magic_numbers

This is an old revision of the document!


Chess - Programming - Magic BitBoards - Calculate Magic Numbers

To start off, the following functions are needed:

uint64_t blockermask_rook   (int square);
uint64_t blockermask_bishop (int square);
uint64_t moveboard_rook     (int square, uint64_t blockerboard);
uint64_t moveboard_bishop   (int square, uint64_t blockerboard);
uint64_t blockerboard       (int index, uint64_t blockermask);

NOTE: These functions do not need to be particularly fast, because they will only be used when looking for magic numbers; and once at program startup before the magic numbers are used.

  • Therefore any technique can be used in these functions.

/* Example, Rook on e4:
 *  
 *    The blocker mask        A blocker board         The move board
 *    0 0 0 0 0 0 0 0         0 0 0 0 0 0 0 0         0 0 0 0 0 0 0 0 
 *    0 0 0 0 1 0 0 0         0 0 0 0 1 0 0 0         0 0 0 0 0 0 0 0 
 *    0 0 0 0 1 0 0 0         0 0 0 0 0 0 0 0         0 0 0 0 0 0 0 0 
 *    0 0 0 0 1 0 0 0         0 0 0 0 1 0 0 0         0 0 0 0 1 0 0 0 
 *    0 1 1 1 0 1 1 0         0 1 1 0 0 0 0 0         0 0 1 1 0 1 1 1 
 *    0 0 0 0 1 0 0 0         0 0 0 0 0 0 0 0         0 0 0 0 1 0 0 0 
 *    0 0 0 0 1 0 0 0         0 0 0 0 1 0 0 0         0 0 0 0 1 0 0 0 
 *    0 0 0 0 0 0 0 0         0 0 0 0 0 0 0 0         0 0 0 0 0 0 0 0 
 */

The Blocker Mask is all of the squares that can be occupied and block a piece from moving further.

  • 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 combination.
  • In this case, there are 10 ones, so there are 2^10 (1024) possible permutations of pieces that can block an e4 rook.

The Blocker Board is one of these permutations.

  • In this example, there are pieces on b4, c4, e2, e5, and e7.
  • These are enemy and friendly pieces.
  • A Blocker Board is always a subset of the blocker mask (it need not show pieces on other squares.
    • blockers = occupancy & blockermask.

The Move Board is the resulting available moves for your piece, for a given Blocker Board.

  • 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).
chess/programming/magic_bitboards/calculate_magic_numbers.1635373651.txt.gz · Last modified: 2021/10/27 22:27 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki