5

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.

0

2 Answers 2

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.smallchess.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

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

  1. Use the key to locate an entry in the book using binary search
  2. Read the entry from (2)
  3. Extract the chess move like:

enter image description here

1
  • 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. Commented Mar 16, 2020 at 17:31
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.

1
  • 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. Commented Dec 12, 2020 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.