1
/****************************************************************************
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/
6
** This file is part of the demonstration applications of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** GNU Lesser General Public License Usage
10
** This file may be used under the terms of the GNU Lesser General Public
11
** License version 2.1 as published by the Free Software Foundation and
12
** appearing in the file LICENSE.LGPL included in the packaging of this
13
** file. Please review the following information to ensure the GNU Lesser
14
** General Public License version 2.1 requirements will be met:
15
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17
** In addition, as a special exception, Nokia gives you certain additional
18
** rights. These rights are described in the Nokia Qt LGPL Exception
19
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21
** GNU General Public License Usage
22
** Alternatively, this file may be used under the terms of the GNU General
23
** Public License version 3.0 as published by the Free Software Foundation
24
** and appearing in the file LICENSE.GPL included in the packaging of this
25
** file. Please review the following information to ensure the GNU General
26
** Public License version 3.0 requirements will be met:
27
** http://www.gnu.org/copyleft/gpl.html.
30
** Alternatively, this file may be used in accordance with the terms and
31
** conditions contained in a signed written agreement between you and Nokia.
40
****************************************************************************/
48
void tilesPropAppend(QQmlListProperty<TileData>* prop, TileData* value)
52
return; //Append not supported
55
int tilesPropCount(QQmlListProperty<TileData>* prop)
57
return static_cast<QList<TileData*>*>(prop->data)->count();
60
TileData* tilesPropAt(QQmlListProperty<TileData>* prop, int index)
62
return static_cast<QList<TileData*>*>(prop->data)->at(index);
65
QQmlListProperty<TileData> MinehuntGame::tiles(){
66
return QQmlListProperty<TileData>(this, &_tiles, &tilesPropAppend,
67
&tilesPropCount, &tilesPropAt, 0);
70
MinehuntGame::MinehuntGame()
71
: numCols(9), numRows(9), playing(true), won(false)
73
setObjectName("mainObject");
74
srand(QTime(0,0,0).secsTo(QTime::currentTime()));
77
for(int ii = 0; ii < numRows * numCols; ++ii) {
78
_tiles << new TileData;
84
void MinehuntGame::setBoard()
86
foreach(TileData* t, _tiles){
92
remaining = numRows*numCols-mines;
94
int col = int((double(rand()) / double(RAND_MAX)) * numCols);
95
int row = int((double(rand()) / double(RAND_MAX)) * numRows);
97
TileData* t = tile( row, col );
99
if (t && !t->hasMine()) {
100
t->setHasMine( true );
106
for (int r = 0; r < numRows; r++)
107
for (int c = 0; c < numCols; c++) {
108
TileData* t = tile(r, c);
109
if (t && !t->hasMine()) {
110
int hint = getHint(r,c);
118
void MinehuntGame::reset()
120
foreach(TileData* t, _tiles){
122
t->setHasFlag(false);
126
emit numMinesChanged();
127
emit numFlagsChanged();
129
QTimer::singleShot(600,this, SLOT(setBoard()));
132
int MinehuntGame::getHint(int row, int col)
135
for (int c = col-1; c <= col+1; c++)
136
for (int r = row-1; r <= row+1; r++) {
137
TileData* t = tile(r, c);
138
if (t && t->hasMine())
144
bool MinehuntGame::flip(int row, int col)
149
TileData *t = tile(row, col);
150
if (!t || t->hasFlag())
155
for (int c = col-1; c <= col+1; c++)
156
for (int r = row-1; r <= row+1; r++) {
157
TileData *nearT = tile(r, c);
158
if(!nearT || nearT == t)
163
if(!t->hint() || t->hint() != flags)
165
for (int c = col-1; c <= col+1; c++)
166
for (int r = row-1; r <= row+1; r++) {
167
TileData *nearT = tile(r, c);
168
if (nearT && !nearT->flipped() && !nearT->hasFlag()) {
177
if (t->hint() == 0) {
178
for (int c = col-1; c <= col+1; c++)
179
for (int r = row-1; r <= row+1; r++) {
180
TileData* t = tile(r, c);
181
if (t && !t->flipped()) {
188
for (int r = 0; r < numRows; r++)//Flip all other mines
189
for (int c = 0; c < numCols; c++) {
190
TileData* t = tile(r, c);
191
if (t && t->hasMine()) {
211
bool MinehuntGame::flag(int row, int col)
213
TileData *t = tile(row, col);
214
if(!t || !playing || t->flipped())
217
t->setHasFlag(!t->hasFlag());
218
nFlags += (t->hasFlag()?1:-1);
219
emit numFlagsChanged();