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
@@ -107,5 +107,4 @@ PiecesTable::PiecesTable(QWidget* parent, const char* name )
// init arrays
- initMap();
readConfig();
initColors();
@@ -125,9 +124,15 @@ void PiecesTable::writeConfig()
cfg.setGroup("Game");
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, '-');
cfg.writeEntry("Randomized", _randomized );
cfg.writeEntry("Image", _image );
+ cfg.writeEntry("Rows", numRows() );
+ cfg.writeEntry("Cols", numCols() );
}
@@ -139,9 +144,23 @@ void PiecesTable::readConfig()
_randomized = cfg.readBoolEntry( "Randomized", FALSE );
_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 ) {
_map[i] = (*it).toInt();
i++;
- if ( i > 15 ) break;
+ if ( i > items ) break;
}
@@ -153,5 +172,5 @@ void PiecesTable::clear() {
for (uint i = 0; i < _pixmap.count(); ++i )
delete _pixmap[i];
- _pixmap.resize( 16 );
+ _pixmap.resize( numRows()*numCols() );
}
@@ -161,5 +180,5 @@ void PiecesTable::clear() {
* last we put the number on it
*/
-void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
+void PiecesTable::slotCustomImage( const QString& _str ) {
QString str = _str;
@@ -167,14 +186,16 @@ void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
/* couldn't load image fall back to plain tiles*/
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();
@@ -191,7 +212,8 @@ void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
int y_offset = cellH - int(cellH * bw);
- /* border polygon */
+ /* border polygon calculation*/
initPolygon(cellW, cellH, x_offset, y_offset );
+ /* avoid crashes with isNull() pixmap later */
if ( cellW == 0 || cellH == 0 ) {
_pixmap.resize( 0 );
@@ -199,4 +221,5 @@ void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
}
+ /* make it bold and bigger */
QFont f = font();
f.setPixelSize(18);
@@ -236,7 +259,4 @@ void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
}
_image = str;
-
- if ( upd )
- update();
}
@@ -267,8 +287,16 @@ void PiecesTable::paintCell(QPainter *p, int row, int col)
int h = cellHeight();
+ 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;
// draw cell background
- if(number == 16) {
+ if(number == numCols()*numRows() ) {
p->setBrush(colorGroup().background());
p->setPen(NoPen);
@@ -277,4 +305,5 @@ void PiecesTable::paintCell(QPainter *p, int row, int col)
}
+ /* no tiles then contentRect() is not visible or too small anyway */
if( _pixmap.count() == 0 )
return;
@@ -285,8 +314,13 @@ void PiecesTable::paintCell(QPainter *p, int row, int col)
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());
@@ -301,11 +335,12 @@ void PiecesTable::initColors()
for (int r = 0; r < numRows(); r++)
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);
}
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;
@@ -318,19 +353,21 @@ void PiecesTable::randomizeMap()
_randomized = true;
// find the free position
- int pos = _map.find(15);
+ int cols = numCols();
+ int rows = numRows();
+ int pos = _map.find( cols*rows -1 );
int move = 0;
while ( move < 333 ) {
- 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;
@@ -342,10 +379,10 @@ void PiecesTable::randomizeMap()
if (col < fcol) {
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];
}
}
else if (col > fcol) {
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];
}
}
@@ -356,15 +393,15 @@ void PiecesTable::randomizeMap()
if (row < frow) {
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];
}
}
else if (row > frow) {
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];
}
}
}
// move free cell to click position
- _map[pos=(col + row * numCols())] = 15;
+ _map[pos=(col + row * cols)] = rows*cols-1;
}
repaint();
@@ -375,10 +412,11 @@ void PiecesTable::checkwin()
if(!_randomized) return;
+ int items=numCols()*numRows();
int i;
- for (i = 0; i < 16; i++)
+ for (i = 0; i < items; i++)
if(i != _map[i])
break;
- if (i == 16) {
+ if (i == items) {
QMessageBox::information(this, tr("Fifteen Pieces"),
tr("Congratulations!\nYou win the game!"));
@@ -428,11 +466,14 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
else {
// 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;
// find click position
@@ -441,6 +482,6 @@ 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;
@@ -453,5 +494,5 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
if (col < fcol) {
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);
}
@@ -459,5 +500,5 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
else if (col > fcol) {
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);
}
@@ -469,5 +510,5 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
if (row < frow) {
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);
}
@@ -475,5 +516,5 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
else if (row > frow) {
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);
}
@@ -481,5 +522,5 @@ 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);
@@ -495,5 +536,23 @@ 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
@@ -42,5 +42,5 @@ class PiecesTable : public QTableView
protected slots:
void slotConfigure();
- void slotCustomImage(const QString &str, bool upd = false);
+ void slotCustomImage(const QString &str);
void slotRandomize();
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;
FifteenConfigDialog::FifteenConfigDialog( QWidget* parent, const char* name, bool modal )
: FifteenConfigDialogBase( parent, name, modal )
-{
- grpGameGrid->hide();
-}
+{}
FifteenConfigDialog::~FifteenConfigDialog()