summaryrefslogtreecommitdiff
path: root/noncore/games/minesweep/minefield.cpp
Unidiff
Diffstat (limited to 'noncore/games/minesweep/minefield.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/minesweep/minefield.cpp144
1 files changed, 85 insertions, 59 deletions
diff --git a/noncore/games/minesweep/minefield.cpp b/noncore/games/minesweep/minefield.cpp
index 04cfb97..1790110 100644
--- a/noncore/games/minesweep/minefield.cpp
+++ b/noncore/games/minesweep/minefield.cpp
@@ -1,34 +1,35 @@
1/********************************************************************** 1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved. 2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3** 3**
4** This file is part of 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#include "minefield.h" 20#include "minefield.h"
21 21
22#include <qpe/config.h> 22#include <qtopia/config.h>
23#include <qtopia/qpeapplication.h>
23 24
24#include <qpainter.h> 25#include <qpainter.h>
25#include <qdrawutil.h> 26#include <qdrawutil.h>
26#include <qpixmap.h> 27#include <qpixmap.h>
27#include <qimage.h> 28#include <qimage.h>
28#include <qtimer.h> 29#include <qtimer.h>
29 30
30#include <stdlib.h> 31#include <stdlib.h>
31 32
32static const char *pix_flag[]={ 33static const char *pix_flag[]={
33"13 13 3 1", 34"13 13 3 1",
34"# c #000000", 35"# c #000000",
@@ -60,25 +61,25 @@ static const char *pix_mine[]={
60"..##aa#####..", 61"..##aa#####..",
61"..##aa#####..", 62"..##aa#####..",
62"#############", 63"#############",
63"..#########..", 64"..#########..",
64"..#########..", 65"..#########..",
65"...#######...", 66"...#######...",
66"..#.#####.#..", 67"..#.#####.#..",
67"......#......", 68"......#......",
68"......#......"}; 69"......#......"};
69 70
70 71
71static const int maxGrid = 28; 72static const int maxGrid = 28;
72static const int minGrid = 9; 73static const int minGrid = 12;
73 74
74 75
75 76
76class Mine : public Qt 77class Mine : public Qt
77{ 78{
78public: 79public:
79 enum MineState { 80 enum MineState {
80 Hidden = 0, 81 Hidden = 0,
81 Empty, 82 Empty,
82 Mined, 83 Mined,
83 Flagged, 84 Flagged,
84#ifdef MARK_UNSURE 85#ifdef MARK_UNSURE
@@ -175,46 +176,54 @@ void Mine::paletteChange()
175 delete unknownField; 176 delete unknownField;
176 unknownField = 0; 177 unknownField = 0;
177 delete mine_pix; 178 delete mine_pix;
178 mine_pix = 0; 179 mine_pix = 0;
179 delete flag_pix; 180 delete flag_pix;
180 flag_pix = 0; 181 flag_pix = 0;
181} 182}
182 183
183void Mine::paint( QPainter* p, const QColorGroup &cg, const QRect& cr ) 184void Mine::paint( QPainter* p, const QColorGroup &cg, const QRect& cr )
184{ 185{
185 int x = cr.x(); 186 int x = cr.x();
186 int y = cr.y(); 187 int y = cr.y();
187 if ( !knownField ) { 188 if ( !knownField || knownField->width() != cr.width() ||
189 knownField->height() != cr.height() ) {
190 delete knownField;
188 knownField = new QPixmap( cr.width(), cr.height() ); 191 knownField = new QPixmap( cr.width(), cr.height() );
189 QPainter pp( knownField ); 192 QPainter pp( knownField );
190 QBrush br( cg.button().dark(115) ); 193 QBrush br( cg.button().dark(115) );
191 qDrawWinButton( &pp, cr, cg, TRUE, &br ); 194 qDrawWinButton( &pp, QRect( 0, 0, cr.width(), cr.height() ), cg, TRUE, &br );
192 } 195 }
193 196
194 const int pmmarg=cr.width()/5; 197 const int pmmarg=cr.width()/5;
195 198
196 if ( !unknownField ) { 199 if ( !unknownField || unknownField->width() != cr.width() ||
200 unknownField->height() != cr.height() ) {
201 delete unknownField;
197 unknownField = new QPixmap( cr.width(), cr.height() ); 202 unknownField = new QPixmap( cr.width(), cr.height() );
198 QPainter pp( unknownField ); 203 QPainter pp( unknownField );
199 QBrush br( cg.button() ); 204 QBrush br( cg.button() );
200 qDrawWinButton( &pp, cr, cg, FALSE, &br ); 205 qDrawWinButton( &pp, QRect( 0, 0, cr.width(), cr.height() ), cg, FALSE, &br );
201 } 206 }
202 207
203 if ( !flag_pix ) { 208 if ( !flag_pix || flag_pix->width() != cr.width()-pmmarg*2 ||
209 flag_pix->height() != cr.height()-pmmarg*2 ) {
210 delete flag_pix;
204 flag_pix = new QPixmap( cr.width()-pmmarg*2, cr.height()-pmmarg*2 ); 211 flag_pix = new QPixmap( cr.width()-pmmarg*2, cr.height()-pmmarg*2 );
205 flag_pix->convertFromImage( QImage(pix_flag).smoothScale(cr.width()-pmmarg*2, cr.height()-pmmarg*2) ); 212 flag_pix->convertFromImage( QImage(pix_flag).smoothScale(cr.width()-pmmarg*2, cr.height()-pmmarg*2) );
206 } 213 }
207 214
208 if ( !mine_pix ) { 215 if ( !mine_pix || mine_pix->width() != cr.width()-pmmarg*2 ||
216 mine_pix->height() != cr.height()-pmmarg*2 ) {
217 delete mine_pix;
209 mine_pix = new QPixmap( cr.width()-pmmarg*2, cr.height()-pmmarg*2 ); 218 mine_pix = new QPixmap( cr.width()-pmmarg*2, cr.height()-pmmarg*2 );
210 mine_pix->convertFromImage( QImage(pix_mine).smoothScale(cr.width()-pmmarg*2, cr.height()-pmmarg*2) ); 219 mine_pix->convertFromImage( QImage(pix_mine).smoothScale(cr.width()-pmmarg*2, cr.height()-pmmarg*2) );
211 } 220 }
212 221
213 p->save(); 222 p->save();
214 223
215 switch(st) { 224 switch(st) {
216 case Hidden: 225 case Hidden:
217 p->drawPixmap( x, y, *unknownField ); 226 p->drawPixmap( x, y, *unknownField );
218 break; 227 break;
219 case Empty: 228 case Empty:
220 p->drawPixmap( x, y, *knownField ); 229 p->drawPixmap( x, y, *knownField );
@@ -274,175 +283,179 @@ void Mine::paint( QPainter* p, const QColorGroup &cg, const QRect& cr )
274 } 283 }
275 284
276 p->restore(); 285 p->restore();
277} 286}
278 287
279/* 288/*
280 MineField implementation 289 MineField implementation
281*/ 290*/
282 291
283MineField::MineField( QWidget* parent, const char* name ) 292MineField::MineField( QWidget* parent, const char* name )
284: QScrollView( parent, name ) 293: QScrollView( parent, name )
285{ 294{
295 viewport()->setBackgroundMode( NoBackground );
286 setState( GameOver ); 296 setState( GameOver );
287 297
288 setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum ) ); 298 setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum ) );
289 299
290 setFocusPolicy( QWidget::NoFocus ); 300 setFocusPolicy( QWidget::NoFocus );
291 301
292 holdTimer = new QTimer( this ); 302 holdTimer = new QTimer( this );
293 connect( holdTimer, SIGNAL( timeout() ), this, SLOT( held() ) ); 303 connect( holdTimer, SIGNAL( timeout() ), this, SLOT( held() ) );
294 304
295 flagAction = NoAction; 305 flagAction = NoAction;
296 ignoreClick = FALSE; 306 ignoreClick = FALSE;
297 currRow = currCol = -1; 307 currRow = currCol = -1;
298 minecount=0; 308 minecount=0;
299 mineguess=0; 309 mineguess=0;
300 nonminecount=0; 310 nonminecount=0;
301 cellSize = -1; 311 cellSize = -1;
302 mines = 0; 312
313 numRows = numCols = 0;
314 mines = NULL;
303} 315}
304 316
305MineField::~MineField() 317MineField::~MineField()
306{ 318{
307 int i; 319 for ( int i = 0; i < numCols*numRows; i++ )
308 if ( mines ) 320 delete mines[i];
309 { 321 delete[] mines;
310 for ( i = 0; i < numCols*numRows; i++ )
311 {
312 delete mines[i];
313 }
314 delete[] mines;
315 }
316} 322}
317 323
318void MineField::setState( State st ) 324void MineField::setState( State st )
319{ 325{
320 stat = st; 326 stat = st;
321} 327}
322 328
323void MineField::setup( int level ) 329void MineField::setup( int level )
324{ 330{
325 lev = level; 331 lev = level;
326 setState( Waiting ); 332 setState( Waiting );
327 //viewport()->setUpdatesEnabled( FALSE ); 333 //viewport()->setUpdatesEnabled( FALSE );
328 334
329 int i; 335 int i;
330 if ( mines ) 336 for ( i = 0; i < numCols*numRows; i++ )
331 { 337 delete mines[i];
332 for ( i = 0; i < numCols*numRows; i++ ) 338 delete[] mines;
333 {
334 delete mines[i];
335 }
336 delete[] mines;
337 }
338 339
339 switch( lev ) { 340 switch( lev ) {
340 case 1: 341 case 1:
341 numRows = 9 ; 342 numRows = 9 ;
342 numCols = 9 ; 343 numCols = 9 ;
343 minecount = 12; 344 minecount = 12;
344 break; 345 break;
345 case 2: 346 case 2:
346 numRows = 16; 347 numRows = 13;
347 numCols = 16; 348 numCols = 13;
348 minecount = 45; 349 minecount = 33;
349 break; 350 break;
350 case 3: 351 case 3:
351 numCols = 18; 352 numCols = 18;
352 numRows = 18; 353 numRows = 18;
353 minecount = 66 ; 354 minecount = 66 ;
354 break; 355 break;
355 } 356 }
356 mines = new Mine*[numRows*numCols]; 357 mines = new Mine* [numRows*numCols];
357 for ( i = 0; i < numCols*numRows; i++ ) 358 for ( i = 0; i < numCols*numRows; i++ )
358 mines[i] = new Mine( this ); 359 mines[i] = new Mine( this );
359 360
360 361
361 nonminecount = numRows*numCols - minecount; 362 nonminecount = numRows*numCols - minecount;
362 mineguess = minecount; 363 mineguess = minecount;
363 emit mineCount( mineguess ); 364 emit mineCount( mineguess );
364 Mine::paletteChange(); 365 Mine::paletteChange();
365 366
366 if ( availableRect.isValid() ) 367 if ( availableRect.isValid() )
367 setCellSize(findCellSize()); 368 setCellSize(findCellSize());
368 // viewport()->setUpdatesEnabled( TRUE ); 369 // viewport()->setUpdatesEnabled( TRUE );
369 //viewport()->repaint( TRUE ); 370 //viewport()->repaint( TRUE );
370 updateContents( 0, 0, numCols*cellSize, numRows*cellSize ); 371 updateContents( 0, 0, numCols*cellSize, numRows*cellSize );
371 updateGeometry(); 372 updateGeometry();
372} 373}
373 374
374void MineField::drawContents( QPainter * p, int clipx, int clipy, int clipw, int cliph ) 375void MineField::drawContents( QPainter * p, int clipx, int clipy, int clipw, int cliph )
375{ 376{
376 int c1 = clipx / cellSize; 377 int c1 = clipx / cellSize;
377 int c2 = ( clipx + clipw - 1 ) / cellSize; 378 int c2 = ( clipx + clipw - 1 ) / cellSize;
378 int r1 = clipy / cellSize; 379 int r1 = clipy / cellSize;
379 int r2 = ( clipy + cliph - 1 ) / cellSize; 380 int r2 = ( clipy + cliph - 1 ) / cellSize;
380 381
381 for ( int c = c1; c <= c2 ; c++ ) { 382 for ( int c = c1; c <= c2 ; c++ ) {
382 for ( int r = r1; r <= r2 ; r++ ) { 383 for ( int r = r1; r <= r2 ; r++ ) {
383 int x = c * cellSize; 384 int x = c * cellSize;
384 int y = r * cellSize; 385 int y = r * cellSize;
385 Mine *m = mine( r, c ); 386 Mine *m = mine( r, c );
386 if ( m ) 387 if ( m )
387 m->paint( p, colorGroup(), QRect(x, y, cellSize, cellSize ) ); 388 m->paint( p, colorGroup(), QRect(x, y, cellSize, cellSize ) );
388 } 389 }
389 } 390 }
390} 391}
391 392
392 393
393// Chicken and egg problem: We need to know how big the parent is 394// Chicken and egg problem: We need to know how big the parent is
394// before we can decide how big to make the table. 395// before we can decide how big to make the table.
395 396
396void MineField::setAvailableRect( const QRect &r ) 397void MineField::setAvailableRect( const QRect &r )
397{ 398{
398 availableRect = r; 399 availableRect = r;
399 int newCellSize = findCellSize(); 400 int newCellSize = findCellSize();
400 if ( newCellSize != cellSize ) { 401
402
403 if ( newCellSize == cellSize ) {
404 setCellSize( cellSize );
405 } else {
401 viewport()->setUpdatesEnabled( FALSE ); 406 viewport()->setUpdatesEnabled( FALSE );
402 setCellSize( newCellSize ); 407 setCellSize( newCellSize );
403 viewport()->setUpdatesEnabled( TRUE ); 408 viewport()->setUpdatesEnabled( TRUE );
404 viewport()->repaint( TRUE ); 409 viewport()->repaint( TRUE );
405 } 410 }
406} 411}
407 412
408int MineField::findCellSize() 413int MineField::findCellSize()
409{ 414{
410 int w = availableRect.width() - 1; 415 int w = availableRect.width() - 2;
411 int h = availableRect.height() - 1; 416 int h = availableRect.height() - 2;
412 int cellsize; 417 int cellsize;
413 418
414 cellsize = QMIN( w/numCols, h/numRows ); 419 cellsize = QMIN( w/numCols, h/numRows );
415 cellsize = QMIN( QMAX( cellsize, minGrid ), maxGrid ); 420 cellsize = QMIN( QMAX( cellsize, minGrid ), maxGrid );
416 return cellsize; 421 return cellsize;
417} 422}
418 423
419 424
420void MineField::setCellSize( int cellsize ) 425void MineField::setCellSize( int cellsize )
421{ 426{
422 cellSize = cellsize; 427 int b = 2;
423 428
424 int w = availableRect.width();
425 int h = availableRect.height();
426
427 int w2 = cellsize*numCols; 429 int w2 = cellsize*numCols;
428 int h2 = cellsize*numRows; 430 int h2 = cellsize*numRows;
429 431
430 resizeContents( w2, h2 ); 432 int w = QMIN( availableRect.width(), w2+b );
431 433 int h = QMIN( availableRect.height(), h2+b );
432 int b = 5; 434
433 435 //
434 setGeometry( availableRect.x() + (w-w2)/2, availableRect.y() + (h-h2)/2, 436 // Don't rely on the change in cellsize to force a resize,
435 w2+b, h2+b ); 437 // as it's possible to have the same size cells when going
436 // QMIN(w,w2+b), QMIN(h,h2+b) ); 438 // from a large play area to a small one.
439 //
440 resizeContents(w2, h2);
441
442 if ( availableRect.height() < h2 &&
443 availableRect.width() - w > style().scrollBarExtent().width() ) {
444 w += style().scrollBarExtent().width();
445 }
446
447 setGeometry( availableRect.x() + (availableRect.width()-w)/2,
448 availableRect.y() + (availableRect.height()-h)/2, w, h );
449 cellSize = cellsize;
437} 450}
438 451
439 452
440void MineField::placeMines() 453void MineField::placeMines()
441{ 454{
442 int mines = minecount; 455 int mines = minecount;
443 while ( mines ) { 456 while ( mines ) {
444 int col = int((double(rand()) / double(RAND_MAX)) * numCols); 457 int col = int((double(rand()) / double(RAND_MAX)) * numCols);
445 int row = int((double(rand()) / double(RAND_MAX)) * numRows); 458 int row = int((double(rand()) / double(RAND_MAX)) * numRows);
446 459
447 Mine* m = mine( row, col ); 460 Mine* m = mine( row, col );
448 461
@@ -604,28 +617,32 @@ void MineField::updateMine( int row, int col )
604 if ( m->state() == Mine::Exploded ) { 617 if ( m->state() == Mine::Exploded ) {
605 emit gameOver( FALSE ); 618 emit gameOver( FALSE );
606 setState( GameOver ); 619 setState( GameOver );
607 return; 620 return;
608 } else if ( m->state() == Mine::Empty ) { 621 } else if ( m->state() == Mine::Empty ) {
609 setHint( row, col ); 622 setHint( row, col );
610 if ( !wasEmpty ) 623 if ( !wasEmpty )
611 nonminecount--; 624 nonminecount--;
612 } 625 }
613 626
614 if ( flagAction != NoAction ) { 627 if ( flagAction != NoAction ) {
615 if ( m->state() == Mine::Flagged ) { 628 if ( m->state() == Mine::Flagged ) {
616 --mineguess; 629 if (mineguess > 0) {
617 emit mineCount( mineguess ); 630 --mineguess;
618 if ( m->isMined() ) 631 emit mineCount( mineguess );
619 --minecount; 632 if ( m->isMined() )
633 --minecount;
634 } else {
635 m->setState(Mine::Hidden);
636 }
620 } else if ( wasFlagged ) { 637 } else if ( wasFlagged ) {
621 ++mineguess; 638 ++mineguess;
622 emit mineCount( mineguess ); 639 emit mineCount( mineguess );
623 if ( m->isMined() ) 640 if ( m->isMined() )
624 ++minecount; 641 ++minecount;
625 } 642 }
626 } 643 }
627 644
628 updateCell( row, col ); 645 updateCell( row, col );
629 646
630 if ( !minecount && !mineguess || !nonminecount ) { 647 if ( !minecount && !mineguess || !nonminecount ) {
631 emit gameOver( TRUE ); 648 emit gameOver( TRUE );
@@ -672,29 +689,30 @@ void MineField::writeConfig(Config& cfg) const
672 cfg.writeEntry("Grid",grid); 689 cfg.writeEntry("Grid",grid);
673} 690}
674 691
675void MineField::readConfig(Config& cfg) 692void MineField::readConfig(Config& cfg)
676{ 693{
677 cfg.setGroup("Field"); 694 cfg.setGroup("Field");
678 lev = cfg.readNumEntry("Level",1); 695 lev = cfg.readNumEntry("Level",1);
679 setup(lev); 696 setup(lev);
680 flagAction = NoAction; 697 flagAction = NoAction;
681 ignoreClick = FALSE; 698 ignoreClick = FALSE;
682 currRow = currCol = 0; 699 currRow = currCol = 0;
683 QString grid = cfg.readEntry("Grid"); 700 QString grid = cfg.readEntry("Grid");
701 int x;
684 if ( !grid.isEmpty() ) { 702 if ( !grid.isEmpty() ) {
685 int i=0; 703 int i=0;
686 minecount=0; 704 minecount=0;
687 mineguess=0; 705 mineguess=0;
688 for ( int x = 0; x < numCols; x++ ) { 706 for ( x = 0; x < numCols; x++ ) {
689 for ( int y = 0; y < numRows; y++ ) { 707 for ( int y = 0; y < numRows; y++ ) {
690 char code='A'+(x*17+y*101)%21; // Reduce the urge to cheat 708 char code='A'+(x*17+y*101)%21; // Reduce the urge to cheat
691 int st = (char)(QChar)grid[i++]-code; 709 int st = (char)(QChar)grid[i++]-code;
692 Mine* m = mine( y, x ); 710 Mine* m = mine( y, x );
693 if ( st >= 5 ) { 711 if ( st >= 5 ) {
694 st-=5; 712 st-=5;
695 m->setMined(TRUE); 713 m->setMined(TRUE);
696 minecount++; 714 minecount++;
697 mineguess++; 715 mineguess++;
698 } 716 }
699 m->setState((Mine::MineState)st); 717 m->setState((Mine::MineState)st);
700 switch ( m->state() ) { 718 switch ( m->state() ) {
@@ -702,24 +720,32 @@ void MineField::readConfig(Config& cfg)
702 if (m->isMined()) 720 if (m->isMined())
703 minecount--; 721 minecount--;
704 mineguess--; 722 mineguess--;
705 break; 723 break;
706 case Mine::Empty: 724 case Mine::Empty:
707 --nonminecount; 725 --nonminecount;
708 break; 726 break;
709 default: 727 default:
710 break; 728 break;
711 } 729 }
712 } 730 }
713 } 731 }
714 for ( int x = 0; x < numCols; x++ ) { 732 for ( x = 0; x < numCols; x++ ) {
715 for ( int y = 0; y < numRows; y++ ) { 733 for ( int y = 0; y < numRows; y++ ) {
716 Mine* m = mine( y, x ); 734 Mine* m = mine( y, x );
717 if ( m->state() == Mine::Empty ) 735 if ( m->state() == Mine::Empty )
718 m->setHint(getHint(y,x)); 736 m->setHint(getHint(y,x));
719 } 737 }
720 } 738 }
721 } 739 }
722 setState( Playing ); 740 setState( Playing );
723 emit mineCount( mineguess ); 741 emit mineCount( mineguess );
724} 742}
725 743
744QSize MineField::sizeHint() const
745{
746 if ( qApp->desktop()->width() >= 240 )
747 return QSize(200,200);
748 else
749 return QSize(160, 160);
750}
751