Cute Chess 0.1
chessengine.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 CHESSENGINE_H
20#define CHESSENGINE_H
21
22#include "chessplayer.h"
23#include <QVariant>
24#include <QStringList>
25#include "engineconfiguration.h"
26
27class QIODevice;
28class EngineOption;
29
30
41class LIB_EXPORT ChessEngine : public ChessPlayer
42{
43 Q_OBJECT
44
45 public:
56
58 ChessEngine(QObject* parent = nullptr);
59 virtual ~ChessEngine();
60
62 QIODevice* device() const;
64 void setDevice(QIODevice* device);
65
66 // Inherited from ChessPlayer
67 virtual void endGame(const Chess::Result& result);
68 virtual bool isHuman() const;
69 virtual bool isReady() const;
70 virtual bool supportsVariant(const QString& variant) const;
71
76 void start();
77
79 void applyConfiguration(const EngineConfiguration& configuration);
80
94 void ping(bool sendCommand = true);
95
97 virtual QString protocol() const = 0;
98
105 void write(const QString& data, WriteMode mode = Buffered);
106
113 void setOption(const QString& name, const QVariant& value);
114
117
119 QStringList variants() const;
120
121 public slots:
122 // Inherited from ChessPlayer
123 virtual void go();
124 virtual void quit();
125 virtual void kill();
126
127 protected:
139 static QStringRef firstToken(const QString& str,
140 bool readToEnd = false);
153 static QStringRef nextToken(const QStringRef& previous,
154 bool readToEnd = false);
155
156 // Inherited from ChessPlayer
157 virtual void startGame() = 0;
158
163 virtual void startProtocol() = 0;
164
166 virtual void parseLine(const QString& line) = 0;
167
172 virtual bool sendPing() = 0;
173
175 virtual void sendStop() = 0;
176
178 virtual void sendQuit() = 0;
179
186 bool stopThinking();
187
189 void addOption(EngineOption* option);
194 EngineOption* getOption(const QString& name) const;
196 virtual void sendOption(const QString& name, const QVariant& value) = 0;
197
199 void addVariant(const QString& variant);
201 void clearVariants();
202
212 virtual bool restartsBetweenGames() const;
217 virtual bool isPondering() const;
218
220 bool whiteEvalPov() const;
228 bool pondering() const;
232 int id() const;
233
234 protected slots:
235 // Inherited from ChessPlayer
236 virtual void onTimeout();
237
239 void onReadyRead();
240
242 void onPingTimeout();
243
245 void onIdleTimeout();
246
255 void pong(bool emitReady = true);
256
261 void onProtocolStart();
262
268 void flushWriteBuffer();
269
271 void clearWriteBuffer();
272
273 private slots:
274 void onQuitTimeout();
275 void onProtocolStartTimeout();
276
277 private:
278 static int s_count;
279
280 int m_id;
281 State m_pingState;
282 bool m_pinging;
283 bool m_whiteEvalPov;
284 bool m_pondering;
285 QTimer* m_pingTimer;
286 QTimer* m_quitTimer;
287 QTimer* m_idleTimer;
288 QTimer* m_protocolStartTimer;
289 QIODevice *m_ioDevice;
290 QStringList m_writeBuffer;
291 QStringList m_variants;
292 QList<EngineOption*> m_options;
293 QMap<QString, QVariant> m_optionBuffer;
295};
296
297#endif // CHESSENGINE_H
An artificial intelligence chess player.
Definition chessengine.h:42
void setOption(const QString &name, const QVariant &value)
Definition chessengine.cpp:164
virtual void startProtocol()=0
QStringList variants() const
Definition chessengine.cpp:198
void onIdleTimeout()
Definition chessengine.cpp:330
void addVariant(const QString &variant)
Definition chessengine.cpp:203
virtual void startGame()=0
bool whiteEvalPov() const
Definition chessengine.cpp:269
void write(const QString &data, WriteMode mode=Buffered)
Definition chessengine.cpp:423
void clearVariants()
Definition chessengine.cpp:209
int id() const
Definition chessengine.cpp:306
virtual bool sendPing()=0
virtual void parseLine(const QString &line)=0
void onReadyRead()
Definition chessengine.cpp:445
void addOption(EngineOption *option)
Definition chessengine.cpp:147
void onPingTimeout()
Definition chessengine.cpp:409
virtual bool isPondering() const
Definition chessengine.cpp:264
bool stopThinking()
Definition chessengine.cpp:311
virtual bool restartsBetweenGames() const
Definition chessengine.cpp:259
virtual void sendQuit()=0
EngineOption * getOption(const QString &name) const
Definition chessengine.cpp:153
QList< EngineOption * > options() const
Definition chessengine.cpp:193
void onProtocolStart()
Definition chessengine.cpp:229
bool pondering() const
Definition chessengine.cpp:274
virtual void sendOption(const QString &name, const QVariant &value)=0
virtual QString protocol() const =0
ChessEngine(QObject *parent=nullptr)
Definition chessengine.cpp:72
void clearWriteBuffer()
Definition chessengine.cpp:483
WriteMode
Definition chessengine.h:52
@ Unbuffered
Bypass the write buffer.
Definition chessengine.h:54
@ Buffered
Use the write buffer.
Definition chessengine.h:53
static QStringRef nextToken(const QStringRef &previous, bool readToEnd=false)
Definition chessengine.cpp:29
void pong(bool emitReady=true)
Definition chessengine.cpp:380
void flushWriteBuffer()
Definition chessengine.cpp:473
virtual void sendStop()=0
EngineConfiguration::RestartMode restartMode() const
Definition chessengine.cpp:254
static QStringRef firstToken(const QString &str, bool readToEnd=false)
Definition chessengine.cpp:66
virtual void endGame(const Chess::Result &result)
Definition chessplayer.cpp:74
virtual void onTimeout()
Definition chessplayer.cpp:297
State
Definition chessplayer.h:45
QString name() const
Definition chessplayer.cpp:202
virtual void quit()
Definition chessplayer.cpp:107
virtual bool isReady() const
Definition chessplayer.cpp:42
virtual void go()
Definition chessplayer.cpp:87
virtual bool supportsVariant(const QString &variant) const =0
virtual bool isHuman() const =0
virtual void kill()
Definition chessplayer.cpp:285
ChessPlayer(QObject *parent=nullptr)
Definition chessplayer.cpp:24
The result of a chess game.
Definition result.h:35
The EngineConfiguration class defines a chess engine configuration.
Definition engineconfiguration.h:34
RestartMode
Definition engineconfiguration.h:41
Definition engineoption.h:26