forked from KDE/kmines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminefielditem.h
218 lines (204 loc) · 6.09 KB
/
minefielditem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
Copyright 2007 Dmitry Suzdalev <[email protected]>
Copyright 2010 Brian Croom <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef MINEFIELDITEM_H
#define MINEFIELDITEM_H
#include <QVector>
#include <QGraphicsObject>
#include <QPair>
#include <KRandomSequence>
class KGameRenderer;
class CellItem;
class BorderItem;
typedef QPair<int,int> FieldPos;
/**
* Graphics item that represents MineField.
* It is composed of many (or little) of CellItems.
* This class is responsible of generation game field
* with given properties (num rows, num cols, num mines) and
* handling resizes
*/
class MineFieldItem : public QGraphicsObject
{
Q_OBJECT
public:
/**
* Constructor.
*/
explicit MineFieldItem(KGameRenderer* renderer);
/**
* Initializes game field: creates items, places them on positions,
* (re)sets some variables
*
* @param numRows number of rows
* @param numCols number of columns
* @param numMines number of mines
*/
void initField( int numRows, int numCols, int numMines );
/**
* Resets mines to the initial state.
*/
void resetMines();
/**
* Resizes this graphics item so it fits in given rect
*/
void resizeToFitInRect(const QRectF& rect);
/**
* Reimplemented from QGraphicsItem
*/
QRectF boundingRect() const override;// reimp
/**
* @return num rows in field
*/
int rowCount() const;
/**
* @return num columns in field
*/
int columnCount() const;
/**
* @return num mines in field
*/
int minesCount() const;
/**
* Minimal number of free positions on a field
*/
static const int MINIMAL_FREE = 10;
Q_SIGNALS:
void flaggedMinesCountChanged(int);
void firstClickDone();
void gameOver(bool won);
private:
// reimplemented
void mousePressEvent( QGraphicsSceneMouseEvent * ) override;
// reimplemented
void mouseReleaseEvent( QGraphicsSceneMouseEvent * ) override;
// reimplemented
void mouseMoveEvent( QGraphicsSceneMouseEvent * ) override;
/**
* Returns cell item at (row,col).
* Always use this function instead hand-computing index in m_cells
*/
inline CellItem* itemAt(int row, int col) { return m_cells.at( row*m_numCols + col ); }
/**
* Overloaded one, which takes QPair
*/
inline CellItem* itemAt( const FieldPos& pos ) { return itemAt(pos.first,pos.second); }
/**
* Calculates (row,col) from given index in m_cells and returns them in QPair
*/
inline FieldPos rowColFromIndex(int idx)
{
int row = idx/m_numCols;
return qMakePair(row, idx - row*m_numCols);
}
/**
* Generates game field ensuring that cell at clickedIdx
* will be empty to allow the player quickly jump into the game.
*
* @param clickedIdx specifies index which should NOT have mine and be empty
*/
void generateField(int clickedIdx);
/**
* Returns all adjacent items for item at row, col
*/
QList<CellItem*> adjacentItemsFor(int row, int col);
/**
* Returns all valid adjacent row,col pairs for row, col
*/
QList<FieldPos> adjacentRowColsFor(int row, int col);
/**
* Checks if player lost the game. Return `true` if lost.
* A `true` return value and a `false` `m_gameOver` indicates that the game is restarted.
*/
bool checkLost();
/**
* Checks if player won the game. Return `true` if won.
*/
bool checkWon();
/**
* Reveals all unmarked items containing mines
*/
void revealAllMines();
/**
* Reimplemented from QGraphicsItem
*/
void paint( QPainter * painter, const QStyleOptionGraphicsItem*, QWidget * widget = nullptr ) override;
/**
* Repositions all child cell items upon resizes
*/
void adjustItemPositions();
/**
* Reveals all empty cells around cell at (row,col),
* until it found cells with digits (which are also revealed)
*/
void revealEmptySpace(int row, int col);
/**
* Sets up border items (positions and properties)
*/
void setupBorderItems();
/**
* Return `true` if the game is finished (and possibly restarted) after the call.
*/
bool onItemRevealed(int row, int col);
// overload
bool onItemRevealed(CellItem* item);
// note: in member functions use itemAt (see above )
// instead of hand-computing index from row & col!
// => not depend on how m_cells is represented
/**
* Array which holds all child cell items
*/
QVector<CellItem*> m_cells;
/**
* Array which holds border items
*/
QVector<BorderItem*> m_borders;
/**
* The width and height of minefield cells in scene coordinates
*/
int m_cellSize;
/**
* Number of field rows
*/
int m_numRows;
/**
* Number of field columns
*/
int m_numCols;
/**
* Number of mines in field
*/
int m_minesCount;
/**
* Number of flagged mines
*/
int m_flaggedMinesCount;
/**
* Random sequence used to generate mine positions
*/
KRandomSequence m_randomSeq;
/**
* row and column where mouse was pressed.
* (-1,-1) if it is already released
*/
FieldPos m_leftButtonPos;
FieldPos m_midButtonPos;
bool m_firstClick;
bool m_gameOver;
bool m_emulatingMidButton;
int m_numUnrevealed;
KGameRenderer* m_renderer;
};
#endif