File indexing completed on 2024-03-24 04:07:36
0001 /* 0002 SPDX-FileCopyrightText: 2006 Matthew Williams <matt@milliams.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #ifndef KSQUARESGAME_H 0008 #define KSQUARESGAME_H 0009 0010 #include <QList> 0011 #include <QObject> 0012 0013 #include "ksquaresplayer.h" 0014 0015 class QColor; 0016 0017 /** 0018 * @short The game controller 0019 * 0020 * Keeps charge of the game. Everything you'd expect really. 0021 * 0022 * - Create one instance of this class which will last the whole time the program is running. 0023 * 0024 * - In order to start a (new) game just call @ref createGame() with the appropriate arguments. 0025 * 0026 * - Once the game is started, play passes to the first player. @ref takeTurnSig() will be emitted to allow you to chose how the go should be taken (AI controller or 'click' from a View). 0027 * 0028 * - However the turn is taken, to make the move, the @ref addLineToIndex() function must be called. This will emit the @ref drawLine() signal to allow you to draw the line on the board with the correct colours. 0029 * 0030 * - If the player completed a square, @ref drawSquare() will then be emitted to allow you to draw the completed square with the correct colour. 0031 * 0032 * - If the player gets another go, @ref takeTurnSig() will be emitted again. If not, play will pass to the next player and @ref takeTurnSig() will be emitted for them. 0033 * 0034 * - If a player completes the scoreboard, @ref gameOver() will be emitted with the full list of players to allow you to construct a score board. 0035 * 0036 * - All variables will remain in the state they were at the end of the game until @ref createGame() is called again. 0037 * 0038 * @author Matt Williams <matt@milliams.com> 0039 */ 0040 0041 class KSquaresGame : public QObject 0042 { 0043 Q_OBJECT 0044 0045 public: 0046 ///Constructor 0047 KSquaresGame(); 0048 ~KSquaresGame() override; 0049 0050 /** 0051 * Create a new game 0052 * 0053 * @param startPlayers liat of the players in the game 0054 * @param startWidth the width of the game board 0055 * @param startHeight the height of the game board 0056 */ 0057 void createGame(const QList<KSquaresPlayer> &startPlayers, int startWidth, int startHeight); 0058 /** 0059 * Starts the game 0060 */ 0061 void start() 0062 { 0063 gameInProgress = true; 0064 } 0065 /** 0066 * Stops the game 0067 */ 0068 void stop() 0069 { 0070 gameInProgress = false; 0071 } 0072 /** 0073 * Externally determined player switch, for network game 0074 */ 0075 void switchPlayer(); 0076 /** 0077 * @return the id of the current player. 0 >= id \< number of players 0078 */ 0079 int currentPlayerId() const 0080 { 0081 return i_currentPlayerId; 0082 } 0083 /** 0084 * @return the current player 0085 */ 0086 KSquaresPlayer *currentPlayer() 0087 { 0088 return &players[currentPlayerId()]; 0089 } 0090 0091 //getters 0092 /** 0093 * @return the table of currently owned squares 0094 */ 0095 QList<int> squares() const 0096 { 0097 return squareOwnerTable; 0098 } 0099 /** 0100 * @return the list of lines 0101 */ 0102 QList<bool> lines() const 0103 { 0104 return lineList; 0105 } 0106 /** 0107 * @return the width of the game board 0108 */ 0109 int boardWidth() const 0110 { 0111 return width; 0112 } 0113 /** 0114 * @return the height of the game board 0115 */ 0116 int boardHeight() const 0117 { 0118 return height; 0119 } 0120 0121 public Q_SLOTS: 0122 /** 0123 * @param index the index of the line to add 0124 */ 0125 void addLineToIndex(int index); 0126 0127 protected: 0128 /** 0129 * Moves play control to the next player, looping round when necessary 0130 * 0131 * @return the Id of the player who's turn just started 0132 */ 0133 int nextPlayer(); 0134 ///Sets lots of things to zero, clears lists etc. 0135 void resetEverything(); 0136 0137 ///A line was drawn, see if the player gets another go 0138 void tryEndGo(); 0139 ///Scans the board to see if any new squares were completed 0140 void checkForNewSquares(); 0141 /** 0142 * A player completed a square. Emits the lineDrawn() signal. Checks to see if the game is over. 0143 * 0144 * @param index the index of the square which was completed 0145 */ 0146 void playerSquareComplete(int index); 0147 0148 // Static throughout each game 0149 ///Number of players in this game 0150 int numOfPlayers; 0151 ///Width of the game board 0152 int width; 0153 /// Height of the game board 0154 int height; 0155 ///List of the squares and their owners 0156 QList<int> squareOwnerTable; 0157 ///List of the lines and whether they're drawn 0158 QList<bool> lineList; 0159 0160 // Updated as the game progresses 0161 ///List of all the players in the game 0162 QList<KSquaresPlayer> players; 0163 0164 // Probably changes every go 0165 ///Id of the current player 0166 int i_currentPlayerId; 0167 /// Should the current player have another go 0168 bool anotherGo; 0169 /// is there currently a game in progress 0170 bool gameInProgress; 0171 /// last line added 0172 int lastLine; 0173 Q_SIGNALS: 0174 ///A player's turn has started. This allows you to use AI/networking etc. 0175 void takeTurnSig(KSquaresPlayer *); //emit the new curent player 0176 ///emitted when the game board is completed. Allows you to construct a scoreboard 0177 void gameOver(const QList<KSquaresPlayer> &); //for scoreboard purposes 0178 ///Emits the index and colour of the line 0179 void drawLine(int, QColor); //int == lineList index 0180 ///Emits the index and colour of the square 0181 void drawSquare(int, QColor); //int == squareOwnerTable index 0182 ///Emitted when the last move in a series is played by the AI 0183 void highlightMove(int); 0184 }; 0185 0186 #endif // KSQUARESGAME_H