Answers.Body: <p>I have every reason to believe your approach is wrong and will lead to disaster. I'm saying it as someone who make softwares to work on Polyglot books (www.smallchess.com).</p>
  
  <p>You are <strong>strongly</strong> advised <strong>NOT</strong> to do all the hard works like what I did before. Just wrap the C++ Polyglot code in your C#! Your time should be on your C# engine, not this.</p>
  
  <p>Polyglot, although open source is not something you would like to port to C# unless you are super motivated. Why? Polyglot has bunch of hash keys. Screenshot on my source code:</p>
  
  <p><a href="https://i.sstatic.net/11hKh.png" rel="noreferrer"><img src="https://i.sstatic.net/11hKh.png" alt="enter image description here"></a></p>
  
  <p>There was never any good documentation on Polyglot, it existed mainly in source code. Today, not many people really know how that does (including myself). I understand only part of it, by reading source code.</p>
  
  <p>That was the comment I wrote to myself when I started using Polyglot many years ago:</p>
  
  <pre><code>    /*
       * A PolyGlot book move is encoded as follows:
       *
       * bit  0- 5: destination square (from 0 to 63)
       * bit  6-11: origin square (from 0 to 63)
       * bit 12-13-14: promotion piece (from KNIGHT == 1 to QUEEN == 4)
       *
       * Castling moves follow "king captures rook" representation. So in case
       * book move is a promotion we have to convert to our representation, in
       * all other cases we can directly compare with a Move after having
       * masked out special Move's flags that are not supported by PolyGlot.
       */
  </code></pre>
  
  <p>Polyglot works like this:</p>
  
  <ol>
  <li>Given a position, convert to a hash key. I have my screenshot for the conversion</li>
  </ol>
  
  <p><a href="https://i.sstatic.net/DT1YO.png" rel="noreferrer"><img src="https://i.sstatic.net/DT1YO.png" alt="enter image description here"></a></p>
  
  <ol start="2">
  <li>Use the key to locate an entry in the book using binary search</li>
  <li>Read the entry from (2)</li>
  <li>Extract the chess move like:</li>
  </ol>
  
  <p><a href="https://i.sstatic.net/nMsmp.png" rel="noreferrer"><img src="https://i.sstatic.net/nMsmp.png" alt="enter image description here"></a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-16T17:31:47.173Z
Answers.Text: Hi thanks for the reply. I don't know c++. so that's why I wanted information on the data structure of the bin files, so I can read them and use them like that, compiled  as they are, inside my code. So please let me know if you can provide me with info on the bin file data structure for the opening book files , or the link where you read and understood part of the code then.
Answers.UserDisplayName: Jorge Acuna
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-15T16:24:13.167Z
Answers.LastActivityDate: 2020-03-15T16:24:13.167Z
Answers.UserDisplayName: SmallChess
Answers.Body: <p>The binary file consists of a number of 16 byte entries, formatted as follows first 8 bytes the position hash, next 2 bytes encode the move as mentioned in the previous post, the next 2 are the move weights and the final 4 are the move learning.
  The main problem you'll have is matching the position hash you generate with that used in  the book, so you'll need to use the same keys as in the polyglot source code to match up, or generate the book yourself with your own keys.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-12T15:56:13.000Z
Answers.Text: Hi, thanks for the reply. I have never worked with bits before, so it will be a learning curve for sure! But, I do agree I would have to either pretty much implement the same system polyglot uses to create and read the opening books, or generate my own book. I would rather not having to implement all the move and board classes just so that I can convert the extracted move in something that I could use, but it seems the only way if I want to use those opening books.
Answers.UserDisplayName: Jorge Acuna
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-11T11:06:53.527Z
Answers.LastActivityDate: 2020-12-11T11:06:53.527Z
Answers.UserDisplayName: Mark Lloyd
Body: <p>I previously asked how to use polyglot in c# with a chess engine. My question then was not accurate. What property(ies) of the bin file object do I need to do the following:</p>
  <p>I will parse a FEN string, then, I will access a property of this bin file that will allow me to generate a FEN string that's hopefully linked to a move, whether this move is in a PGN string of a game, or saved as a specific data struct.</p>
  <p>Then I will compare my parsed string with such FEN string obtained by accessing a property of the bin file object.</p>
  <p>Then, when I find a match, I will use another method to obtain the next immediate move, which is the one the computer should do, according to the game from which the move was obtained after finding a FEN matching the one I parsed in the first place.</p>
  <p>So, I guess a pseudo code would look like:</p>
  <pre><code> string extractedMove;
   string parsedFEN = &quot;...&quot;;
   object bookBytes = File.Read(pathofthebookfile);
  
    string GetFENOutOfBook (string FENvalue)
  { 
     return  bookBytes.Find(FENproperty/Fenkey =&gt; FENvalue);
  }
  
    public void GetMove ()
  {
     extractedMove = bookBytes.moves[GetFENOutOfBook(parsedFEN)].ToString();
  }
  </code></pre>
  <p>I just need to know what properties I need to work with of the polyglot book bin file so I can create the logic to extract the move.</p>
  
Tags: <opening><programming><books><polyglot>
Title: How to use Polyglot opening book bin file
ThreadText: # How to use Polyglot opening book bin file
  
  (Asked by: username_0 on 2020-03-15)
  
  I previously asked how to use polyglot in c# with a chess engine. My question then was not accurate. What property(ies) of the bin file object do I need to do the following:
  
  I will parse a FEN string, then, I will access a property of this bin file that will allow me to generate a FEN string that's hopefully linked to a move, whether this move is in a PGN string of a game, or saved as a specific data struct.
  
  Then I will compare my parsed string with such FEN string obtained by accessing a property of the bin file object.
  
  Then, when I find a match, I will use another method to obtain the next immediate move, which is the one the computer should do, according to the game from which the move was obtained after finding a FEN matching the one I parsed in the first place.
  
  So, I guess a pseudo code would look like:
  
  ```
   string extractedMove;
   string parsedFEN = "...";
   object bookBytes = File.Read(pathofthebookfile);
  
    string GetFENOutOfBook (string FENvalue)
  { 
     return  bookBytes.Find(FENproperty/Fenkey => FENvalue);
  }
  
    public void GetMove ()
  {
     extractedMove = bookBytes.moves[GetFENOutOfBook(parsedFEN)].ToString();
  }
  
  ```
  
  I just need to know what properties I need to work with of the polyglot book bin file so I can create the logic to extract the move.
  
  **Tags:** opening, programming, books, polyglot
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2020-03-15. Score: 7
  
  I have every reason to believe your approach is wrong and will lead to disaster. I'm saying it as someone who make softwares to work on Polyglot books (www.username_1.com).
  
  You are **strongly** advised **NOT** to do all the hard works like what I did before. Just wrap the C++ Polyglot code in your C#! Your time should be on your C# engine, not this.
  
  Polyglot, although open source is not something you would like to port to C# unless you are super motivated. Why? Polyglot has bunch of hash keys. Screenshot on my source code:
  
  [![enter image description here](https://i.sstatic.net/11hKh.png)](https://i.sstatic.net/11hKh.png)
  
  There was never any good documentation on Polyglot, it existed mainly in source code. Today, not many people really know how that does (including myself). I understand only part of it, by reading source code.
  
  That was the comment I wrote to myself when I started using Polyglot many years ago:
  
  ```
      /*
       * A PolyGlot book move is encoded as follows:
       *
       * bit  0- 5: destination square (from 0 to 63)
       * bit  6-11: origin square (from 0 to 63)
       * bit 12-13-14: promotion piece (from KNIGHT == 1 to QUEEN == 4)
       *
       * Castling moves follow "king captures rook" representation. So in case
       * book move is a promotion we have to convert to our representation, in
       * all other cases we can directly compare with a Move after having
       * masked out special Move's flags that are not supported by PolyGlot.
       */
  
  ```
  
  Polyglot works like this:
  
  1. Given a position, convert to a hash key. I have my screenshot for the conversion
  
  [![enter image description here](https://i.sstatic.net/DT1YO.png)](https://i.sstatic.net/DT1YO.png)
  
  2. Use the key to locate an entry in the book using binary search
  3. Read the entry from (2)
  4. Extract the chess move like:
  
  [![enter image description here](https://i.sstatic.net/nMsmp.png)](https://i.sstatic.net/nMsmp.png)
  
  > **Comments:**
  > **username_0**: Hi thanks for the reply. I don't know c++. so that's why I wanted information on the data structure of the bin files, so I can read them and use them like that, compiled  as they are, inside my code. So please let me know if you can provide me with info on the bin file data structure for the opening book files , or the link where you read and understood part of the code then.  
  
  ---
  
  ### Answer by username_2 on 2020-12-11. Score: 1
  
  The binary file consists of a number of 16 byte entries, formatted as follows first 8 bytes the position hash, next 2 bytes encode the move as mentioned in the previous post, the next 2 are the move weights and the final 4 are the move learning.
  The main problem you'll have is matching the position hash you generate with that used in the book, so you'll need to use the same keys as in the polyglot source code to match up, or generate the book yourself with your own keys.
  
  > **Comments:**
  > **username_0**: Hi, thanks for the reply. I have never worked with bits before, so it will be a learning curve for sure! But, I do agree I would have to either pretty much implement the same system polyglot uses to create and read the opening books, or generate my own book. I would rather not having to implement all the move and board classes just so that I can convert the extracted move in something that I could use, but it seems the only way if I want to use those opening books.  
  
  ---
  
  