1 /*****************************************************************************
2  * LibreMines                                                                *
3  * Copyright (C) 2020-2021  Bruno Bollos Correa                              *
4  *                                                                           *
5  * This program 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  * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.     *
17  *****************************************************************************
18  */
19 
20 
21 #ifndef LIBREMINESGAMEENGINE_H
22 #define LIBREMINESGAMEENGINE_H
23 
24 #include <QPointer>
25 #include <QElapsedTimer>
26 
27 #include "common.h"
28 #include "libreminesscore.h"
29 
30 class LibreMinesGameEngine: public QObject
31 {
32     Q_OBJECT
33 
34 public:
35     class CellGameEngine
36     {
37     public:
38         CellGameEngine();
39 
40         CELL_STATE state;
41         bool isHidden;
42         bool hasFlag;
43     };
44 
45 public:
46     LibreMinesGameEngine();
47     void vNewGame(const uchar _X,
48                   const uchar _Y,
49                   ushort i_nMines_,
50                   const uchar i_X_Clean = 255,
51                   const uchar i_Y_Clean = 255);
52 
53     const std::vector< std::vector<CellGameEngine> >& getPrincipalMatrix()const;
54     uchar rows()const;
55     uchar lines()const;
56     ushort mines()const;
57     ushort cellsToUnlock()const;
58     ushort hiddenCells()const;
59     bool isGameActive()const;
60 
61     void setFirstCellClean(const bool x);
62 private:
63     void vResetPrincipalMatrix();
64     bool bCleanCell(const uchar _X, const uchar _Y);
65     void vGameLost(const uchar _X, const uchar _Y);
66     void vGameWon();
67 
68     void vGenerateEndGameScore(qint64 iTimeInNs = -1, bool ignorePreferences=false);
69 
70     std::vector< std::vector<CellGameEngine> > principalMatrix; /**< TODO: describe */
71 
72     uchar iX; /**< TODO: describe */
73     uchar iY; /**< TODO: describe */
74 
75     ushort nMines; /**< TODO: describe */
76     ushort iTimeInSeconds; /**< TODO: describe */
77     ushort iMinesLeft; /**< TODO: describe */
78     ushort iHiddenCells; /**< TODO: describe */
79     ushort iCellsToUnlock; /**< TODO: describe */
80 
81     QScopedPointer<QTimer> timerTimeInGame; /**< TODO: describe */
82     QElapsedTimer elapsedPreciseTimeInGame;
83 
84     bool bFirst; /**< TODO: describe */
85     bool bFirstCellClean; /**< TODO: describe */
86 
87     bool bGameActive;
88 
89 Q_SIGNALS:
90     void SIGNAL_showCell(const uchar _X, const uchar _Y);
91     void SIGNAL_endGameScore(LibreMinesScore score,
92                              int iCorrectFlags,
93                              int iWrongFlags,
94                              int iUnlockedCells,
95                              double dFlagsPerSecond,
96                              double dCellsPerSecond,
97                              bool ignorePreferences=false);
98     void SIGNAL_currentTime(const ushort);
99     void SIGNAL_minesLeft(const ushort);
100     void SIGNAL_flagCell(const uchar _X, const uchar _Y);
101     void SIGNAL_unflagCell(const uchar _X, const uchar _Y);
102     void SIGNAL_gameWon();
103     void SIGNAL_gameLost(const uchar _X, const uchar _Y);
104     void SIGNAL_remakeGame();
105 
106 public Q_SLOTS:
107     void SLOT_cleanCell(const uchar _X, const uchar _Y);
108     void SLOT_addOrRemoveFlag(const uchar _X, const uchar _Y);
109     void SLOT_stop();
110     void SLOT_startTimer();
111     void SLOT_cleanNeighborCells(const uchar _X, const uchar _Y);
112     void SLOT_generateEndGameScoreAgain();
113 
114 private Q_SLOTS:
115     void SLOT_UpdateTime();
116 
117 };
118 
119 #endif // LIBREMINESGAMEENGINE_H
120