|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the demonstration applications of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <stdlib.h> |
|
43 #include <qdeclarativeextensionplugin.h> |
|
44 #include <qdeclarativecontext.h> |
|
45 #include <qdeclarativeengine.h> |
|
46 #include <qdeclarative.h> |
|
47 |
|
48 #include <QTime> |
|
49 #include <QTimer> |
|
50 |
|
51 class TileData : public QObject |
|
52 { |
|
53 Q_OBJECT |
|
54 public: |
|
55 TileData() : _hasFlag(false), _hasMine(false), _hint(-1), _flipped(false) {} |
|
56 |
|
57 Q_PROPERTY(bool hasFlag READ hasFlag WRITE setHasFlag NOTIFY hasFlagChanged) |
|
58 bool hasFlag() const { return _hasFlag; } |
|
59 |
|
60 Q_PROPERTY(bool hasMine READ hasMine NOTIFY hasMineChanged) |
|
61 bool hasMine() const { return _hasMine; } |
|
62 |
|
63 Q_PROPERTY(int hint READ hint NOTIFY hintChanged) |
|
64 int hint() const { return _hint; } |
|
65 |
|
66 Q_PROPERTY(bool flipped READ flipped NOTIFY flippedChanged()) |
|
67 bool flipped() const { return _flipped; } |
|
68 |
|
69 void setHasFlag(bool flag) {if(flag==_hasFlag) return; _hasFlag = flag; emit hasFlagChanged();} |
|
70 void setHasMine(bool mine) {if(mine==_hasMine) return; _hasMine = mine; emit hasMineChanged();} |
|
71 void setHint(int hint) { if(hint == _hint) return; _hint = hint; emit hintChanged(); } |
|
72 void flip() { if (_flipped) return; _flipped = true; emit flippedChanged(); } |
|
73 void unflip() { if(!_flipped) return; _flipped = false; emit flippedChanged(); } |
|
74 |
|
75 signals: |
|
76 void flippedChanged(); |
|
77 void hasFlagChanged(); |
|
78 void hintChanged(); |
|
79 void hasMineChanged(); |
|
80 |
|
81 private: |
|
82 bool _hasFlag; |
|
83 bool _hasMine; |
|
84 int _hint; |
|
85 bool _flipped; |
|
86 }; |
|
87 |
|
88 class MinehuntGame : public QObject |
|
89 { |
|
90 Q_OBJECT |
|
91 public: |
|
92 MinehuntGame(); |
|
93 |
|
94 Q_PROPERTY(QDeclarativeListProperty<TileData> tiles READ tiles CONSTANT) |
|
95 QDeclarativeListProperty<TileData> tiles(); |
|
96 |
|
97 Q_PROPERTY(bool isPlaying READ isPlaying NOTIFY isPlayingChanged) |
|
98 bool isPlaying() {return playing;} |
|
99 |
|
100 Q_PROPERTY(bool hasWon READ hasWon NOTIFY hasWonChanged) |
|
101 bool hasWon() {return won;} |
|
102 |
|
103 Q_PROPERTY(int numMines READ numMines NOTIFY numMinesChanged) |
|
104 int numMines() const{return nMines;} |
|
105 |
|
106 Q_PROPERTY(int numFlags READ numFlags NOTIFY numFlagsChanged) |
|
107 int numFlags() const{return nFlags;} |
|
108 |
|
109 public slots: |
|
110 Q_INVOKABLE bool flip(int row, int col); |
|
111 Q_INVOKABLE bool flag(int row, int col); |
|
112 void setBoard(); |
|
113 void reset(); |
|
114 |
|
115 signals: |
|
116 void isPlayingChanged(); |
|
117 void hasWonChanged(); |
|
118 void numMinesChanged(); |
|
119 void numFlagsChanged(); |
|
120 |
|
121 private: |
|
122 bool onBoard( int r, int c ) const { return r >= 0 && r < numRows && c >= 0 && c < numCols; } |
|
123 TileData *tile( int row, int col ) { return onBoard(row, col) ? _tiles[col+numRows*row] : 0; } |
|
124 int getHint(int row, int col); |
|
125 void setPlaying(bool b){if(b==playing) return; playing=b; emit isPlayingChanged();} |
|
126 |
|
127 QList<TileData *> _tiles; |
|
128 int numCols; |
|
129 int numRows; |
|
130 bool playing; |
|
131 bool won; |
|
132 int remaining; |
|
133 int nMines; |
|
134 int nFlags; |
|
135 }; |
|
136 |
|
137 void tilesPropAppend(QDeclarativeListProperty<TileData>* prop, TileData* value) |
|
138 { |
|
139 Q_UNUSED(prop); |
|
140 Q_UNUSED(value); |
|
141 return; //Append not supported |
|
142 } |
|
143 |
|
144 int tilesPropCount(QDeclarativeListProperty<TileData>* prop) |
|
145 { |
|
146 return static_cast<QList<TileData*>*>(prop->data)->count(); |
|
147 } |
|
148 |
|
149 TileData* tilesPropAt(QDeclarativeListProperty<TileData>* prop, int index) |
|
150 { |
|
151 return static_cast<QList<TileData*>*>(prop->data)->at(index); |
|
152 } |
|
153 |
|
154 QDeclarativeListProperty<TileData> MinehuntGame::tiles(){ |
|
155 return QDeclarativeListProperty<TileData>(this, &_tiles, &tilesPropAppend, |
|
156 &tilesPropCount, &tilesPropAt, 0); |
|
157 } |
|
158 |
|
159 MinehuntGame::MinehuntGame() |
|
160 : numCols(9), numRows(9), playing(true), won(false) |
|
161 { |
|
162 setObjectName("mainObject"); |
|
163 srand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
164 |
|
165 //initialize array |
|
166 for(int ii = 0; ii < numRows * numCols; ++ii) { |
|
167 _tiles << new TileData; |
|
168 } |
|
169 reset(); |
|
170 |
|
171 } |
|
172 |
|
173 void MinehuntGame::setBoard() |
|
174 { |
|
175 foreach(TileData* t, _tiles){ |
|
176 t->setHasMine(false); |
|
177 t->setHint(-1); |
|
178 } |
|
179 //place mines |
|
180 int mines = nMines; |
|
181 remaining = numRows*numCols-mines; |
|
182 while ( mines ) { |
|
183 int col = int((double(rand()) / double(RAND_MAX)) * numCols); |
|
184 int row = int((double(rand()) / double(RAND_MAX)) * numRows); |
|
185 |
|
186 TileData* t = tile( row, col ); |
|
187 |
|
188 if (t && !t->hasMine()) { |
|
189 t->setHasMine( true ); |
|
190 mines--; |
|
191 } |
|
192 } |
|
193 |
|
194 //set hints |
|
195 for (int r = 0; r < numRows; r++) |
|
196 for (int c = 0; c < numCols; c++) { |
|
197 TileData* t = tile(r, c); |
|
198 if (t && !t->hasMine()) { |
|
199 int hint = getHint(r,c); |
|
200 t->setHint(hint); |
|
201 } |
|
202 } |
|
203 |
|
204 setPlaying(true); |
|
205 } |
|
206 |
|
207 void MinehuntGame::reset() |
|
208 { |
|
209 foreach(TileData* t, _tiles){ |
|
210 t->unflip(); |
|
211 t->setHasFlag(false); |
|
212 } |
|
213 nMines = 12; |
|
214 nFlags = 0; |
|
215 emit numMinesChanged(); |
|
216 emit numFlagsChanged(); |
|
217 setPlaying(false); |
|
218 QTimer::singleShot(600,this, SLOT(setBoard())); |
|
219 } |
|
220 |
|
221 int MinehuntGame::getHint(int row, int col) |
|
222 { |
|
223 int hint = 0; |
|
224 for (int c = col-1; c <= col+1; c++) |
|
225 for (int r = row-1; r <= row+1; r++) { |
|
226 TileData* t = tile(r, c); |
|
227 if (t && t->hasMine()) |
|
228 hint++; |
|
229 } |
|
230 return hint; |
|
231 } |
|
232 |
|
233 bool MinehuntGame::flip(int row, int col) |
|
234 { |
|
235 if(!playing) |
|
236 return false; |
|
237 |
|
238 TileData *t = tile(row, col); |
|
239 if (!t || t->hasFlag()) |
|
240 return false; |
|
241 |
|
242 if(t->flipped()){ |
|
243 int flags = 0; |
|
244 for (int c = col-1; c <= col+1; c++) |
|
245 for (int r = row-1; r <= row+1; r++) { |
|
246 TileData *nearT = tile(r, c); |
|
247 if(!nearT || nearT == t) |
|
248 continue; |
|
249 if(nearT->hasFlag()) |
|
250 flags++; |
|
251 } |
|
252 if(!t->hint() || t->hint() != flags) |
|
253 return false; |
|
254 for (int c = col-1; c <= col+1; c++) |
|
255 for (int r = row-1; r <= row+1; r++) { |
|
256 TileData *nearT = tile(r, c); |
|
257 if (nearT && !nearT->flipped() && !nearT->hasFlag()) { |
|
258 flip( r, c ); |
|
259 } |
|
260 } |
|
261 return true; |
|
262 } |
|
263 |
|
264 t->flip(); |
|
265 |
|
266 if (t->hint() == 0) { |
|
267 for (int c = col-1; c <= col+1; c++) |
|
268 for (int r = row-1; r <= row+1; r++) { |
|
269 TileData* t = tile(r, c); |
|
270 if (t && !t->flipped()) { |
|
271 flip( r, c ); |
|
272 } |
|
273 } |
|
274 } |
|
275 |
|
276 if(t->hasMine()){ |
|
277 for (int r = 0; r < numRows; r++)//Flip all other mines |
|
278 for (int c = 0; c < numCols; c++) { |
|
279 TileData* t = tile(r, c); |
|
280 if (t && t->hasMine()) { |
|
281 flip(r, c); |
|
282 } |
|
283 } |
|
284 won = false; |
|
285 hasWonChanged(); |
|
286 setPlaying(false); |
|
287 } |
|
288 |
|
289 remaining--; |
|
290 if(!remaining){ |
|
291 won = true; |
|
292 hasWonChanged(); |
|
293 setPlaying(false); |
|
294 } |
|
295 return true; |
|
296 } |
|
297 |
|
298 bool MinehuntGame::flag(int row, int col) |
|
299 { |
|
300 TileData *t = tile(row, col); |
|
301 if(!t) |
|
302 return false; |
|
303 |
|
304 t->setHasFlag(!t->hasFlag()); |
|
305 nFlags += (t->hasFlag()?1:-1); |
|
306 emit numFlagsChanged(); |
|
307 return true; |
|
308 } |
|
309 |
|
310 class MinehuntExtensionPlugin : public QDeclarativeExtensionPlugin |
|
311 { |
|
312 Q_OBJECT |
|
313 |
|
314 public: |
|
315 void registerTypes(const char *uri) { |
|
316 Q_UNUSED(uri); |
|
317 qmlRegisterType<TileData>(); |
|
318 } |
|
319 |
|
320 void initializeEngine(QDeclarativeEngine *engine, const char *uri) { |
|
321 Q_UNUSED(uri); |
|
322 |
|
323 srand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
324 |
|
325 MinehuntGame* game = new MinehuntGame(); |
|
326 |
|
327 engine->rootContext()->setContextObject(game); |
|
328 } |
|
329 }; |
|
330 |
|
331 #include "minehunt.moc" |
|
332 |
|
333 Q_EXPORT_PLUGIN(MinehuntExtensionPlugin); |
|
334 |