1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the demonstration applications of the Qt Toolkit.
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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
48 void tilesPropAppend(QDeclarativeListProperty<TileData>* prop, TileData* value)
52 return; //Append not supported
55 int tilesPropCount(QDeclarativeListProperty<TileData>* prop)
57 return static_cast<QList<TileData*>*>(prop->data)->count();
60 TileData* tilesPropAt(QDeclarativeListProperty<TileData>* prop, int index)
62 return static_cast<QList<TileData*>*>(prop->data)->at(index);
65 QDeclarativeListProperty<TileData> MinehuntGame::tiles(){
66 return QDeclarativeListProperty<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();