Answers.Body: <p>I have found the problem: my board representation was different and while my ranks are also from 0-7, but they are counted from the black side, while in the polyglot encoding, they are counted from the white side (which makes sense).</p>
  <p>So I had to invert my ranks like so:</p>
  <pre><code>unsigned short fromFile = (moveKey&gt;&gt;6)&amp;7,
                            fromRank = 7 - ((moveKey&gt;&gt;9)&amp;7),
                            toFile = (moveKey&gt;&gt;0)&amp;7,
                            toRank = 7 - ((moveKey&gt;&gt;3)&amp;7);
  </code></pre>
  <p>Files remain unchanged. I am now getting reasonable output:</p>
  <pre><code>Finding moves for board key: 823c9b50fd114196
  Move found: c7c5
  Move found: e7e5
  Move found: c7c6
  Move found: e7e6
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-07T15:13:32.317Z
Answers.LastActivityDate: 2021-02-07T15:13:32.317Z
Answers.UserDisplayName: Gtomika
Body: <p>I am in the process of adding an opening book to my C++ engine and I hit a problem. Here is what I have:</p>
  <ul>
  <li>The Perfect2021.bin opening book that I would like to use.</li>
  <li>The function that creates the polyglot hash for the board. I tested this with many boards, and it is correct.</li>
  </ul>
  <p>Now I would like to retrieve and for now only print all the moves my engine can make in a position, according to the book. I tried it with the following method:</p>
  <pre><code>move opening_book::getBookMove(const chessboard&amp; board, const game_information&amp; info) {
          uint64 boardKey = polyKeyFromBoard(board, info); //get key for board
          std::cout &lt;&lt; &quot;Finding moves for board key: &quot; &lt;&lt; std::hex &lt;&lt; boardKey &lt;&lt; std::dec &lt;&lt; std::endl;
          unsigned short moveKey;
          //list moves from the book
          for(polyglot_book_entry* entry = entries; entry &lt; entries + entriesNumber; entry++) {
              //apparently I need to swap bits for these
              if(boardKey == swap_uint64(entry-&gt;boardKey)) { //found the board in the book
                  moveKey = swap_uint16(entry-&gt;move);
                  unsigned short fromFile = (moveKey&gt;&gt;6)&amp;7, fromRank = (moveKey&gt;&gt;9)&amp;7,
                                 toFile = (moveKey&gt;&gt;0)&amp;7, toRank = (moveKey&gt;&gt;3)&amp;7;
                  unsigned int fromSquare = (8 * fromRank) + fromFile,
                               toSquare = (8 * toRank) + toFile;
                  std::cout &lt;&lt; &quot;Move found: &quot; &lt;&lt; createSquareName(fromSquare) &lt;&lt; createSquareName(toSquare) &lt;&lt; std::endl;
              }
          }
          return move(); //not returning actual move right now
      }
  </code></pre>
  <p>But when I for example make my engine play black and I open the game on white with the move <em>e2e4</em> then the following is printed upon the engine checking the opening book:</p>
  <pre><code>Finding moves for board key: 823c9b50fd114196
  Move found: c2c4
  Move found: e2e4
  Move found: c2c3
  Move found: e2e3
  </code></pre>
  <p>The board key is correct. It is one of the test cases on <a href="http://hgm.nubati.net/book_format.html" rel="nofollow noreferrer">this</a> site. However, as you can see the moves that are found seem to be for white.</p>
  <p>Can someone please help me out? Thanks.</p>
  
Tags: <engines><programming>
Title: Polyglot opening book: getting incorrect moves
ThreadText: # Polyglot opening book: getting incorrect moves
  
  (Asked by: username_0 on 2021-02-07)
  
  I am in the process of adding an opening book to my C++ engine and I hit a problem. Here is what I have:
  
  * The Perfect2021.bin opening book that I would like to use.
  * The function that creates the polyglot hash for the board. I tested this with many boards, and it is correct.
  
  Now I would like to retrieve and for now only print all the moves my engine can make in a position, according to the book. I tried it with the following method:
  
  ```
  move opening_book::getBookMove(const chessboard& board, const game_information& info) {
          uint64 boardKey = polyKeyFromBoard(board, info); //get key for board
          std::cout << "Finding moves for board key: " << std::hex << boardKey << std::dec << std::endl;
          unsigned short moveKey;
          //list moves from the book
          for(polyglot_book_entry* entry = entries; entry < entries + entriesNumber; entry++) {
              //apparently I need to swap bits for these
              if(boardKey == swap_uint64(entry->boardKey)) { //found the board in the book
                  moveKey = swap_uint16(entry->move);
                  unsigned short fromFile = (moveKey>>6)&7, fromRank = (moveKey>>9)&7,
                                 toFile = (moveKey>>0)&7, toRank = (moveKey>>3)&7;
                  unsigned int fromSquare = (8 * fromRank) + fromFile,
                               toSquare = (8 * toRank) + toFile;
                  std::cout << "Move found: " << createSquareName(fromSquare) << createSquareName(toSquare) << std::endl;
              }
          }
          return move(); //not returning actual move right now
      }
  
  ```
  
  But when I for example make my engine play black and I open the game on white with the move *e2e4* then the following is printed upon the engine checking the opening book:
  
  ```
  Finding moves for board key: 823c9b50fd114196
  Move found: c2c4
  Move found: e2e4
  Move found: c2c3
  Move found: e2e3
  
  ```
  
  The board key is correct. It is one of the test cases on [this](http://hgm.nubati.net/book_format.html) site. However, as you can see the moves that are found seem to be for white.
  
  Can someone please help me out? Thanks.
  
  **Tags:** engines, programming
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2021-02-07. Score: 2
  
  I have found the problem: my board representation was different and while my ranks are also from 0-7, but they are counted from the black side, while in the polyglot encoding, they are counted from the white side (which makes sense).
  
  So I had to invert my ranks like so:
  
  ```
  unsigned short fromFile = (moveKey>>6)&7,
                            fromRank = 7 - ((moveKey>>9)&7),
                            toFile = (moveKey>>0)&7,
                            toRank = 7 - ((moveKey>>3)&7);
  
  ```
  
  Files remain unchanged. I am now getting reasonable output:
  
  ```
  Finding moves for board key: 823c9b50fd114196
  Move found: c7c5
  Move found: e7e5
  Move found: c7c6
  Move found: e7e6
  
  ```
  
  
  ---
  
  