Cute Chess  0.1
chessgame.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 CHESSGAME_H
19 #define CHESSGAME_H
20 
21 #include <QObject>
22 #include <QVector>
23 #include <QStringList>
24 #include <QMap>
25 #include <QSemaphore>
26 #include "pgngame.h"
27 #include "board/result.h"
28 #include "board/move.h"
29 #include "timecontrol.h"
30 #include "gameadjudicator.h"
31 
32 namespace Chess { class Board; }
33 class ChessPlayer;
34 class OpeningBook;
35 class MoveEvaluation;
36 
37 
38 class LIB_EXPORT ChessGame : public QObject
39 {
40  Q_OBJECT
41 
42  public:
43  ChessGame(Chess::Board* board, PgnGame* pgn, QObject* parent = nullptr);
44  virtual ~ChessGame();
45 
46  QString errorString() const;
47  ChessPlayer* player(Chess::Side side) const;
48  ChessPlayer* playerToMove() const;
49  ChessPlayer* playerToWait() const;
50  bool isFinished() const;
51  bool boardShouldBeFlipped() const;
52  void setBoardShouldBeFlipped(bool flip);
53 
54  PgnGame* pgn() const;
55  Chess::Board* board() const;
56  QString startingFen() const;
57  const QVector<Chess::Move>& moves() const;
58  const QMap<int,int>& scores() const;
59  Chess::Result result() const;
60 
61  void setError(const QString& message);
62  void setPlayer(Chess::Side side, ChessPlayer* player);
63  void setStartingFen(const QString& fen);
64  void setTimeControl(const TimeControl& timeControl,
65  Chess::Side side = Chess::Side());
66  void setMoves(const QVector<Chess::Move>& moves);
67  bool setMoves(const PgnGame& pgn);
68  void setOpeningBook(const OpeningBook* book,
69  Chess::Side side = Chess::Side(),
70  int depth = 1000);
71  void setAdjudicator(const GameAdjudicator& adjudicator);
72  void setStartDelay(int time);
73  void setBookOwnership(bool enabled);
74 
75  void generateOpening();
76 
77  void lockThread();
78  void unlockThread();
79 
80  public slots:
81  void start();
82  void pause();
83  void resume();
84  void stop(bool emitMoveChanged = true);
85  void kill();
86  void emitStartFailed();
87  void onMoveMade(const Chess::Move& move);
88 
89  signals:
90  void humanEnabled(bool);
91  void fenChanged(const QString& fenString);
92  void moveMade(const Chess::GenericMove& move,
93  const QString& sanString,
94  const QString& comment);
95  void moveChanged(int ply,
96  const Chess::GenericMove& move,
97  const QString& sanString,
98  const QString& comment);
99  void scoreChanged(int ply, int score);
100  void started(ChessGame* game = nullptr);
101  void finished(ChessGame* game = nullptr,
102  Chess::Result result = Chess::Result());
103  void startFailed(ChessGame* game = nullptr);
104  void playersReady();
105 
106  private slots:
107  void startGame();
108  void startTurn();
109  void finish();
110  void onResultClaim(const Chess::Result& result);
111  void onPlayerReady();
112  void syncPlayers();
113  void pauseThread();
114 
115  private:
116  Chess::Move bookMove(Chess::Side side);
117  bool resetBoard();
118  void initializePgn();
119  void addPgnMove(const Chess::Move& move, const QString& comment);
120  void emitLastMove();
121 
122  Chess::Board* m_board;
123  ChessPlayer* m_player[2];
124  TimeControl m_timeControl[2];
125  const OpeningBook* m_book[2];
126  int m_bookDepth[2];
127  int m_startDelay;
128  bool m_finished;
129  bool m_gameInProgress;
130  bool m_paused;
131  bool m_pgnInitialized;
132  bool m_bookOwnership;
133  bool m_boardShouldBeFlipped;
134  QString m_error;
135  QString m_startingFen;
136  Chess::Result m_result;
137  QVector<Chess::Move> m_moves;
138  QMap<int,int> m_scores;
139  PgnGame* m_pgn;
140  QSemaphore m_pauseSem;
141  QSemaphore m_resumeSem;
142  GameAdjudicator m_adjudicator;
143 };
144 
145 #endif // CHESSGAME_H
Time controls of a chess game.
Definition: timecontrol.h:34
A chess player, human or AI.
Definition: chessplayer.h:37
Evaluation data for a chess move.
Definition: moveevaluation.h:34
An internal chessboard class.
Definition: board.h:56
A game of chess in PGN format.
Definition: pgngame.h:51
Definition: chessgame.h:38
A chess move independent of chess variant or opening book format.
Definition: genericmove.h:33
Definition: boardscene.h:28
The side or color of a chess player.
Definition: side.h:34
The result of a chess game.
Definition: result.h:33
A class for adjudicating chess games.
Definition: gameadjudicator.h:31
A small and efficient chessmove class.
Definition: move.h:41
A collection of opening moves for chess.
Definition: openingbook.h:42