File indexing completed on 2024-04-28 04:03:12

0001 /***************************************************************************
0002     File                 : gamemanager.h
0003     Project              : Knights
0004     Description          : Game manager
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2016 Alexander Semke (alexander.semke@web.de)
0007     SPDX-FileCopyrightText: 2009-2011 Miha Čančula (miha@noughmad.eu)
0008 
0009  ***************************************************************************/
0010 
0011 /***************************************************************************
0012  *                                                                         *
0013  *  SPDX-License-Identifier: GPL-2.0-or-later
0014  *                                                                         *
0015  *  This program is distributed in the hope that it will be useful,        *
0016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0018  *  GNU General Public License for more details.                           *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program; if not, write to the Free Software           *
0022  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0023  *   Boston, MA  02110-1301  USA                                           *
0024  *                                                                         *
0025  ***************************************************************************/
0026 
0027 #ifndef KNIGHTS_GAMEMANAGER_H
0028 #define KNIGHTS_GAMEMANAGER_H
0029 
0030 #include <core/piece.h>
0031 #include <core/move.h>
0032 #include "offerwidget.h"
0033 
0034 #include <QTime>
0035 #include <QStack>
0036 
0037 class KGameDifficultyLevel;
0038 
0039 namespace Knights {
0040 
0041 class Protocol;
0042 class Rules;
0043 class Move;
0044 class GameManagerPrivate;
0045 
0046 struct TimeControl {
0047     int moves;
0048     QTime baseTime;
0049     int increment;
0050     QTime currentTime;
0051 };
0052 
0053 enum GameAction {
0054     ActionNone,
0055     ActionDraw,
0056     ActionUndo,
0057     ActionAdjourn,
0058     ActionAbort,
0059     ActionPause,
0060     ActionResume,
0061     ActionResign,
0062     ActionOther
0063 };
0064 
0065 class Offer {
0066 public:
0067     Offer() {
0068         action = ActionNone;
0069         id = 0;
0070         player = NoColor;
0071         numberOfMoves = 0;
0072     };
0073     GameAction action;
0074     int id;
0075     QString text;
0076     Color player;
0077     int numberOfMoves; // Only used for Takeback offers.
0078 
0079     void accept() const;
0080     void decline() const;
0081 };
0082 
0083 class Manager : public QObject {
0084     Q_OBJECT
0085 public:
0086     static Manager* self();
0087     explicit Manager(QObject* parent = nullptr);
0088     ~Manager() override;
0089 
0090     void setCurrentTime(Color, const QTime&);
0091     void setTimeControl(Color, const TimeControl&);
0092     TimeControl timeControl(Color) const;
0093     bool timeControlEnabled(Color) const;
0094     QTime timeLimit(Color);
0095 
0096     void changeActivePlayer();
0097     void setActivePlayer(Color);
0098     Color activePlayer() const;
0099     bool isRunning();
0100 
0101     bool canRedo() const;
0102     bool isGameActive() const;
0103     bool canLocalMove() const;
0104 
0105     Rules* rules() const;
0106     void setRules(Rules*);
0107     void reset();
0108 
0109     void startGame();
0110     void resign();
0111     QStack<Move> moveHistory() const;
0112 
0113 private:
0114     void addMoveToHistory(const Move&);
0115     Move nextUndoMove();
0116     Move nextRedoMove();
0117 
0118     void startTime();
0119     void stopTime();
0120 
0121     void processMove(const Move&);
0122     Protocol* local();
0123 
0124 public Q_SLOTS:
0125     void levelChanged(const KGameDifficultyLevel*);
0126 
0127     void moveByExternalControl(const Move&);
0128     void moveByBoard(const Move&);
0129 
0130     void sendOffer(GameAction action, Color player = NoColor, int id = 0);
0131     void sendOffer(const Offer&);
0132     void setOfferResult(int id, OfferAction result);
0133 
0134     void initialize();
0135     void pause(bool);
0136     void undo();
0137     void redo();
0138 
0139     void offerDraw();
0140     void adjourn();
0141     void abort();
0142 
0143     void loadGameHistoryFrom(const QString& filename);
0144     void saveGameHistoryAs(const QString& filename);
0145 
0146 private Q_SLOTS:
0147     void moveByProtocol(const Move&);
0148     void protocolInitSuccesful();
0149     void gameOver(Color);
0150     void setTimeRunning(bool);
0151     void sendPendingMove();
0152 
0153 private:
0154     GameManagerPrivate* d_ptr;
0155     Move pendingMove;
0156     Q_DECLARE_PRIVATE(GameManager)
0157 
0158 protected:
0159     void timerEvent(QTimerEvent*) override;
0160 
0161 Q_SIGNALS:
0162     void timeChanged(Color, const QTime&);
0163     void undoPossible(bool);
0164     void redoPossible(bool);
0165     void pieceMoved(const Move&);
0166     void activePlayerChanged(Color);
0167     void initComplete();
0168     void notification(const Offer& );
0169     void winnerNotify(Color);
0170     void historyChanged();
0171     void playerNameChanged();
0172 };
0173 
0174 }
0175 
0176 #endif // KNIGHTS_GAMEMANAGER_H