Cute Chess  0.1
chessengine.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 CHESSENGINE_H
19 #define CHESSENGINE_H
20 
21 #include "chessplayer.h"
22 #include <QVariant>
23 #include <QStringList>
24 #include "engineconfiguration.h"
25 
26 class QIODevice;
27 class EngineOption;
28 
29 
40 class LIB_EXPORT ChessEngine : public ChessPlayer
41 {
42  Q_OBJECT
43 
44  public:
50  enum WriteMode
51  {
53  Unbuffered
54  };
55 
57  ChessEngine(QObject* parent = nullptr);
58  virtual ~ChessEngine();
59 
61  QIODevice* device() const;
63  void setDevice(QIODevice* device);
64 
65  // Inherited from ChessPlayer
66  virtual void endGame(const Chess::Result& result);
67  virtual bool isHuman() const;
68  virtual bool isReady() const;
69  virtual bool supportsVariant(const QString& variant) const;
70 
75  void start();
76 
78  void applyConfiguration(const EngineConfiguration& configuration);
79 
93  void ping(bool sendCommand = true);
94 
96  virtual QString protocol() const = 0;
97 
104  void write(const QString& data, WriteMode mode = Buffered);
105 
112  void setOption(const QString& name, const QVariant& value);
113 
115  QList<EngineOption*> options() const;
116 
118  QStringList variants() const;
119 
120  public slots:
121  // Inherited from ChessPlayer
122  virtual void go();
123  virtual void quit();
124  virtual void kill();
125 
126  protected:
138  static QStringRef firstToken(const QString& str,
139  bool readToEnd = false);
152  static QStringRef nextToken(const QStringRef& previous,
153  bool readToEnd = false);
154 
155  // Inherited from ChessPlayer
156  virtual void startGame() = 0;
157 
162  virtual void startProtocol() = 0;
163 
165  virtual void parseLine(const QString& line) = 0;
166 
171  virtual bool sendPing() = 0;
172 
174  virtual void sendStop() = 0;
175 
177  virtual void sendQuit() = 0;
178 
185  bool stopThinking();
186 
188  void addOption(EngineOption* option);
193  EngineOption* getOption(const QString& name) const;
195  virtual void sendOption(const QString& name, const QVariant& value) = 0;
196 
198  void addVariant(const QString& variant);
200  void clearVariants();
201 
206  EngineConfiguration::RestartMode restartMode() const;
211  virtual bool restartsBetweenGames() const;
216  virtual bool isPondering() const;
217 
219  bool whiteEvalPov() const;
227  bool pondering() const;
228 
229  protected slots:
230  // Inherited from ChessPlayer
231  virtual void onTimeout();
232 
234  void onReadyRead();
235 
237  void onPingTimeout();
238 
240  void onIdleTimeout();
241 
250  void pong(bool emitReady = true);
251 
256  void onProtocolStart();
257 
263  void flushWriteBuffer();
264 
266  void clearWriteBuffer();
267 
268  private slots:
269  void onQuitTimeout();
270  void onProtocolStartTimeout();
271 
272  private:
273  static int s_count;
274 
275  int m_id;
276  State m_pingState;
277  bool m_pinging;
278  bool m_whiteEvalPov;
279  bool m_pondering;
280  QTimer* m_pingTimer;
281  QTimer* m_quitTimer;
282  QTimer* m_idleTimer;
283  QTimer* m_protocolStartTimer;
284  QIODevice *m_ioDevice;
285  QStringList m_writeBuffer;
286  QStringList m_variants;
287  QList<EngineOption*> m_options;
288  QMap<QString, QVariant> m_optionBuffer;
289  EngineConfiguration::RestartMode m_restartMode;
290 };
291 
292 #endif // CHESSENGINE_H
virtual void go()
Definition: chessplayer.cpp:85
Use the write buffer.
Definition: chessengine.h:52
virtual void quit()
Definition: chessplayer.cpp:105
The EngineConfiguration class defines a chess engine configuration.
Definition: engineconfiguration.h:32
virtual bool isReady() const
Definition: chessplayer.cpp:40
A chess player, human or AI.
Definition: chessplayer.h:37
virtual bool supportsVariant(const QString &variant) const =0
virtual void kill()
Definition: chessplayer.cpp:262
An artificial intelligence chess player.
Definition: chessengine.h:40
WriteMode
Definition: chessengine.h:50
The result of a chess game.
Definition: result.h:33
Definition: engineoption.h:24
virtual void endGame(const Chess::Result &result)
Definition: chessplayer.cpp:72
RestartMode
Definition: engineconfiguration.h:39
virtual bool isHuman() const =0
virtual void onTimeout()
Definition: chessplayer.cpp:274
virtual void startGame()=0