+
+/// Book::probe() gets a book move for a given position. Returns MOVE_NONE
+/// if no book move is found. If findBest is true then returns always the
+/// highest rated move otherwise chooses randomly based on the move score.
+
+Move Book::probe(const Position& pos, bool findBest) {
+
+ if (!bookSize || !bookFile.is_open())
+ return MOVE_NONE;
+
+ BookEntry entry;
+ unsigned scoresSum = 0, bestScore = 0, bookMove = 0;
+ uint64_t key = book_key(pos);
+ int idx = first_entry(key) - 1;
+
+ // Choose a book move among the possible moves for the given position
+ while (++idx < bookSize && (entry = read_entry(idx), entry.key == key))
+ {
+ scoresSum += entry.count;
+
+ // Choose book move according to its score. If a move has a very
+ // high score it has higher probability to be choosen than a move
+ // with lower score. Note that first entry is always chosen.
+ if ( RKiss.rand<unsigned>() % scoresSum < entry.count
+ || (findBest && entry.count > bestScore))
+ bookMove = entry.move;
+
+ if (entry.count > bestScore)
+ bestScore = entry.count;