summaryrefslogtreecommitdiff
path: root/noncore/games
Unidiff
Diffstat (limited to 'noncore/games') (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
@@ -108,3 +108,2 @@ PiecesTable::PiecesTable(QWidget* parent, const char* name )
108 // init arrays 108 // init arrays
109 initMap();
110 readConfig(); 109 readConfig();
@@ -126,4 +125,8 @@ void PiecesTable::writeConfig()
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, '-');
@@ -131,2 +134,4 @@ void PiecesTable::writeConfig()
131 cfg.writeEntry("Image", _image ); 134 cfg.writeEntry("Image", _image );
135 cfg.writeEntry("Rows", numRows() );
136 cfg.writeEntry("Cols", numCols() );
132} 137}
@@ -140,3 +145,17 @@ void PiecesTable::readConfig()
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 ) {
@@ -144,3 +163,3 @@ void PiecesTable::readConfig()
144 i++; 163 i++;
145 if ( i > 15 ) break; 164 if ( i > items ) break;
146 } 165 }
@@ -154,3 +173,3 @@ void PiecesTable::clear() {
154 delete _pixmap[i]; 173 delete _pixmap[i];
155 _pixmap.resize( 16 ); 174 _pixmap.resize( numRows()*numCols() );
156} 175}
@@ -162,3 +181,3 @@ void PiecesTable::clear() {
162 */ 181 */
163void PiecesTable::slotCustomImage( const QString& _str , bool upd ) { 182void PiecesTable::slotCustomImage( const QString& _str ) {
164 QString str = _str; 183 QString str = _str;
@@ -168,12 +187,14 @@ void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
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();
@@ -192,5 +213,6 @@ void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
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 ) {
@@ -200,2 +222,3 @@ void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
200 222
223 /* make it bold and bigger */
201 QFont f = font(); 224 QFont f = font();
@@ -237,5 +260,2 @@ void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
237 _image = str; 260 _image = str;
238
239 if ( upd )
240 update();
241} 261}
@@ -268,2 +288,10 @@ void PiecesTable::paintCell(QPainter *p, int row, int col)
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;
@@ -271,3 +299,3 @@ void PiecesTable::paintCell(QPainter *p, int row, int col)
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());
@@ -278,2 +306,3 @@ void PiecesTable::paintCell(QPainter *p, int row, int col)
278 306
307 /* no tiles then contentRect() is not visible or too small anyway */
279 if( _pixmap.count() == 0 ) 308 if( _pixmap.count() == 0 )
@@ -286,6 +315,11 @@ void 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
@@ -302,3 +336,3 @@ void PiecesTable::initColors()
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}
@@ -307,4 +341,5 @@ void 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;
@@ -319,3 +354,5 @@ void PiecesTable::randomizeMap()
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
@@ -324,12 +361,12 @@ void PiecesTable::randomizeMap()
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;
@@ -343,3 +380,3 @@ void PiecesTable::randomizeMap()
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 }
@@ -348,3 +385,3 @@ void PiecesTable::randomizeMap()
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 }
@@ -357,3 +394,3 @@ void PiecesTable::randomizeMap()
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 }
@@ -362,3 +399,3 @@ void PiecesTable::randomizeMap()
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 }
@@ -367,3 +404,3 @@ void PiecesTable::randomizeMap()
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 }
@@ -376,4 +413,5 @@ void PiecesTable::checkwin()
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])
@@ -381,3 +419,3 @@ void PiecesTable::checkwin()
381 419
382 if (i == 16) { 420 if (i == items) {
383 QMessageBox::information(this, tr("Fifteen Pieces"), 421 QMessageBox::information(this, tr("Fifteen Pieces"),
@@ -429,9 +467,12 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
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
@@ -442,4 +483,4 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
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;
@@ -454,3 +495,3 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
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);
@@ -460,3 +501,3 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
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);
@@ -470,3 +511,3 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
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);
@@ -476,3 +517,3 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
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);
@@ -482,3 +523,3 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
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);
@@ -496,4 +537,22 @@ void PiecesTable::slotConfigure() {
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
@@ -43,3 +43,3 @@ class PiecesTable : public QTableView
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();
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
@@ -47,5 +47,3 @@ FifteenConfigDialog::FifteenConfigDialog( QWidget* parent, const char* name, boo
47 : FifteenConfigDialogBase( parent, name, modal ) 47 : FifteenConfigDialogBase( parent, name, modal )
48{ 48{}
49 grpGameGrid->hide();
50}
51 49