summaryrefslogtreecommitdiff
Unidiff
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
@@ -107,5 +107,4 @@ PiecesTable::PiecesTable(QWidget* parent, const char* name )
107 107
108 // init arrays 108 // init arrays
109 initMap();
110 readConfig(); 109 readConfig();
111 initColors(); 110 initColors();
@@ -125,9 +124,15 @@ void PiecesTable::writeConfig()
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
@@ -139,9 +144,23 @@ void PiecesTable::readConfig()
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
@@ -153,5 +172,5 @@ void PiecesTable::clear() {
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
@@ -161,5 +180,5 @@ void PiecesTable::clear() {
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
@@ -167,14 +186,16 @@ void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
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
@@ -191,7 +212,8 @@ void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
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 );
@@ -199,4 +221,5 @@ void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
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);
@@ -236,7 +259,4 @@ void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
236 } 259 }
237 _image = str; 260 _image = str;
238
239 if ( upd )
240 update();
241} 261}
242 262
@@ -267,8 +287,16 @@ void PiecesTable::paintCell(QPainter *p, int row, int col)
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);
@@ -277,4 +305,5 @@ void PiecesTable::paintCell(QPainter *p, int row, int col)
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;
@@ -285,8 +314,13 @@ void PiecesTable::paintCell(QPainter *p, int row, int col)
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
@@ -301,11 +335,12 @@ void PiecesTable::initColors()
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
@@ -318,19 +353,21 @@ void PiecesTable::randomizeMap()
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
@@ -342,10 +379,10 @@ void PiecesTable::randomizeMap()
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 }
@@ -356,15 +393,15 @@ void PiecesTable::randomizeMap()
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();
@@ -375,10 +412,11 @@ void PiecesTable::checkwin()
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!"));
@@ -428,11 +466,14 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
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
@@ -441,6 +482,6 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
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
@@ -453,5 +494,5 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
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 }
@@ -459,5 +500,5 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
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 }
@@ -469,5 +510,5 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
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 }
@@ -475,5 +516,5 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
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 }
@@ -481,5 +522,5 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
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
@@ -495,5 +536,23 @@ void PiecesTable::slotConfigure() {
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
@@ -42,5 +42,5 @@ class PiecesTable : public QTableView
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();
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
@@ -46,7 +46,5 @@ using Opie::Ui::OFileDialog;
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()