summaryrefslogtreecommitdiff
Side-by-side diff
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--noncore/games/fifteen/fifteen.cpp149
-rw-r--r--noncore/games/fifteen/fifteen.h2
-rw-r--r--noncore/games/fifteen/fifteenconfigdialog.cpp4
3 files changed, 106 insertions, 49 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 )
// init arrays
- initMap();
readConfig();
@@ -126,4 +125,8 @@ void PiecesTable::writeConfig()
QStringList map;
- for (int i = 0; i < 16; i++)
+
+ int items = numRows()*numCols();
+
+ for (int i = 0; i < items; i++)
map.append( QString::number( _map[i] ) );
+
cfg.writeEntry("Map", map, '-');
@@ -131,2 +134,4 @@ void PiecesTable::writeConfig()
cfg.writeEntry("Image", _image );
+ cfg.writeEntry("Rows", numRows() );
+ cfg.writeEntry("Cols", numCols() );
}
@@ -140,3 +145,17 @@ void PiecesTable::readConfig()
_image = cfg.readEntry( "Image", QString::null );
- int i = 0;
+
+ int rows = cfg.readNumEntry( "Rows", 4 );
+ int cols = cfg.readNumEntry( "Cols", 4 );
+ uint items= rows*cols;
+ setNumRows( rows );
+ setNumCols( cols );
+
+ initMap();
+
+ /* if we've more items than 'stones' don't restore the state */
+ if ( items > map.count() )
+ return;
+
+
+ uint i = 0;
for ( QStringList::Iterator it = map.begin(); it != map.end(); ++it ) {
@@ -144,3 +163,3 @@ void PiecesTable::readConfig()
i++;
- if ( i > 15 ) break;
+ if ( i > items ) break;
}
@@ -154,3 +173,3 @@ void PiecesTable::clear() {
delete _pixmap[i];
- _pixmap.resize( 16 );
+ _pixmap.resize( numRows()*numCols() );
}
@@ -162,3 +181,3 @@ void PiecesTable::clear() {
*/
-void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
+void PiecesTable::slotCustomImage( const QString& _str ) {
QString str = _str;
@@ -168,12 +187,14 @@ void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
QImage img = QImage(str);
+ QPixmap pix;
if(img.isNull())
str = QString::null;
- else
+ else{
img = img.smoothScale( width(),height() );
-
- QPixmap pix;
pix.convertFromImage( img );
+ }
+ /* initialize base point */
uint image=0;
+ /* clear the old tiles */
clear();
@@ -192,5 +213,6 @@ void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
- /* border polygon */
+ /* border polygon calculation*/
initPolygon(cellW, cellH, x_offset, y_offset );
+ /* avoid crashes with isNull() pixmap later */
if ( cellW == 0 || cellH == 0 ) {
@@ -200,2 +222,3 @@ void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
+ /* make it bold and bigger */
QFont f = font();
@@ -237,5 +260,2 @@ void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
_image = str;
-
- if ( upd )
- update();
}
@@ -268,2 +288,10 @@ void PiecesTable::paintCell(QPainter *p, int row, int col)
+ uint pos = col+row*numCols();
+
+ /* sanity check. setNumRows()/setNumCols() calls repaint() directly */
+ if ( pos >= _map.count() ) {
+ p->drawRect(0, 0, w, h);
+ return;
+ }
+
int number = _map[col + row * numCols()] + 1;
@@ -271,3 +299,3 @@ void PiecesTable::paintCell(QPainter *p, int row, int col)
// draw cell background
- if(number == 16) {
+ if(number == numCols()*numRows() ) {
p->setBrush(colorGroup().background());
@@ -278,2 +306,3 @@ void PiecesTable::paintCell(QPainter *p, int row, int col)
+ /* no tiles then contentRect() is not visible or too small anyway */
if( _pixmap.count() == 0 )
@@ -286,6 +315,11 @@ void PiecesTable::resizeEvent(QResizeEvent *e)
{
+ /*
+ * null if we faked it after the config dialog ran to
+ * regenerate everything
+ */
+ if ( e )
QTableView::resizeEvent(e);
- setCellWidth(contentsRect().width()/ numRows());
- setCellHeight(contentsRect().height() / numCols());
+ setCellWidth(contentsRect().width()/ numCols());
+ setCellHeight(contentsRect().height() / numRows());
@@ -302,3 +336,3 @@ void PiecesTable::initColors()
for (int c = 0; c < numCols(); c++)
- _colors[c + r *numCols()] = QColor(255 - 70 * c,255 - 70 * r, 150);
+ _colors[c + r *numCols()] = QColor( 255 - (70 * c)%255 ,255 - (70 * r)%255, 150);
}
@@ -307,4 +341,5 @@ void PiecesTable::initMap()
{
- _map.resize(16);
- for ( int i = 0; i < 16; i++)
+ int items = numCols()*numRows();
+ _map.resize( items );
+ for ( int i = 0; i < items; i++)
_map[i] = i;
@@ -319,3 +354,5 @@ void PiecesTable::randomizeMap()
// find the free position
- int pos = _map.find(15);
+ int cols = numCols();
+ int rows = numRows();
+ int pos = _map.find( cols*rows -1 );
@@ -324,12 +361,12 @@ void PiecesTable::randomizeMap()
- int frow = pos / numCols();
- int fcol = pos - frow * numCols();
+ int frow = pos / cols;
+ int fcol = pos - frow * cols;
// find click position
- int row = rand()%4;
- int col = rand()%4;
+ int row = rand()%rows;
+ int col = rand()%cols;
// sanity check
- if ( row < 0 || row >= numRows() ) continue;
- if ( col < 0 || col >= numCols() ) continue;
+ if ( row < 0 || row >= rows ) continue;
+ if ( col < 0 || col >= cols ) continue;
if ( row != frow && col != fcol ) continue;
@@ -343,3 +380,3 @@ void PiecesTable::randomizeMap()
for(int c = fcol; c > col; c--) {
- _map[c + row * numCols()] = _map[ c-1 + row *numCols()];
+ _map[c + row * cols] = _map[ c-1 + row *cols];
}
@@ -348,3 +385,3 @@ void PiecesTable::randomizeMap()
for(int c = fcol; c < col; c++) {
- _map[c + row * numCols()] = _map[ c+1 + row *numCols()];
+ _map[c + row * cols] = _map[ c+1 + row *cols];
}
@@ -357,3 +394,3 @@ void PiecesTable::randomizeMap()
for(int r = frow; r > row; r--) {
- _map[col + r * numCols()] = _map[ col + (r-1) *numCols()];
+ _map[col + r * cols] = _map[ col + (r-1) *cols];
}
@@ -362,3 +399,3 @@ void PiecesTable::randomizeMap()
for(int r = frow; r < row; r++) {
- _map[col + r * numCols()] = _map[ col + (r+1) *numCols()];
+ _map[col + r * cols] = _map[ col + (r+1) *cols];
}
@@ -367,3 +404,3 @@ void PiecesTable::randomizeMap()
// move free cell to click position
- _map[pos=(col + row * numCols())] = 15;
+ _map[pos=(col + row * cols)] = rows*cols-1;
}
@@ -376,4 +413,5 @@ void PiecesTable::checkwin()
+ int items=numCols()*numRows();
int i;
- for (i = 0; i < 16; i++)
+ for (i = 0; i < items; i++)
if(i != _map[i])
@@ -381,3 +419,3 @@ void PiecesTable::checkwin()
- if (i == 16) {
+ if (i == items) {
QMessageBox::information(this, tr("Fifteen Pieces"),
@@ -429,9 +467,12 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
// GAME LOGIC
+ int cols = numCols();
+ int rows = numRows();
+ int item = cols*rows -1;
// find the free position
- int pos = _map.find(15);
+ int pos = _map.find(item);
if(pos < 0) return;
- int frow = pos / numCols();
- int fcol = pos - frow * numCols();
+ int frow = pos / cols;
+ int fcol = pos - frow * cols;
@@ -442,4 +483,4 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
// sanity check
- if (row < 0 || row >= numRows()) return;
- if (col < 0 || col >= numCols()) return;
+ if (row < 0 || row >= rows) return;
+ if (col < 0 || col >= cols) return;
if ( row != frow && col != fcol ) return;
@@ -454,3 +495,3 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
for(int c = fcol; c > col; c--) {
- _map[c + row * numCols()] = _map[ c-1 + row *numCols()];
+ _map[c + row * cols] = _map[ c-1 + row *cols];
updateCell(row, c, false);
@@ -460,3 +501,3 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
for(int c = fcol; c < col; c++) {
- _map[c + row * numCols()] = _map[ c+1 + row *numCols()];
+ _map[c + row * cols] = _map[ c+1 + row *cols];
updateCell(row, c, false);
@@ -470,3 +511,3 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
for(int r = frow; r > row; r--) {
- _map[col + r * numCols()] = _map[ col + (r-1) *numCols()];
+ _map[col + r * cols] = _map[ col + (r-1) *cols];
updateCell(r, col, false);
@@ -476,3 +517,3 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
for(int r = frow; r < row; r++) {
- _map[col + r * numCols()] = _map[ col + (r+1) *numCols()];
+ _map[col + r * cols] = _map[ col + (r+1) *cols];
updateCell(r, col, false);
@@ -482,3 +523,3 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
// move free cell to click position
- _map[col + row * numCols()] = 15;
+ _map[col + row * cols] = item;
updateCell(row, col, false);
@@ -496,4 +537,22 @@ void PiecesTable::slotConfigure() {
_dialog->setImageSrc( _image );
- if ( QPEApplication::execDialog(_dialog) == QDialog::Accepted )
- slotCustomImage( _dialog->imageSrc(), true );
+ _dialog->setGameboard( numRows(), numCols() );
+
+ if ( QPEApplication::execDialog(_dialog) == QDialog::Accepted ) {
+ /*
+ * update the board grid and reinit the game if changed
+ * First set new columns so the update will regenerate the
+ * tiles with slotCustomImage
+ */
+ _image = _dialog->imageSrc();
+ if (numRows() != _dialog->rows() ||
+ numCols() != _dialog->columns() ) {
+ setNumCols(_dialog->columns());
+ setNumRows(_dialog->rows());
+ slotReset();
+ }
+ resizeEvent( 0l );
+
+
+ update();
+ }
}
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
void slotConfigure();
- void slotCustomImage(const QString &str, bool upd = false);
+ void slotCustomImage(const QString &str);
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
: FifteenConfigDialogBase( parent, name, modal )
-{
- grpGameGrid->hide();
-}
+{}