Cute Chess  0.1
openingbook.h
1 /*
2  This file is part of Cute Chess.
3 
4  Cute Chess is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  Cute Chess is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with Cute Chess. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef OPENING_BOOK_H
19 #define OPENING_BOOK_H
20 
21 #include <QtGlobal>
22 #include <QMultiMap>
23 #include "board/genericmove.h"
24 
25 class QString;
26 class QDataStream;
27 class PgnGame;
28 class PgnStream;
29 
30 
42 class LIB_EXPORT OpeningBook
43 {
44  public:
47  {
48  Ram,
49  Disk
50  };
51 
59  struct Entry
60  {
68  quint16 weight;
69  };
70 
72  OpeningBook(AccessMode mode = Ram);
74  virtual ~OpeningBook();
75 
85  int import(const PgnGame& pgn, int maxMoves);
95  int import(PgnStream& in, int maxMoves);
96 
108  Chess::GenericMove move(quint64 key) const;
109 
111  QList<Entry> entries(quint64 key) const;
112 
117  bool read(const QString& filename);
118 
123  bool write(const QString& filename) const;
124 
125 
126  protected:
127  friend LIB_EXPORT QDataStream& operator>>(QDataStream& in, OpeningBook* book);
128  friend LIB_EXPORT QDataStream& operator<<(QDataStream& out, const OpeningBook* book);
129 
132 
134  virtual int entrySize() const = 0;
135 
137  void addEntry(const Entry& entry, quint64 key);
138 
145  virtual Entry readEntry(QDataStream& in, quint64* key) const = 0;
146 
148  virtual void writeEntry(const Map::const_iterator& it,
149  QDataStream& out) const = 0;
150 
151  private:
152  QList<Entry> entriesFromDisk(quint64 key) const;
153 
154  AccessMode m_mode;
155  QString m_filename;
156  Map m_map;
157 };
158 
165 extern LIB_EXPORT QDataStream& operator>>(QDataStream& in, OpeningBook* book);
166 
174 extern LIB_EXPORT QDataStream& operator<<(QDataStream& out, const OpeningBook* book);
175 
176 #endif // OPENING_BOOK_H
A class for reading games in PGN format from a text stream.
Definition: pgnstream.h:41
Load the entire book to RAM.
Definition: openingbook.h:48
QMultiMap< quint64, Entry > Map
Definition: openingbook.h:131
A game of chess in PGN format.
Definition: pgngame.h:51
quint16 weight
Definition: openingbook.h:68
A chess move independent of chess variant or opening book format.
Definition: genericmove.h:33
An entry in the opening book.
Definition: openingbook.h:59
AccessMode
Definition: openingbook.h:46
Chess::GenericMove move
Definition: openingbook.h:62
QDebug operator<<(QDebug d, const QCPVector2D &vec)
Definition: qcustomplot.h:441
A collection of opening moves for chess.
Definition: openingbook.h:42