summaryrefslogtreecommitdiff
authorzecke <zecke>2004-07-24 18:11:45 (UTC)
committer zecke <zecke>2004-07-24 18:11:45 (UTC)
commit5a8963dff615da61611f5cc4bf410e7a6eeef55b (patch) (unidiff)
tree5a559826d04a2f99a0d00bd8e979c4e9e133c05c
parente7ccf9bd121f98fa4c07b78eae7d36bdfa5b60bc (diff)
downloadopie-5a8963dff615da61611f5cc4bf410e7a6eeef55b.zip
opie-5a8963dff615da61611f5cc4bf410e7a6eeef55b.tar.gz
opie-5a8963dff615da61611f5cc4bf410e7a6eeef55b.tar.bz2
Have a custom grid for the game. Now the name of fifteen is
pretty obsolete but hey it is fun :)
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/fifteen/fifteen.cpp153
-rw-r--r--noncore/games/fifteen/fifteen.h2
-rw-r--r--noncore/games/fifteen/fifteenconfigdialog.cpp4
3 files changed, 108 insertions, 51 deletions
diff --git a/noncore/games/fifteen/fifteen.cpp b/noncore/games/fifteen/fifteen.cpp
index f425e06..bb57ee1 100644
--- a/noncore/games/fifteen/fifteen.cpp
+++ b/noncore/games/fifteen/fifteen.cpp
@@ -1,499 +1,558 @@
1/********************************************************************** 1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. 2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3** 3**
4** This file is part of the Qtopia Environment. 4** This file is part of the Qtopia Environment.
5** 5**
6** This file may be distributed and/or modified under the terms of the 6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software 7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the 8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file. 9** packaging of this file.
10** 10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13** 13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information. 14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15** 15**
16** Contact info@trolltech.com if any conditions of this licensing are 16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20 20
21#include "fifteen.h" 21#include "fifteen.h"
22 22
23#include "fifteenconfigdialog.h" 23#include "fifteenconfigdialog.h"
24 24
25#include <opie2/ofileselector.h> 25#include <opie2/ofileselector.h>
26 26
27#include <qtopia/resource.h> 27#include <qtopia/resource.h>
28#include <qtopia/config.h> 28#include <qtopia/config.h>
29#include <qtopia/qpeapplication.h> 29#include <qtopia/qpeapplication.h>
30 30
31#include <qvbox.h> 31#include <qvbox.h>
32#include <qaction.h> 32#include <qaction.h>
33#include <qpainter.h> 33#include <qpainter.h>
34#include <qmessagebox.h> 34#include <qmessagebox.h>
35#include <qtoolbar.h> 35#include <qtoolbar.h>
36#include <qmenubar.h> 36#include <qmenubar.h>
37#include <qimage.h> 37#include <qimage.h>
38 38
39#include <stdlib.h> 39#include <stdlib.h>
40#include <time.h> 40#include <time.h>
41 41
42FifteenMainWindow::FifteenMainWindow(QWidget *parent, const char* name, WFlags fl) 42FifteenMainWindow::FifteenMainWindow(QWidget *parent, const char* name, WFlags fl)
43 : QMainWindow( parent, name, fl ) 43 : QMainWindow( parent, name, fl )
44{ 44{
45 45
46 // random seed 46 // random seed
47 srand(time(0)); 47 srand(time(0));
48 setCaption( tr("Fifteen Pieces") ); 48 setCaption( tr("Fifteen Pieces") );
49 49
50 QToolBar *toolbar = new QToolBar(this); 50 QToolBar *toolbar = new QToolBar(this);
51 toolbar->setHorizontalStretchable( FALSE ); 51 toolbar->setHorizontalStretchable( FALSE );
52 QMenuBar *menubar = new QMenuBar( toolbar ); 52 QMenuBar *menubar = new QMenuBar( toolbar );
53 menubar->setMargin(0); 53 menubar->setMargin(0);
54 QPopupMenu *game = new QPopupMenu( this ); 54 QPopupMenu *game = new QPopupMenu( this );
55 55
56 QWidget *spacer = new QWidget( toolbar ); 56 QWidget *spacer = new QWidget( toolbar );
57 spacer->setBackgroundMode( PaletteButton ); 57 spacer->setBackgroundMode( PaletteButton );
58 toolbar->setStretchableWidget( spacer ); 58 toolbar->setStretchableWidget( spacer );
59 59
60 60
61 setToolBarsMovable( FALSE ); 61 setToolBarsMovable( FALSE );
62 QVBox *vbox = new QVBox( this ); 62 QVBox *vbox = new QVBox( this );
63 PiecesTable *table = new PiecesTable( vbox ); 63 PiecesTable *table = new PiecesTable( vbox );
64 setCentralWidget(vbox); 64 setCentralWidget(vbox);
65 65
66 66
67 67
68 QAction *a = new QAction( tr( "Randomize" ), Resource::loadPixmap( "new" ), 68 QAction *a = new QAction( tr( "Randomize" ), Resource::loadPixmap( "new" ),
69 QString::null, 0, this, 0 ); 69 QString::null, 0, this, 0 );
70 connect( a, SIGNAL( activated() ), table, SLOT( slotRandomize() ) ); 70 connect( a, SIGNAL( activated() ), table, SLOT( slotRandomize() ) );
71 a->addTo( game ); 71 a->addTo( game );
72 a->addTo( toolbar ); 72 a->addTo( toolbar );
73 73
74 74
75 a = new QAction( tr("Configure"), Resource::loadPixmap( "SettingsIcon" ), 75 a = new QAction( tr("Configure"), Resource::loadPixmap( "SettingsIcon" ),
76 QString::null, 0, this, 0 ); 76 QString::null, 0, this, 0 );
77 connect( a, SIGNAL( activated()), table, SLOT( slotConfigure()) ); 77 connect( a, SIGNAL( activated()), table, SLOT( slotConfigure()) );
78 a->addTo( game ); 78 a->addTo( game );
79 79
80 /* This is pointless and confusing. 80 /* This is pointless and confusing.
81 a = new QAction( tr( "Solve" ), Resource::loadIconSet( "repeat" ), 81 a = new QAction( tr( "Solve" ), Resource::loadIconSet( "repeat" ),
82 QString::null, 0, this, 0 ); 82 QString::null, 0, this, 0 );
83 connect( a, SIGNAL( activated() ), table, SLOT( slotReset() ) ); 83 connect( a, SIGNAL( activated() ), table, SLOT( slotReset() ) );
84 a->addTo( game ); 84 a->addTo( game );
85 a->addTo( toolbar ); 85 a->addTo( toolbar );
86 */ 86 */
87 menubar->insertItem( tr( "Game" ), game ); 87 menubar->insertItem( tr( "Game" ), game );
88} 88}
89 89
90 90
91 91
92 92
93/////////////// 93///////////////
94/////// Pieces table Implementation 94/////// Pieces table Implementation
95/////// 95///////
96PiecesTable::PiecesTable(QWidget* parent, const char* name ) 96PiecesTable::PiecesTable(QWidget* parent, const char* name )
97 : QTableView(parent, name), _menu(0), _randomized(false), 97 : QTableView(parent, name), _menu(0), _randomized(false),
98 _dialog( 0l ) 98 _dialog( 0l )
99{ 99{
100 // setup table view 100 // setup table view
101 setFrameStyle(StyledPanel | Sunken); 101 setFrameStyle(StyledPanel | Sunken);
102 setBackgroundMode(NoBackground); 102 setBackgroundMode(NoBackground);
103 setMouseTracking(true); 103 setMouseTracking(true);
104 104
105 setNumRows(4); 105 setNumRows(4);
106 setNumCols(4); 106 setNumCols(4);
107 107
108 // init arrays 108 // init arrays
109 initMap();
110 readConfig(); 109 readConfig();
111 initColors(); 110 initColors();
112 111
113} 112}
114 113
115 114
116PiecesTable::~PiecesTable() 115PiecesTable::~PiecesTable()
117{ 116{
118 writeConfig(); 117 writeConfig();
119 clear(); 118 clear();
120} 119}
121 120
122void PiecesTable::writeConfig() 121void PiecesTable::writeConfig()
123{ 122{
124 Config cfg("Fifteen"); 123 Config cfg("Fifteen");
125 cfg.setGroup("Game"); 124 cfg.setGroup("Game");
126 QStringList map; 125 QStringList map;
127 for (int i = 0; i < 16; i++) 126
127 int items = numRows()*numCols();
128
129 for (int i = 0; i < items; i++)
128 map.append( QString::number( _map[i] ) ); 130 map.append( QString::number( _map[i] ) );
131
129 cfg.writeEntry("Map", map, '-'); 132 cfg.writeEntry("Map", map, '-');
130 cfg.writeEntry("Randomized", _randomized ); 133 cfg.writeEntry("Randomized", _randomized );
131 cfg.writeEntry("Image", _image ); 134 cfg.writeEntry("Image", _image );
135 cfg.writeEntry("Rows", numRows() );
136 cfg.writeEntry("Cols", numCols() );
132} 137}
133 138
134void PiecesTable::readConfig() 139void PiecesTable::readConfig()
135{ 140{
136 Config cfg("Fifteen"); 141 Config cfg("Fifteen");
137 cfg.setGroup("Game"); 142 cfg.setGroup("Game");
138 QStringList map = cfg.readListEntry("Map", '-'); 143 QStringList map = cfg.readListEntry("Map", '-');
139 _randomized = cfg.readBoolEntry( "Randomized", FALSE ); 144 _randomized = cfg.readBoolEntry( "Randomized", FALSE );
140 _image = cfg.readEntry( "Image", QString::null ); 145 _image = cfg.readEntry( "Image", QString::null );
141 int i = 0; 146
147 int rows = cfg.readNumEntry( "Rows", 4 );
148 int cols = cfg.readNumEntry( "Cols", 4 );
149 uint items= rows*cols;
150 setNumRows( rows );
151 setNumCols( cols );
152
153 initMap();
154
155 /* if we've more items than 'stones' don't restore the state */
156 if ( items > map.count() )
157 return;
158
159
160 uint i = 0;
142 for ( QStringList::Iterator it = map.begin(); it != map.end(); ++it ) { 161 for ( QStringList::Iterator it = map.begin(); it != map.end(); ++it ) {
143 _map[i] = (*it).toInt(); 162 _map[i] = (*it).toInt();
144 i++; 163 i++;
145 if ( i > 15 ) break; 164 if ( i > items ) break;
146 } 165 }
147 166
148} 167}
149 168
150 169
151void PiecesTable::clear() { 170void PiecesTable::clear() {
152 /* clean up and resize */ 171 /* clean up and resize */
153 for (uint i = 0; i < _pixmap.count(); ++i ) 172 for (uint i = 0; i < _pixmap.count(); ++i )
154 delete _pixmap[i]; 173 delete _pixmap[i];
155 _pixmap.resize( 16 ); 174 _pixmap.resize( numRows()*numCols() );
156} 175}
157 176
158/* 177/*
159 * Let us pre-render the tiles. Either we've a Custom Image as 178 * Let us pre-render the tiles. Either we've a Custom Image as
160 * background or we use the drawRect to fill the background and 179 * background or we use the drawRect to fill the background and
161 * last we put the number on it 180 * last we put the number on it
162 */ 181 */
163void PiecesTable::slotCustomImage( const QString& _str , bool upd ) { 182void PiecesTable::slotCustomImage( const QString& _str ) {
164 QString str = _str; 183 QString str = _str;
165 184
166 185
167 /* couldn't load image fall back to plain tiles*/ 186 /* couldn't load image fall back to plain tiles*/
168 QImage img = QImage(str); 187 QImage img = QImage(str);
188 QPixmap pix;
169 if(img.isNull()) 189 if(img.isNull())
170 str = QString::null; 190 str = QString::null;
171 else 191 else{
172 img = img.smoothScale( width(),height() ); 192 img = img.smoothScale( width(),height() );
193 pix.convertFromImage( img );
194 }
173 195
174 QPixmap pix; 196 /* initialize base point */
175 pix.convertFromImage( img );
176
177 uint image=0; 197 uint image=0;
178 198
199 /* clear the old tiles */
179 clear(); 200 clear();
180 201
181 /* used variables */ 202 /* used variables */
182 int cols = numCols(); 203 int cols = numCols();
183 int rows = numRows(); 204 int rows = numRows();
184 int cellW = cellWidth(); 205 int cellW = cellWidth();
185 int cellH = cellHeight(); 206 int cellH = cellHeight();
186 int x2 = cellW-1; 207 int x2 = cellW-1;
187 int y2 = cellH-1; 208 int y2 = cellH-1;
188 bool empty = str.isEmpty(); 209 bool empty = str.isEmpty();
189 double bw = empty ? 0.9 : 0.98; 210 double bw = empty ? 0.9 : 0.98;
190 int x_offset = cellW - int(cellW * bw);// 10% should be enough 211 int x_offset = cellW - int(cellW * bw);// 10% should be enough
191 inty_offset = cellH - int(cellH * bw); 212 inty_offset = cellH - int(cellH * bw);
192 213
193 /* border polygon */ 214 /* border polygon calculation*/
194 initPolygon(cellW, cellH, x_offset, y_offset ); 215 initPolygon(cellW, cellH, x_offset, y_offset );
195 216
217 /* avoid crashes with isNull() pixmap later */
196 if ( cellW == 0 || cellH == 0 ) { 218 if ( cellW == 0 || cellH == 0 ) {
197 _pixmap.resize( 0 ); 219 _pixmap.resize( 0 );
198 return; 220 return;
199 } 221 }
200 222
223 /* make it bold and bigger */
201 QFont f = font(); 224 QFont f = font();
202 f.setPixelSize(18); 225 f.setPixelSize(18);
203 f.setBold( TRUE ); 226 f.setBold( TRUE );
204 227
205 /* for every tile */ 228 /* for every tile */
206 for(int row = 0; row < rows; ++row ) { 229 for(int row = 0; row < rows; ++row ) {
207 for(int col= 0; col < cols; ++col) { 230 for(int col= 0; col < cols; ++col) {
208 QPixmap *pip = new QPixmap(cellW, cellH ); 231 QPixmap *pip = new QPixmap(cellW, cellH );
209 QPainter *p = new QPainter(pip ); 232 QPainter *p = new QPainter(pip );
210 p->setFont( f ); 233 p->setFont( f );
211 234
212 /* draw the tradional tile or a part of the pixmap*/ 235 /* draw the tradional tile or a part of the pixmap*/
213 if(empty) { 236 if(empty) {
214 p->setBrush(_colors[image]); 237 p->setBrush(_colors[image]);
215 p->setPen(NoPen); 238 p->setPen(NoPen);
216 p->drawRect(0,0,cellW,cellH); 239 p->drawRect(0,0,cellW,cellH);
217 }else 240 }else
218 p->drawPixmap(0, 0, pix,col*cellW, row*cellH, cellW, cellH ); 241 p->drawPixmap(0, 0, pix,col*cellW, row*cellH, cellW, cellH );
219 242
220 // draw borders 243 // draw borders
221 if (height() > 40) { 244 if (height() > 40) {
222 p->setBrush(_colors[image].light(130)); 245 p->setBrush(_colors[image].light(130));
223 p->drawPolygon(light_border); 246 p->drawPolygon(light_border);
224 247
225 p->setBrush(_colors[image].dark(130)); 248 p->setBrush(_colors[image].dark(130));
226 p->drawPolygon(dark_border); 249 p->drawPolygon(dark_border);
227 } 250 }
228 251
229 // draw number 252 // draw number
230 p->setPen(black); 253 p->setPen(black);
231 p->drawText(0, 0, x2, y2, AlignHCenter | AlignVCenter, QString::number(image+1)); 254 p->drawText(0, 0, x2, y2, AlignHCenter | AlignVCenter, QString::number(image+1));
232 255
233 delete p; 256 delete p;
234 _pixmap[image++] = pip; 257 _pixmap[image++] = pip;
235 } 258 }
236 } 259 }
237 _image = str; 260 _image = str;
238
239 if ( upd )
240 update();
241} 261}
242 262
243/* 263/*
244 * Calculate 3d-effect borders 264 * Calculate 3d-effect borders
245 */ 265 */
246void PiecesTable::initPolygon(int cell_w, int cell_h, int x_offset, int y_offset ) { 266void PiecesTable::initPolygon(int cell_w, int cell_h, int x_offset, int y_offset ) {
247 light_border.setPoints(6, 267 light_border.setPoints(6,
248 0, 0, 268 0, 0,
249 cell_w, 0, 269 cell_w, 0,
250 cell_w - x_offset, y_offset, 270 cell_w - x_offset, y_offset,
251 x_offset, y_offset, 271 x_offset, y_offset,
252 x_offset, cell_h - y_offset, 272 x_offset, cell_h - y_offset,
253 0, cell_h); 273 0, cell_h);
254 274
255 dark_border.setPoints(6, 275 dark_border.setPoints(6,
256 cell_w, 0, 276 cell_w, 0,
257 cell_w, cell_h, 277 cell_w, cell_h,
258 0, cell_h, 278 0, cell_h,
259 x_offset, cell_h - y_offset, 279 x_offset, cell_h - y_offset,
260 cell_w - x_offset, cell_h - y_offset, 280 cell_w - x_offset, cell_h - y_offset,
261 cell_w - x_offset, y_offset); 281 cell_w - x_offset, y_offset);
262} 282}
263 283
264void PiecesTable::paintCell(QPainter *p, int row, int col) 284void PiecesTable::paintCell(QPainter *p, int row, int col)
265{ 285{
266 int w = cellWidth(); 286 int w = cellWidth();
267 int h = cellHeight(); 287 int h = cellHeight();
268 288
289 uint pos = col+row*numCols();
290
291 /* sanity check. setNumRows()/setNumCols() calls repaint() directly */
292 if ( pos >= _map.count() ) {
293 p->drawRect(0, 0, w, h);
294 return;
295 }
296
269 int number = _map[col + row * numCols()] + 1; 297 int number = _map[col + row * numCols()] + 1;
270 298
271 // draw cell background 299 // draw cell background
272 if(number == 16) { 300 if(number == numCols()*numRows() ) {
273 p->setBrush(colorGroup().background()); 301 p->setBrush(colorGroup().background());
274 p->setPen(NoPen); 302 p->setPen(NoPen);
275 p->drawRect(0, 0, w, h); 303 p->drawRect(0, 0, w, h);
276 return; 304 return;
277 } 305 }
278 306
307 /* no tiles then contentRect() is not visible or too small anyway */
279 if( _pixmap.count() == 0 ) 308 if( _pixmap.count() == 0 )
280 return; 309 return;
281 310
282 p->drawPixmap(0, 0, *(_pixmap[(number-1 )]) ); 311 p->drawPixmap(0, 0, *(_pixmap[(number-1 )]) );
283} 312}
284 313
285void PiecesTable::resizeEvent(QResizeEvent *e) 314void PiecesTable::resizeEvent(QResizeEvent *e)
286{ 315{
287 QTableView::resizeEvent(e); 316 /*
317 * null if we faked it after the config dialog ran to
318 * regenerate everything
319 */
320 if ( e )
321 QTableView::resizeEvent(e);
288 322
289 setCellWidth(contentsRect().width()/ numRows()); 323 setCellWidth(contentsRect().width()/ numCols());
290 setCellHeight(contentsRect().height() / numCols()); 324 setCellHeight(contentsRect().height() / numRows());
291 325
292 326
293 /* update the image and calculate border*/ 327 /* update the image and calculate border*/
294 slotCustomImage( _image ); 328 slotCustomImage( _image );
295 329
296} 330}
297 331
298void PiecesTable::initColors() 332void PiecesTable::initColors()
299{ 333{
300 _colors.resize(numRows() * numCols()); 334 _colors.resize(numRows() * numCols());
301 for (int r = 0; r < numRows(); r++) 335 for (int r = 0; r < numRows(); r++)
302 for (int c = 0; c < numCols(); c++) 336 for (int c = 0; c < numCols(); c++)
303 _colors[c + r *numCols()] = QColor(255 - 70 * c,255 - 70 * r, 150); 337 _colors[c + r *numCols()] = QColor( 255 - (70 * c)%255 ,255 - (70 * r)%255, 150);
304} 338}
305 339
306void PiecesTable::initMap() 340void PiecesTable::initMap()
307{ 341{
308 _map.resize(16); 342 int items = numCols()*numRows();
309 for ( int i = 0; i < 16; i++) 343 _map.resize( items );
344 for ( int i = 0; i < items; i++)
310 _map[i] = i; 345 _map[i] = i;
311 346
312 _randomized = false; 347 _randomized = false;
313} 348}
314 349
315void PiecesTable::randomizeMap() 350void PiecesTable::randomizeMap()
316{ 351{
317 initMap(); 352 initMap();
318 _randomized = true; 353 _randomized = true;
319 // find the free position 354 // find the free position
320 int pos = _map.find(15); 355 int cols = numCols();
356 int rows = numRows();
357 int pos = _map.find( cols*rows -1 );
321 358
322 int move = 0; 359 int move = 0;
323 while ( move < 333 ) { 360 while ( move < 333 ) {
324 361
325 int frow = pos / numCols(); 362 int frow = pos / cols;
326 int fcol = pos - frow * numCols(); 363 int fcol = pos - frow * cols;
327 364
328 // find click position 365 // find click position
329 int row = rand()%4; 366 int row = rand()%rows;
330 int col = rand()%4; 367 int col = rand()%cols;
331 368
332 // sanity check 369 // sanity check
333 if ( row < 0 || row >= numRows() ) continue; 370 if ( row < 0 || row >= rows ) continue;
334 if ( col < 0 || col >= numCols() ) continue; 371 if ( col < 0 || col >= cols ) continue;
335 if ( row != frow && col != fcol ) continue; 372 if ( row != frow && col != fcol ) continue;
336 373
337 move++; 374 move++;
338 375
339 // rows match -> shift pieces 376 // rows match -> shift pieces
340 if(row == frow) { 377 if(row == frow) {
341 378
342 if (col < fcol) { 379 if (col < fcol) {
343 for(int c = fcol; c > col; c--) { 380 for(int c = fcol; c > col; c--) {
344 _map[c + row * numCols()] = _map[ c-1 + row *numCols()]; 381 _map[c + row * cols] = _map[ c-1 + row *cols];
345 } 382 }
346 } 383 }
347 else if (col > fcol) { 384 else if (col > fcol) {
348 for(int c = fcol; c < col; c++) { 385 for(int c = fcol; c < col; c++) {
349 _map[c + row * numCols()] = _map[ c+1 + row *numCols()]; 386 _map[c + row * cols] = _map[ c+1 + row *cols];
350 } 387 }
351 } 388 }
352 } 389 }
353 // cols match -> shift pieces 390 // cols match -> shift pieces
354 else if (col == fcol) { 391 else if (col == fcol) {
355 392
356 if (row < frow) { 393 if (row < frow) {
357 for(int r = frow; r > row; r--) { 394 for(int r = frow; r > row; r--) {
358 _map[col + r * numCols()] = _map[ col + (r-1) *numCols()]; 395 _map[col + r * cols] = _map[ col + (r-1) *cols];
359 } 396 }
360 } 397 }
361 else if (row > frow) { 398 else if (row > frow) {
362 for(int r = frow; r < row; r++) { 399 for(int r = frow; r < row; r++) {
363 _map[col + r * numCols()] = _map[ col + (r+1) *numCols()]; 400 _map[col + r * cols] = _map[ col + (r+1) *cols];
364 } 401 }
365 } 402 }
366 } 403 }
367 // move free cell to click position 404 // move free cell to click position
368 _map[pos=(col + row * numCols())] = 15; 405 _map[pos=(col + row * cols)] = rows*cols-1;
369 } 406 }
370 repaint(); 407 repaint();
371} 408}
372 409
373void PiecesTable::checkwin() 410void PiecesTable::checkwin()
374{ 411{
375 if(!_randomized) return; 412 if(!_randomized) return;
376 413
414 int items=numCols()*numRows();
377 int i; 415 int i;
378 for (i = 0; i < 16; i++) 416 for (i = 0; i < items; i++)
379 if(i != _map[i]) 417 if(i != _map[i])
380 break; 418 break;
381 419
382 if (i == 16) { 420 if (i == items) {
383 QMessageBox::information(this, tr("Fifteen Pieces"), 421 QMessageBox::information(this, tr("Fifteen Pieces"),
384 tr("Congratulations!\nYou win the game!")); 422 tr("Congratulations!\nYou win the game!"));
385 _randomized = FALSE; 423 _randomized = FALSE;
386 } 424 }
387 425
388} 426}
389 427
390void PiecesTable::slotRandomize() 428void PiecesTable::slotRandomize()
391{ 429{
392 randomizeMap(); 430 randomizeMap();
393} 431}
394 432
395void PiecesTable::slotReset() 433void PiecesTable::slotReset()
396{ 434{
397 initMap(); 435 initMap();
398 repaint(); 436 repaint();
399} 437}
400 438
401void PiecesTable::mousePressEvent(QMouseEvent* e) 439void PiecesTable::mousePressEvent(QMouseEvent* e)
402{ 440{
403 QTableView::mousePressEvent(e); 441 QTableView::mousePressEvent(e);
404 442
405 if (e->button() == RightButton) { 443 if (e->button() == RightButton) {
406 444
407 // setup RMB pupup menu 445 // setup RMB pupup menu
408 if(!_menu) { 446 if(!_menu) {
409 _menu = new QPopupMenu(this); 447 _menu = new QPopupMenu(this);
410 _menu->insertItem(tr("R&andomize Pieces"), mRandomize); 448 _menu->insertItem(tr("R&andomize Pieces"), mRandomize);
411 _menu->insertItem(tr("&Reset Pieces"), mReset); 449 _menu->insertItem(tr("&Reset Pieces"), mReset);
412 _menu->adjustSize(); 450 _menu->adjustSize();
413 } 451 }
414 452
415 // execute RMB popup and check result 453 // execute RMB popup and check result
416 switch(_menu->exec(mapToGlobal(e->pos()))) { 454 switch(_menu->exec(mapToGlobal(e->pos()))) {
417 case mRandomize: 455 case mRandomize:
418 randomizeMap(); 456 randomizeMap();
419 break; 457 break;
420 case mReset: 458 case mReset:
421 initMap(); 459 initMap();
422 repaint(); 460 repaint();
423 break; 461 break;
424 default: 462 default:
425 break; 463 break;
426 } 464 }
427 } 465 }
428 else { 466 else {
429 // GAME LOGIC 467 // GAME LOGIC
468 int cols = numCols();
469 int rows = numRows();
470 int item = cols*rows -1;
430 471
431 // find the free position 472 // find the free position
432 int pos = _map.find(15); 473 int pos = _map.find(item);
433 if(pos < 0) return; 474 if(pos < 0) return;
434 475
435 int frow = pos / numCols(); 476 int frow = pos / cols;
436 int fcol = pos - frow * numCols(); 477 int fcol = pos - frow * cols;
437 478
438 // find click position 479 // find click position
439 int row = findRow(e->y()); 480 int row = findRow(e->y());
440 int col = findCol(e->x()); 481 int col = findCol(e->x());
441 482
442 // sanity check 483 // sanity check
443 if (row < 0 || row >= numRows()) return; 484 if (row < 0 || row >= rows) return;
444 if (col < 0 || col >= numCols()) return; 485 if (col < 0 || col >= cols) return;
445 if ( row != frow && col != fcol ) return; 486 if ( row != frow && col != fcol ) return;
446 487
447 // valid move? 488 // valid move?
448 if(row != frow && col != fcol) return; 489 if(row != frow && col != fcol) return;
449 490
450 // rows match -> shift pieces 491 // rows match -> shift pieces
451 if(row == frow) { 492 if(row == frow) {
452 493
453 if (col < fcol) { 494 if (col < fcol) {
454 for(int c = fcol; c > col; c--) { 495 for(int c = fcol; c > col; c--) {
455 _map[c + row * numCols()] = _map[ c-1 + row *numCols()]; 496 _map[c + row * cols] = _map[ c-1 + row *cols];
456 updateCell(row, c, false); 497 updateCell(row, c, false);
457 } 498 }
458 } 499 }
459 else if (col > fcol) { 500 else if (col > fcol) {
460 for(int c = fcol; c < col; c++) { 501 for(int c = fcol; c < col; c++) {
461 _map[c + row * numCols()] = _map[ c+1 + row *numCols()]; 502 _map[c + row * cols] = _map[ c+1 + row *cols];
462 updateCell(row, c, false); 503 updateCell(row, c, false);
463 } 504 }
464 } 505 }
465 } 506 }
466 // cols match -> shift pieces 507 // cols match -> shift pieces
467 else if (col == fcol) { 508 else if (col == fcol) {
468 509
469 if (row < frow) { 510 if (row < frow) {
470 for(int r = frow; r > row; r--) { 511 for(int r = frow; r > row; r--) {
471 _map[col + r * numCols()] = _map[ col + (r-1) *numCols()]; 512 _map[col + r * cols] = _map[ col + (r-1) *cols];
472 updateCell(r, col, false); 513 updateCell(r, col, false);
473 } 514 }
474 } 515 }
475 else if (row > frow) { 516 else if (row > frow) {
476 for(int r = frow; r < row; r++) { 517 for(int r = frow; r < row; r++) {
477 _map[col + r * numCols()] = _map[ col + (r+1) *numCols()]; 518 _map[col + r * cols] = _map[ col + (r+1) *cols];
478 updateCell(r, col, false); 519 updateCell(r, col, false);
479 } 520 }
480 } 521 }
481 } 522 }
482 // move free cell to click position 523 // move free cell to click position
483 _map[col + row * numCols()] = 15; 524 _map[col + row * cols] = item;
484 updateCell(row, col, false); 525 updateCell(row, col, false);
485 526
486 // check if the player wins with this move 527 // check if the player wins with this move
487 checkwin(); 528 checkwin();
488 } 529 }
489} 530}
490 531
491void PiecesTable::slotConfigure() { 532void PiecesTable::slotConfigure() {
492 if ( !_dialog ) 533 if ( !_dialog )
493 _dialog = new FifteenConfigDialog(this, "Fifteen Configure Dialog", true ); 534 _dialog = new FifteenConfigDialog(this, "Fifteen Configure Dialog", true );
494 535
495 536
496 _dialog->setImageSrc( _image ); 537 _dialog->setImageSrc( _image );
497 if ( QPEApplication::execDialog(_dialog) == QDialog::Accepted ) 538 _dialog->setGameboard( numRows(), numCols() );
498 slotCustomImage( _dialog->imageSrc(), true ); 539
540 if ( QPEApplication::execDialog(_dialog) == QDialog::Accepted ) {
541 /*
542 * update the board grid and reinit the game if changed
543 * First set new columns so the update will regenerate the
544 * tiles with slotCustomImage
545 */
546 _image = _dialog->imageSrc();
547 if (numRows() != _dialog->rows() ||
548 numCols() != _dialog->columns() ) {
549 setNumCols(_dialog->columns());
550 setNumRows(_dialog->rows());
551 slotReset();
552 }
553 resizeEvent( 0l );
554
555
556 update();
557 }
499} 558}
diff --git a/noncore/games/fifteen/fifteen.h b/noncore/games/fifteen/fifteen.h
index 4b8702d..c70afc8 100644
--- a/noncore/games/fifteen/fifteen.h
+++ b/noncore/games/fifteen/fifteen.h
@@ -1,102 +1,102 @@
1/********************************************************************** 1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. 2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3** 3**
4** This file is part of the Qtopia Environment. 4** This file is part of the Qtopia Environment.
5** 5**
6** This file may be distributed and/or modified under the terms of the 6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software 7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the 8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file. 9** packaging of this file.
10** 10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13** 13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information. 14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15** 15**
16** Contact info@trolltech.com if any conditions of this licensing are 16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20 20
21#ifndef __fifteenapplet_h__ 21#ifndef __fifteenapplet_h__
22#define __fifteenapplet_h__ 22#define __fifteenapplet_h__
23 23
24#include <qmainwindow.h> 24#include <qmainwindow.h>
25#include <qtableview.h> 25#include <qtableview.h>
26#include <qarray.h> 26#include <qarray.h>
27#include <qpointarray.h> 27#include <qpointarray.h>
28#include <qpixmap.h> 28#include <qpixmap.h>
29#include <qvector.h> 29#include <qvector.h>
30 30
31class QPopupMenu; 31class QPopupMenu;
32class FifteenConfigDialog; 32class FifteenConfigDialog;
33 33
34class PiecesTable : public QTableView 34class PiecesTable : public QTableView
35{ 35{
36 Q_OBJECT 36 Q_OBJECT
37 37
38 public: 38 public:
39 PiecesTable(QWidget* parent = 0, const char* name = 0); 39 PiecesTable(QWidget* parent = 0, const char* name = 0);
40 ~PiecesTable(); 40 ~PiecesTable();
41 41
42 protected slots: 42 protected slots:
43 void slotConfigure(); 43 void slotConfigure();
44 void slotCustomImage(const QString &str, bool upd = false); 44 void slotCustomImage(const QString &str);
45 void slotRandomize(); 45 void slotRandomize();
46 void slotReset(); 46 void slotReset();
47 47
48 protected: 48 protected:
49 void resizeEvent(QResizeEvent*); 49 void resizeEvent(QResizeEvent*);
50 void mousePressEvent(QMouseEvent*); 50 void mousePressEvent(QMouseEvent*);
51 51
52 void paintCell(QPainter *, int row, int col); 52 void paintCell(QPainter *, int row, int col);
53 53
54 void initImage(); 54 void initImage();
55 void initMap(); 55 void initMap();
56 void initColors(); 56 void initColors();
57 void randomizeMap(); 57 void randomizeMap();
58 void checkwin(); 58 void checkwin();
59 void readConfig(); 59 void readConfig();
60 void writeConfig(); 60 void writeConfig();
61 61
62 void initPolygon(int w, int h, int x_of, int y_of ); 62 void initPolygon(int w, int h, int x_of, int y_of );
63 private: 63 private:
64 void clear(); 64 void clear();
65 QString _image; 65 QString _image;
66 QArray<int> _map; 66 QArray<int> _map;
67 QArray<QColor> _colors; 67 QArray<QColor> _colors;
68 QArray<QPixmap*> _pixmap; 68 QArray<QPixmap*> _pixmap;
69 69
70 QPopupMenu *_menu; 70 QPopupMenu *_menu;
71 bool _randomized; 71 bool _randomized;
72 QPointArraylight_border; 72 QPointArraylight_border;
73 QPointArraydark_border; 73 QPointArraydark_border;
74 FifteenConfigDialog *_dialog; 74 FifteenConfigDialog *_dialog;
75 75
76 enum MenuOp { mRandomize = 1, mReset = 2 }; 76 enum MenuOp { mRandomize = 1, mReset = 2 };
77}; 77};
78 78
79class FifteenWidget : public QWidget 79class FifteenWidget : public QWidget
80{ 80{
81 Q_OBJECT 81 Q_OBJECT
82 82
83public: 83public:
84 FifteenWidget(QWidget *parent = 0, const char *name = 0); 84 FifteenWidget(QWidget *parent = 0, const char *name = 0);
85 85
86private: 86private:
87 PiecesTable *_table; 87 PiecesTable *_table;
88}; 88};
89 89
90 90
91class FifteenMainWindow : public QMainWindow 91class FifteenMainWindow : public QMainWindow
92{ 92{
93 Q_OBJECT 93 Q_OBJECT
94 94
95public: 95public:
96 static QString appName() { 96 static QString appName() {
97 return QString::fromLatin1("fifteen"); 97 return QString::fromLatin1("fifteen");
98 } 98 }
99 FifteenMainWindow(QWidget *parent=0, const char* name=0, WFlags fl=0); 99 FifteenMainWindow(QWidget *parent=0, const char* name=0, WFlags fl=0);
100}; 100};
101 101
102#endif 102#endif
diff --git a/noncore/games/fifteen/fifteenconfigdialog.cpp b/noncore/games/fifteen/fifteenconfigdialog.cpp
index 3f974f8..8539e0e 100644
--- a/noncore/games/fifteen/fifteenconfigdialog.cpp
+++ b/noncore/games/fifteen/fifteenconfigdialog.cpp
@@ -1,112 +1,110 @@
1/* 1/*
2               =. This file is part of the OPIE Project 2               =. This file is part of the OPIE Project
3             .=l. Copyright (c) 2002 <> 3             .=l. Copyright (c) 2002 <>
4           .>+-= 4           .>+-=
5 _;:,     .>    :=|. This program is free software; you can 5 _;:,     .>    :=|. This program is free software; you can
6.> <`_,   >  .   <= redistribute it and/or modify it under 6.> <`_,   >  .   <= redistribute it and/or modify it under
7:`=1 )Y*s>-.--   : the terms of the GNU General Public 7:`=1 )Y*s>-.--   : the terms of the GNU General Public
8.="- .-=="i,     .._ License as published by the Free Software 8.="- .-=="i,     .._ License as published by the Free Software
9 - .   .-<_>     .<> Foundation; either version 2 of the License, 9 - .   .-<_>     .<> Foundation; either version 2 of the License,
10     ._= =}       : or (at your option) any later version. 10     ._= =}       : or (at your option) any later version.
11    .%`+i>       _;_. 11    .%`+i>       _;_.
12    .i_,=:_.      -<s. This program is distributed in the hope that 12    .i_,=:_.      -<s. This program is distributed in the hope that
13     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY; 13     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY;
14    : ..    .:,     . . . without even the implied warranty of 14    : ..    .:,     . . . without even the implied warranty of
15    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A 15    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A
16  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU 16  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU
17..}^=.=       =       ; Library General Public License for more 17..}^=.=       =       ; Library General Public License for more
18++=   -.     .`     .: details. 18++=   -.     .`     .: details.
19 :     =  ...= . :.=- 19 :     =  ...= . :.=-
20 -.   .:....=;==+<; You should have received a copy of the GNU 20 -.   .:....=;==+<; You should have received a copy of the GNU
21  -_. . .   )=.  = General Public License along with 21  -_. . .   )=.  = General Public License along with
22    --        :-=` this library; see the file COPYING.LIB. 22    --        :-=` this library; see the file COPYING.LIB.
23 If not, write to the Free Software Foundation, 23 If not, write to the Free Software Foundation,
24 Inc., 59 Temple Place - Suite 330, 24 Inc., 59 Temple Place - Suite 330,
25 Boston, MA 02111-1307, USA. 25 Boston, MA 02111-1307, USA.
26 26
27*/ 27*/
28 28
29 29
30#include "fifteenconfigdialog.h" 30#include "fifteenconfigdialog.h"
31 31
32#include <opie2/ofiledialog.h> 32#include <opie2/ofiledialog.h>
33 33
34#include <qimage.h> 34#include <qimage.h>
35#include <qlabel.h> 35#include <qlabel.h>
36#include <qspinbox.h> 36#include <qspinbox.h>
37#include <qlineedit.h> 37#include <qlineedit.h>
38#include <qcheckbox.h> 38#include <qcheckbox.h>
39#include <qgroupbox.h> 39#include <qgroupbox.h>
40 40
41 41
42using Opie::Ui::OFileSelector; 42using Opie::Ui::OFileSelector;
43using Opie::Ui::OFileDialog; 43using Opie::Ui::OFileDialog;
44 44
45 45
46FifteenConfigDialog::FifteenConfigDialog( QWidget* parent, const char* name, bool modal ) 46FifteenConfigDialog::FifteenConfigDialog( QWidget* parent, const char* name, bool modal )
47 : FifteenConfigDialogBase( parent, name, modal ) 47 : FifteenConfigDialogBase( parent, name, modal )
48{ 48{}
49 grpGameGrid->hide();
50}
51 49
52FifteenConfigDialog::~FifteenConfigDialog() 50FifteenConfigDialog::~FifteenConfigDialog()
53{} 51{}
54 52
55/** 53/**
56 * src.isEmpty() means no Custom Image to be set 54 * src.isEmpty() means no Custom Image to be set
57 */ 55 */
58void FifteenConfigDialog::setImageSrc( const QString& src ) { 56void FifteenConfigDialog::setImageSrc( const QString& src ) {
59 ckbCustomImage->setChecked( !src.isEmpty() ); 57 ckbCustomImage->setChecked( !src.isEmpty() );
60 lneImage->setText( src ); 58 lneImage->setText( src );
61 lblPreview->setPixmap( preview(src ) ); 59 lblPreview->setPixmap( preview(src ) );
62} 60}
63 61
64/* 62/*
65 * If the return isEmpty() this means no custom image is wished 63 * If the return isEmpty() this means no custom image is wished
66 */ 64 */
67QString FifteenConfigDialog::imageSrc()const { 65QString FifteenConfigDialog::imageSrc()const {
68 return ckbCustomImage->isChecked() ? lneImage->text() : QString::null; 66 return ckbCustomImage->isChecked() ? lneImage->text() : QString::null;
69} 67}
70 68
71void FifteenConfigDialog::setGameboard( int rows, int columns ) { 69void FifteenConfigDialog::setGameboard( int rows, int columns ) {
72 spnRow->setValue( rows ); 70 spnRow->setValue( rows );
73 spnCol->setValue( columns ); 71 spnCol->setValue( columns );
74} 72}
75 73
76 74
77int FifteenConfigDialog::columns()const { 75int FifteenConfigDialog::columns()const {
78 return spnCol->value(); 76 return spnCol->value();
79} 77}
80 78
81int FifteenConfigDialog::rows() const{ 79int FifteenConfigDialog::rows() const{
82 return spnRow->value(); 80 return spnRow->value();
83} 81}
84 82
85void FifteenConfigDialog::slotLoadImage() { 83void FifteenConfigDialog::slotLoadImage() {
86 QStringList lst; 84 QStringList lst;
87 lst << "image/*"; 85 lst << "image/*";
88 MimeTypes type; 86 MimeTypes type;
89 type.insert( tr("All Images" ), lst ); 87 type.insert( tr("All Images" ), lst );
90 type.insert( tr("All Files"), "*/*" ); 88 type.insert( tr("All Files"), "*/*" );
91 89
92 90
93 QString str = OFileDialog::getOpenFileName(OFileSelector::Normal, 91 QString str = OFileDialog::getOpenFileName(OFileSelector::Normal,
94 QString::null, QString::null, 92 QString::null, QString::null,
95 type, this, 93 type, this,
96 tr("Select board background") ); 94 tr("Select board background") );
97 if (!str.isEmpty() ) 95 if (!str.isEmpty() )
98 setImageSrc( str ); 96 setImageSrc( str );
99} 97}
100 98
101 99
102QPixmap FifteenConfigDialog::preview( const QString& file ) { 100QPixmap FifteenConfigDialog::preview( const QString& file ) {
103 QPixmap pix; 101 QPixmap pix;
104 QImage img( file ); 102 QImage img( file );
105 if( img.isNull() ) 103 if( img.isNull() )
106 return pix; 104 return pix;
107 105
108 img = img.smoothScale(120, 120 ); 106 img = img.smoothScale(120, 120 );
109 pix.convertFromImage( img ); 107 pix.convertFromImage( img );
110 108
111 return pix; 109 return pix;
112} 110}