Answers.Body: <p>It sounds like you have all the tools to do the job, you just need to put it all together.  And yes, you can use the magic numbers you found.</p>
  <p>When your engine initializes (or any time before you begin the search, really) you'll need to pre-compute the move board for every possible permutation of blockers you might encounter during the search.</p>
  <p><strong>Initialization</strong></p>
  <pre><code>for each square...
    ROOK_MASKS[square] = //rook attacks on an empty board
    
    int bits = bitCount(ROOK_MASKS[square]);
    ROOK_SHIFTS[square] = 64 - bits;
  
    int permutations = 1 &lt;&lt; bits
    for each permutation (p)...
      int index = (ROOK_MAGICs[square] * ROOK_MASKS[square]) &gt;&gt;&gt; ROOK_SHIFTS[square];
      long blockers = //the pth permutation of ROOK_MASKS[square]
      ROOK_ATTACKS[square][index] = //rook attacks with blockers
  </code></pre>
  <p>Then when it comes time to generate moves you need to perform a similar operation using the pieces actually on the board</p>
  <p><strong>Move Generation</strong></p>
  <pre><code>  long blockers = ROOK_MASKS[square] &amp; //all the pieces on the board
    int index = (ROOK_MAGICS[square] * blockers) &gt;&gt;&gt; ROOK_SHIFTS[square];
    return ROOK_ATTACKS[square][index];
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-28T13:21:24.570Z
Answers.Text: Thank you for your reply. Since posting this, I have managed to get it work in a slightly different way. When my engine is finished, I will revisit this and see if I can improve it!
Answers.UserDisplayName: Kieran Montgomery
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-25T20:02:31.590Z
Answers.LastActivityDate: 2020-06-25T20:02:31.590Z
Answers.UserDisplayName: Jeff Wilson
Body: <p>I am currently trying to implement magic bitboards into my chess engine. Let's just talk about rooks here. Given a square, what I can currently do is:</p>
  <p>-Calculate the sliding moves on an empty board.</p>
  <ul>
  <li>Generate all blocker boards.</li>
  <li>Calculate the corresponding mve board.</li>
  </ul>
  <p>What I am having trouble with is the using of this information. I have downloaded an array of length 64, populated by &quot;magic numbers&quot; for the rook squares. Can I use this? I can generate a index from this information, but it doesn't correspond to the correct move board. Any ideas? I am happy to share more information and code upon a request.</p>
  
Tags: <engines><programming><rooks><bitboard>
Title: How to store moves for a magic bitboard system?
ThreadText: # How to store moves for a magic bitboard system?
  
  (Asked by: username_0 on 2020-06-22)
  
  I am currently trying to implement magic bitboards into my chess engine. Let's just talk about rooks here. Given a square, what I can currently do is:
  
  -Calculate the sliding moves on an empty board.
  
  * Generate all blocker boards.
  * Calculate the corresponding mve board.
  
  What I am having trouble with is the using of this information. I have downloaded an array of length 64, populated by "magic numbers" for the rook squares. Can I use this? I can generate a index from this information, but it doesn't correspond to the correct move board. Any ideas? I am happy to share more information and code upon a request.
  
  **Tags:** engines, programming, rooks, bitboard
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-06-25. Score: 1
  
  It sounds like you have all the tools to do the job, you just need to put it all together.  And yes, you can use the magic numbers you found.
  When your engine initializes (or any time before you begin the search, really) you'll need to pre-compute the move board for every possible permutation of blockers you might encounter during the search.
  Initialization
  for each square...
    ROOK_MASKS[square] = //rook attacks on an empty board
    
    int bits = bitCount(ROOK_MASKS[square]);
    ROOK_SHIFTS[square] = 64 - bits;
  
    int permutations = 1 << bits
    for each permutation (p)...
      int index = (ROOK_MAGICs[square] * ROOK_MASKS[square]) >>> ROOK_SHIFTS[square];
      long blockers = //the pth permutation of ROOK_MASKS[square]
      ROOK_ATTACKS[square][index] = //rook attacks with blockers
  
  Then when it comes time to generate moves you need to perform a similar operation using the pieces actually on the board
  Move Generation
    long blockers = ROOK_MASKS[square] & //all the pieces on the board
    int index = (ROOK_MAGICS[square] * blockers) >>> ROOK_SHIFTS[square];
    return ROOK_ATTACKS[square][index];
  
  > **Comments:**
  > **username_0**: Thank you for your reply. Since posting this, I have managed to get it work in a slightly different way. When my engine is finished, I will revisit this and see if I can improve it!  
  
  ---
  
  