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