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 <QStringRef>
26 #include "engineconfiguration.h"
27 
28 class QIODevice;
29 class EngineOption;
30 
31 
42 class LIB_EXPORT ChessEngine : public ChessPlayer
43 {
44  Q_OBJECT
45 
46  public:
52  enum WriteMode
53  {
55  Unbuffered
56  };
57 
59  ChessEngine(QObject* parent = nullptr);
60  virtual ~ChessEngine();
61 
63  QIODevice* device() const;
65  void setDevice(QIODevice* device);
66 
67  // Inherited from ChessPlayer
68  virtual void endGame(const Chess::Result& result);
69  virtual bool isHuman() const;
70  virtual bool isReady() const;
71  virtual bool supportsVariant(const QString& variant) const;
72 
77  void start();
78 
80  void applyConfiguration(const EngineConfiguration& configuration);
81 
95  void ping(bool sendCommand = true);
96 
98  virtual QString protocol() const = 0;
99 
106  void write(const QString& data, WriteMode mode = Buffered);
107 
114  void setOption(const QString& name, const QVariant& value);
115 
117  QList<EngineOption*> options() const;
118 
120  QStringList variants() const;
121 
122  public slots:
123  // Inherited from ChessPlayer
124  virtual void go();
125  virtual void quit();
126  virtual void kill();
127 
128  protected:
140  static QStringRef firstToken(const QString& str,
141  bool readToEnd = false);
154  static QStringRef nextToken(const QStringRef& previous,
155  bool readToEnd = false);
156 
157  // Inherited from ChessPlayer
158  virtual void startGame() = 0;
159 
164  virtual void startProtocol() = 0;
165 
167  virtual void parseLine(const QString& line) = 0;
168 
173  virtual bool sendPing() = 0;
174 
176  virtual void sendStop() = 0;
177 
179  virtual void sendQuit() = 0;
180 
187  bool stopThinking();
188 
190  void addOption(EngineOption* option);
195  EngineOption* getOption(const QString& name) const;
197  virtual void sendOption(const QString& name, const QVariant& value) = 0;
198 
200  void addVariant(const QString& variant);
202  void clearVariants();
203 
208  EngineConfiguration::RestartMode restartMode() const;
213  virtual bool restartsBetweenGames() const;
218  virtual bool isPondering() const;
219 
221  bool whiteEvalPov() const;
229  bool pondering() const;
233  int id() const;
234 
235  protected slots:
236  // Inherited from ChessPlayer
237  virtual void onTimeout();
238 
240  void onReadyRead();
241 
243  void onPingTimeout();
244 
246  void onIdleTimeout();
247 
256  void pong(bool emitReady = true);
257 
262  void onProtocolStart();
263 
269  void flushWriteBuffer();
270 
272  void clearWriteBuffer();
273 
277  void onQuitTimeout();
278 
282  void onProtocolStartTimeout();
283 
284  private:
285  static int s_count;
286 
287  int m_id;
288  State m_pingState;
289  bool m_pinging;
290  bool m_whiteEvalPov;
291  bool m_pondering;
292  QTimer* m_pingTimer;
293  QTimer* m_quitTimer;
294  QTimer* m_idleTimer;
295  QTimer* m_protocolStartTimer;
296  QIODevice *m_ioDevice;
297  QStringList m_writeBuffer;
298  QStringList m_variants;
299  QList<EngineOption*> m_options;
300  QMap<QString, QVariant> m_optionBuffer;
301  EngineConfiguration::RestartMode m_restartMode;
302 };
303 
304 #endif // CHESSENGINE_H
virtual void go()
Definition: chessplayer.cpp:87
Use the write buffer.
Definition: chessengine.h:54
virtual void quit()
Definition: chessplayer.cpp:107
The EngineConfiguration class defines a chess engine configuration.
Definition: engineconfiguration.h:33
virtual bool isReady() const
Definition: chessplayer.cpp:42
A chess player, human or AI.
Definition: chessplayer.h:38
virtual bool supportsVariant(const QString &variant) const =0
virtual void kill()
Definition: chessplayer.cpp:291
An artificial intelligence chess player.
Definition: chessengine.h:42
WriteMode
Definition: chessengine.h:52
The result of a chess game.
Definition: result.h:34
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:303
virtual void startGame()=0