6 #include "board/genericmove.h"
15 * \brief A collection of opening moves for chess.
17 * OpeningBook is a container (binary tree) class for opening moves that
18 * can be played by the GUI. When the game goes "out of book", control
19 * of the game is transferred to the players.
21 * The opening book can be stored externally in a binary file. When it's needed,
22 * it is loaded in memory, and positions can be found quickly by searching
23 * the book for Zobrist keys that match the current board position.
25 class LIB_EXPORT OpeningBook
28 /*! Destroys the opening book. */
29 virtual ~OpeningBook() {}
34 * \param pgn The game to import.
35 * \param maxMoves The maximum number of halfmoves that
38 * Returns the number of moves imported.
40 int import(const PgnGame
& pgn
, int maxMoves
);
42 * Imports PGN games from a stream.
44 * \param in The PGN stream that contains the games.
45 * \param maxMoves The maximum number of halfmoves per game
46 * that can be imported.
48 * Returns the number of moves imported.
50 int import(PgnStream
& in
, int maxMoves
);
53 * Returns a move that can be played in a position where the
54 * Zobrist key is \a key.
56 * If no matching moves are found, an empty (illegal) move is
59 * If there are multiple matches, a random, weighted move is
60 * returned. Popular moves have a higher probablity of being
61 * selected than unpopular ones.
63 Chess::GenericMove
move(quint64 key
) const;
66 * Reads a book from \a filename.
67 * Returns true if successfull.
69 bool read(const QString
& filename
);
72 * Writes the book to \a filename.
73 * Returns true if successfull.
75 bool write(const QString
& filename
) const;
79 friend LIB_EXPORT QDataStream
& operator>>(QDataStream
& in
, OpeningBook
* book
);
80 friend LIB_EXPORT QDataStream
& operator<<(QDataStream
& out
, const OpeningBook
* book
);
83 * \brief An entry in the opening book.
85 * \note Each entry is paired with a Zobrist key.
86 * \note The book file may not use the same structure
92 Chess::GenericMove move
;
94 * A weight or score, usually based on popularity
95 * of the move. The higher the weight, the more
96 * likely the move will be played.
101 /*! The type of binary tree. */
102 typedef QMultiMap
<quint64
, Entry
> Map
;
105 /*! Adds a new entry to the book. */
106 void addEntry(const Entry
& entry
, quint64 key
);
109 * Reads a new book entry from \a in.
111 * The implementation must call addEntry() to add the
114 virtual void readEntry(QDataStream
& in
) = 0;
116 /*! Writes the key and entry pointed to by \a it, to \a out. */
117 virtual void writeEntry(const Map::const_iterator
& it
,
118 QDataStream
& out
) const = 0;
125 * Reads a book from a data stream.
127 * \note Multiple book files can be appended to the same
128 * OpeningBook object.
130 extern LIB_EXPORT QDataStream
& operator>>(QDataStream
& in
, OpeningBook
* book
);
133 * Writes a book to a data stream.
135 * \warning Do not write multiple OpeningBook objects to the same
136 * data stream, because the books are likely to have duplicate
139 extern LIB_EXPORT QDataStream
& operator<<(QDataStream
& out
, const OpeningBook
* book
);
141 #endif // OPENING_BOOK_H