Move Book::get_move(const Position &pos) const { if(this->is_open()) { int bestMove = 0, bestScore = 0, move, score; uint64_t key = book_key(pos); BookEntry entry; for(int i = this->find_key(key); i < bookSize; i++) { this->read_entry(entry, i); if(entry.key != key) break; move = entry.move; score = entry.count; assert(score > 0); bestScore += score; if(int(genrand_int32() % bestScore) < score) bestMove = move; } if(bestMove != 0) { MoveStack moves[256]; int n, j; n = generate_legal_moves(pos, moves); for(j = 0; j < n; j++) if((int(moves[j].move) & 07777) == bestMove) return moves[j].move; } } return MOVE_NONE; }
std::tuple<Move, Value> AperyBook::probe(const Position &pos, const std::string &fname, bool pick_best) { AperyBookEntry entry; uint16_t best = 0; uint32_t sum = 0; Move move = kMoveNone; Key key = book_key(pos); Value min_book_score = static_cast<Value>(static_cast<int>(Options["Min_Book_Score"])); Value score = kValueZero; if (file_name_ != fname && !open(fname.c_str())) return std::make_tuple(kMoveNone, kValueZero); binary_search(key); while (read(reinterpret_cast<char*>(&entry), sizeof(entry)), entry.key == key && good()) { best = std::max(best, entry.count); sum += entry.count; if ( min_book_score <= entry.score && ( (random_() % sum < entry.count) || (pick_best && entry.count == best) ) ) { Square to = to_square(entry.from_to_pro & 0x007fU); int from_raw = (entry.from_to_pro >> 7) & 0x007fU; if (from_raw >= kBoardSquare) { move = move_init(to, to_drop_piece_type(static_cast<Square>(from_raw))); } else { Square from = to_square(from_raw); PieceType pt_from = type_of(pos.square(from)); if (entry.from_to_pro & kPromoted) move = move_init(from, to, pt_from, type_of(pos.square(to)), true); else move = move_init(from, to, pt_from, type_of(pos.square(to)), false); } score = entry.score; } }