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 
27 class QIODevice;
28 class EngineOption;
29 
30 
41 class LIB_EXPORT ChessEngine : public ChessPlayer
42 {
43  Q_OBJECT
44 
45  public:
51  enum WriteMode
52  {
54  Unbuffered
55  };
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 
116  QList<EngineOption*> options() const;
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 
207  EngineConfiguration::RestartMode restartMode() const;
212  virtual bool restartsBetweenGames() const;
217  virtual bool isPondering() const;
218 
220  bool whiteEvalPov() const;
228  bool pondering() const;
229 
230  protected slots:
231  // Inherited from ChessPlayer
232  virtual void onTimeout();
233 
235  void onReadyRead();
236 
238  void onPingTimeout();
239 
241  void onIdleTimeout();
242 
251  void pong(bool emitReady = true);
252 
257  void onProtocolStart();
258 
264  void flushWriteBuffer();
265 
267  void clearWriteBuffer();
268 
269  private slots:
270  void onQuitTimeout();
271  void onProtocolStartTimeout();
272 
273  private:
274  static int s_count;
275 
276  int m_id;
277  State m_pingState;
278  bool m_pinging;
279  bool m_whiteEvalPov;
280  bool m_pondering;
281  QTimer* m_pingTimer;
282  QTimer* m_quitTimer;
283  QTimer* m_idleTimer;
284  QTimer* m_protocolStartTimer;
285  QIODevice *m_ioDevice;
286  QStringList m_writeBuffer;
287  QStringList m_variants;
288  QList<EngineOption*> m_options;
289  QMap<QString, QVariant> m_optionBuffer;
290  EngineConfiguration::RestartMode m_restartMode;
291 };
292 
293 #endif // CHESSENGINE_H
virtual void go()
Definition: chessplayer.cpp:87
Use the write buffer.
Definition: chessengine.h:53
virtual void quit()
Definition: chessplayer.cpp:107
The EngineConfiguration class defines a chess engine configuration.
Definition: engineconfiguration.h:33
A chess player, human or AI.
Definition: chessplayer.h:38
State
Definition: chessplayer.h:44
virtual bool supportsVariant(const QString &variant) const =0
virtual void kill()
Definition: chessplayer.cpp:285
An artificial intelligence chess player.
Definition: chessengine.h:41
WriteMode
Definition: chessengine.h:51
The result of a chess game.
Definition: result.h:34
virtual bool isReady() const
Definition: chessplayer.cpp:42
Definition: engineoption.h:25
virtual void endGame(const Chess::Result &result)
Definition: chessplayer.cpp:74
RestartMode
Definition: engineconfiguration.h:40
virtual bool isHuman() const =0
virtual void onTimeout()
Definition: chessplayer.cpp:297
virtual void startGame()=0