summaryrefslogtreecommitdiff
path: root/noncore/games/minesweep
Unidiff
Diffstat (limited to 'noncore/games/minesweep') (more/less context) (show whitespace changes)
-rw-r--r--noncore/games/minesweep/.cvsignore2
-rw-r--r--noncore/games/minesweep/Makefile.in134
-rw-r--r--noncore/games/minesweep/main.cpp34
-rw-r--r--noncore/games/minesweep/minefield.cpp623
-rw-r--r--noncore/games/minesweep/minefield.h87
-rw-r--r--noncore/games/minesweep/minesweep.cpp390
-rw-r--r--noncore/games/minesweep/minesweep.h67
-rw-r--r--noncore/games/minesweep/minesweep.pro14
-rw-r--r--noncore/games/minesweep/qpe-minesweep.control9
9 files changed, 1360 insertions, 0 deletions
diff --git a/noncore/games/minesweep/.cvsignore b/noncore/games/minesweep/.cvsignore
new file mode 100644
index 0000000..6fe2396
--- a/dev/null
+++ b/noncore/games/minesweep/.cvsignore
@@ -0,0 +1,2 @@
1moc_*
2Makefile
diff --git a/noncore/games/minesweep/Makefile.in b/noncore/games/minesweep/Makefile.in
new file mode 100644
index 0000000..9ed6234
--- a/dev/null
+++ b/noncore/games/minesweep/Makefile.in
@@ -0,0 +1,134 @@
1#############################################################################
2
3####### Compiler, tools and options
4
5 CXX =$(SYSCONF_CXX) $(QT_CXX_MT)
6 CXXFLAGS=$(SYSCONF_CXXFLAGS_QT) $(SYSCONF_CXXFLAGS)
7 CC =$(SYSCONF_CC) $(QT_C_MT)
8 CFLAGS =$(SYSCONF_CFLAGS)
9 INCPATH =-I$(QPEDIR)/include
10 LFLAGS =$(SYSCONF_LFLAGS_QT) $(SYSCONF_RPATH_QT) $(SYSCONF_LFLAGS) $(QT_LFLAGS_MT)
11 LIBS =$(SUBLIBS) -lqpe $(SYSCONF_LIBS_QT) $(SYSCONF_LIBS) $(SYSCONF_LIBS_QTAPP)
12 MOC =$(SYSCONF_MOC)
13 UIC =$(SYSCONF_UIC)
14
15####### Target
16
17DESTDIR = $(QPEDIR)/bin/
18VER_MAJ = 1
19VER_MIN = 0
20VER_PATCH = 0
21 TARGET= minesweep
22TARGET1 = lib$(TARGET).so.$(VER_MAJ)
23
24####### Files
25
26 HEADERS =minefield.h \
27 minesweep.h
28 SOURCES =main.cpp \
29 minefield.cpp \
30 minesweep.cpp
31 OBJECTS =main.o \
32 minefield.o \
33 minesweep.o
34INTERFACES =
35UICDECLS =
36UICIMPLS =
37 SRCMOC =moc_minefield.cpp \
38 moc_minesweep.cpp
39 OBJMOC =moc_minefield.o \
40 moc_minesweep.o
41
42
43####### Implicit rules
44
45.SUFFIXES: .cpp .cxx .cc .C .c
46
47.cpp.o:
48 $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
49
50.cxx.o:
51 $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
52
53.cc.o:
54 $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
55
56.C.o:
57 $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
58
59.c.o:
60 $(CC) -c $(CFLAGS) $(INCPATH) -o $@ $<
61
62####### Build rules
63
64
65all: $(DESTDIR)$(TARGET)
66
67$(DESTDIR)$(TARGET): $(UICDECLS) $(OBJECTS) $(OBJMOC) $(SUBLIBS)
68 $(SYSCONF_LINK) $(LFLAGS) -o $(DESTDIR)$(TARGET) $(OBJECTS) $(OBJMOC) $(LIBS)
69
70moc: $(SRCMOC)
71
72tmake:
73 tmake minesweep.pro
74
75clean:
76 -rm -f $(OBJECTS) $(OBJMOC) $(SRCMOC) $(UICIMPLS) $(UICDECLS)
77 -rm -f *~ core
78 -rm -f allmoc.cpp
79
80####### Extension Modules
81
82listpromodules:
83 @echo
84
85listallmodules:
86 @echo
87
88listaddonpromodules:
89 @echo
90
91listaddonentmodules:
92 @echo
93
94
95REQUIRES=
96
97####### Sub-libraries
98
99
100###### Combined headers
101
102
103
104####### Compile
105
106main.o: main.cpp \
107 minesweep.h \
108 $(QPEDIR)/include/qpe/qpeapplication.h
109
110minefield.o: minefield.cpp \
111 minefield.h \
112 $(QPEDIR)/include/qpe/config.h
113
114minesweep.o: minesweep.cpp \
115 minesweep.h \
116 minefield.h \
117 $(QPEDIR)/include/qpe/resource.h \
118 $(QPEDIR)/include/qpe/config.h \
119 $(QPEDIR)/include/qpe/qpetoolbar.h \
120 $(QPEDIR)/include/qpe/qpemenubar.h
121
122moc_minefield.o: moc_minefield.cpp \
123 minefield.h
124
125moc_minesweep.o: moc_minesweep.cpp \
126 minesweep.h
127
128moc_minefield.cpp: minefield.h
129 $(MOC) minefield.h -o moc_minefield.cpp
130
131moc_minesweep.cpp: minesweep.h
132 $(MOC) minesweep.h -o moc_minesweep.cpp
133
134
diff --git a/noncore/games/minesweep/main.cpp b/noncore/games/minesweep/main.cpp
new file mode 100644
index 0000000..83de9a3
--- a/dev/null
+++ b/noncore/games/minesweep/main.cpp
@@ -0,0 +1,34 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
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
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
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.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include "minesweep.h"
22
23#include <qpe/qpeapplication.h>
24
25int main( int argc, char** argv )
26{
27 QPEApplication a( argc, argv );
28
29 MineSweep ms;
30 QPEApplication::setInputMethodHint( &ms, QPEApplication::AlwaysOff );
31 a.showMainWidget( &ms );
32
33 return a.exec();
34}
diff --git a/noncore/games/minesweep/minefield.cpp b/noncore/games/minesweep/minefield.cpp
new file mode 100644
index 0000000..be2f9a3
--- a/dev/null
+++ b/noncore/games/minesweep/minefield.cpp
@@ -0,0 +1,623 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
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
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
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.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20#include "minefield.h"
21
22#include <qpe/config.h>
23
24#include <qpainter.h>
25#include <qdrawutil.h>
26#include <qpixmap.h>
27#include <qimage.h>
28#include <qtimer.h>
29
30#include <stdlib.h>
31
32static const char *pix_flag[]={
33"13 13 3 1",
34"# c #000000",
35"x c #ff0000",
36". c None",
37".............",
38".............",
39".....#xxxxxx.",
40".....#xxxxxx.",
41".....#xxxxxx.",
42".....#xxxxxx.",
43".....#.......",
44".....#.......",
45".....#.......",
46".....#.......",
47"...#####.....",
48"..#######....",
49"............."};
50
51static const char *pix_mine[]={
52"13 13 3 1",
53"# c #000000",
54". c None",
55"a c #ffffff",
56"......#......",
57"......#......",
58"..#.#####.#..",
59"...#######...",
60"..##aa#####..",
61"..##aa#####..",
62"#############",
63"..#########..",
64"..#########..",
65"...#######...",
66"..#.#####.#..",
67"......#......",
68"......#......"};
69
70class Mine : public QTableItem
71{
72public:
73 enum MineState {
74 Hidden = 0,
75 Empty,
76 Mined,
77 Flagged,
78#ifdef MARK_UNSURE
79 Unsure,
80#endif
81 Exploded,
82 Wrong
83 };
84
85 Mine( QTable* );
86 void paint( QPainter * p, const QColorGroup & cg, const QRect & cr, bool selected );
87 EditType editType() const { return Never; }
88 QSize sizeHint() const { return QSize( 12, 12 ); }
89
90 void activate( bool sure = TRUE );
91 void setHint( int );
92
93 void setState( MineState );
94 MineState state() const { return st; }
95
96 bool isMined() const { return mined; }
97 void setMined( bool m ) { mined = m; }
98
99 static void paletteChange();
100
101private:
102 bool mined;
103 int hint;
104
105 MineState st;
106
107 static QPixmap* knownField;
108 static QPixmap* unknownField;
109 static QPixmap* flag_pix;
110 static QPixmap* mine_pix;
111};
112
113QPixmap* Mine::knownField = 0;
114QPixmap* Mine::unknownField = 0;
115QPixmap* Mine::flag_pix = 0;
116QPixmap* Mine::mine_pix = 0;
117
118Mine::Mine( QTable *t )
119: QTableItem( t, Never, QString::null )
120{
121 mined = FALSE;
122 st = Hidden;
123 hint = 0;
124}
125
126void Mine::activate( bool sure )
127{
128 if ( !sure ) {
129 switch ( st ) {
130 case Hidden:
131 setState( Flagged );
132 break;
133 case Flagged:
134#ifdef MARK_UNSURE
135 setState( Unsure );
136 break;
137 case Unsure:
138#endif
139 setState( Hidden );
140 default:
141 break;
142 }
143 } else if ( st == Flagged ) {
144 return;
145 } else {
146 if ( mined ) {
147 setState( Exploded );
148 } else {
149 setState( Empty );
150 }
151 }
152}
153
154void Mine::setState( MineState s )
155{
156 st = s;
157}
158
159void Mine::setHint( int h )
160{
161 hint = h;
162}
163
164void Mine::paletteChange()
165{
166 delete knownField;
167 knownField = 0;
168 delete unknownField;
169 unknownField = 0;
170 delete mine_pix;
171 mine_pix = 0;
172 delete flag_pix;
173 flag_pix = 0;
174}
175
176void Mine::paint( QPainter* p, const QColorGroup &cg, const QRect& cr, bool )
177{
178 if ( !knownField ) {
179 knownField = new QPixmap( cr.width(), cr.height() );
180 QPainter pp( knownField );
181 QBrush br( cg.button().dark(115) );
182 qDrawWinButton( &pp, QRect(0,0,cr.width(), cr.height())/*cr*/, cg, TRUE, &br );
183 }
184
185 const int pmmarg=cr.width()/5;
186
187 if ( !unknownField ) {
188 unknownField = new QPixmap( cr.width(), cr.height() );
189 QPainter pp( unknownField );
190 QBrush br( cg.button() );
191 qDrawWinButton( &pp, QRect(0,0,cr.width(), cr.height())/*cr*/, cg, FALSE, &br );
192 }
193
194 if ( !flag_pix ) {
195 flag_pix = new QPixmap( cr.width()-pmmarg*2, cr.height()-pmmarg*2 );
196 flag_pix->convertFromImage( QImage(pix_flag).smoothScale(cr.width()-pmmarg*2, cr.height()-pmmarg*2) );
197 }
198
199 if ( !mine_pix ) {
200 mine_pix = new QPixmap( cr.width()-pmmarg*2, cr.height()-pmmarg*2 );
201 mine_pix->convertFromImage( QImage(pix_mine).smoothScale(cr.width()-pmmarg*2, cr.height()-pmmarg*2) );
202 }
203
204 p->save();
205
206 switch(st) {
207 case Hidden:
208 p->drawPixmap( 0, 0, *unknownField );
209 break;
210 case Empty:
211 p->drawPixmap( 0, 0, *knownField );
212 if ( hint > 0 ) {
213 switch( hint ) {
214 case 1:
215 p->setPen( blue );
216 break;
217 case 2:
218 p->setPen( green );
219 case 3:
220 p->setPen( red );
221 break;
222 default:
223 p->setPen( darkMagenta );
224 break;
225 }
226 p->drawText( QRect( 0, 0, cr.width(), cr.height() ), AlignHCenter | AlignVCenter, QString().setNum( hint ) );
227 }
228 break;
229 case Mined:
230 p->drawPixmap( 0, 0, *knownField );
231 p->drawPixmap( pmmarg, pmmarg, *mine_pix );
232 break;
233 case Exploded:
234 p->drawPixmap( 0, 0, *knownField );
235 p->drawPixmap( pmmarg, pmmarg, *mine_pix );
236 p->setPen( red );
237 p->drawText( QRect( 0, 0, cr.width(), cr.height() ), AlignHCenter | AlignVCenter, "X" );
238 break;
239 case Flagged:
240 p->drawPixmap( 0, 0, *unknownField );
241 p->drawPixmap( pmmarg, pmmarg, *flag_pix );
242 break;
243#ifdef MARK_UNSURE
244 case Unsure:
245 p->drawPixmap( 0, 0, *unknownField );
246 p->drawText( QRect( 0, 0, cr.width(), cr.height() ), AlignHCenter | AlignVCenter, "?" );
247 break;
248#endif
249 case Wrong:
250 p->drawPixmap( 0, 0, *unknownField );
251 p->drawPixmap( pmmarg, pmmarg, *flag_pix );
252 p->setPen( red );
253 p->drawText( QRect( 0, 0, cr.width(), cr.height() ), AlignHCenter | AlignVCenter, "X" );
254 break;
255 }
256
257 p->restore();
258}
259
260/*
261 MineField implementation
262*/
263
264MineField::MineField( QWidget* parent, const char* name )
265: QTable( parent, name )
266{
267 setState( GameOver );
268 setShowGrid( FALSE );
269 horizontalHeader()->hide();
270 verticalHeader()->hide();
271 setTopMargin( 0 );
272 setLeftMargin( 0 );
273
274 setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum ) );
275
276 setSelectionMode( QTable::NoSelection );
277 setFocusPolicy( QWidget::NoFocus );
278
279 setCurrentCell( -1, -1 );
280
281 connect( this, SIGNAL( pressed( int, int, int, const QPoint& ) ), this, SLOT( cellPressed( int, int ) ) );
282 connect( this, SIGNAL( clicked( int, int, int, const QPoint& ) ), this, SLOT( cellClicked( int, int ) ) );
283
284 holdTimer = new QTimer( this );
285 connect( holdTimer, SIGNAL( timeout() ), this, SLOT( held() ) );
286
287 flagAction = NoAction;
288 ignoreClick = FALSE;
289 currRow = currCol = 0;
290 minecount=0;
291 mineguess=0;
292 nonminecount=0;
293}
294
295MineField::~MineField()
296{
297}
298
299void MineField::setState( State st )
300{
301 stat = st;
302}
303
304
305void MineField::setup( int level )
306{
307 lev = level;
308 setState( Waiting );
309 viewport()->setUpdatesEnabled( FALSE );
310
311 int cellsize;
312
313 int x;
314 int y;
315 for ( x = 0; x < numCols(); x++ )
316 for ( y = 0; y < numRows(); y++ )
317 clearCell( y, x );
318
319 switch( lev ) {
320 case 1:
321 setNumRows( 9 );
322 setNumCols( 9 );
323 minecount = 12;
324 cellsize = 21;
325 break;
326 case 2:
327 setNumRows( 16 );
328 setNumCols( 16 );
329 minecount = 45;
330 cellsize = 14;
331 break;
332 case 3:
333 setNumRows( 18 );
334 setNumCols( 18 );
335 minecount = 66 ;
336 cellsize = 12;
337 break;
338 }
339 nonminecount = numRows()*numCols() - minecount;
340 mineguess = minecount;
341 emit mineCount( mineguess );
342 Mine::paletteChange();
343
344 for ( y = 0; y < numRows(); y++ )
345 setRowHeight( y, cellsize );
346 for ( x = 0; x < numCols(); x++ )
347 setColumnWidth( x, cellsize );
348 for ( x = 0; x < numCols(); x++ )
349 for ( y = 0; y < numRows(); y++ )
350 setItem( y, x, new Mine( this ) );
351
352 updateGeometry();
353 viewport()->setUpdatesEnabled( TRUE );
354 viewport()->repaint( TRUE );
355}
356
357
358void MineField::placeMines()
359{
360 int mines = minecount;
361 while ( mines ) {
362 int col = int((double(rand()) / double(RAND_MAX)) * numCols());
363 int row = int((double(rand()) / double(RAND_MAX)) * numRows());
364
365 Mine* mine = (Mine*)item( row, col );
366
367 if ( mine && !mine->isMined() && mine->state() == Mine::Hidden ) {
368 mine->setMined( TRUE );
369 mines--;
370 }
371 }
372}
373
374void MineField::paintFocus( QPainter*, const QRect& )
375{
376}
377
378void MineField::viewportMousePressEvent( QMouseEvent* e )
379{
380 QTable::viewportMousePressEvent( e );
381}
382
383void MineField::viewportMouseReleaseEvent( QMouseEvent* e )
384{
385 QTable::viewportMouseReleaseEvent( e );
386 if ( flagAction == FlagNext ) {
387 flagAction = NoAction;
388 }
389}
390
391void MineField::keyPressEvent( QKeyEvent* e )
392{
393#if defined(Q_WS_QWS) || defined(_WS_QWS_)
394 flagAction = ( e->key() == Key_Up ) ? FlagOn : NoAction;
395#else
396 flagAction = ( ( e->state() & ShiftButton ) == ShiftButton ) ? FlagOn : NoAction;
397#endif
398}
399
400void MineField::keyReleaseEvent( QKeyEvent* )
401{
402 flagAction = NoAction;
403}
404
405int MineField::getHint( int row, int col )
406{
407 int hint = 0;
408 for ( int c = col-1; c <= col+1; c++ )
409 for ( int r = row-1; r <= row+1; r++ ) {
410 Mine* mine = (Mine*)item( r, c );
411 if ( mine && mine->isMined() )
412 hint++;
413 }
414
415 return hint;
416}
417
418void MineField::setHint( Mine* mine )
419{
420 if ( !mine )
421 return;
422
423 int row = mine->row();
424 int col = mine->col();
425 int hint = getHint( row, col );
426
427 if ( !hint ) {
428 for ( int c = col-1; c <= col+1; c++ )
429 for ( int r = row-1; r <= row+1; r++ ) {
430 Mine* mine = (Mine*)item( r, c );
431 if ( mine && mine->state() == Mine::Hidden ) {
432 mine->activate( TRUE );
433 nonminecount--;
434 setHint( mine );
435 updateCell( r, c );
436 }
437 }
438 }
439
440 mine->setHint( hint );
441 updateCell( row, col );
442}
443
444/*
445 state == Waiting means no "hold"
446
447
448*/
449void MineField::cellPressed( int row, int col )
450{
451 if ( state() == GameOver )
452 return;
453 currRow = row;
454 currCol = col;
455 if ( state() == Playing )
456 holdTimer->start( 150, TRUE );
457}
458
459void MineField::held()
460{
461 flagAction = FlagNext;
462 updateMine( currRow, currCol );
463 ignoreClick = TRUE;
464}
465
466/*
467 Only place mines after first click, since it is pointless to
468 kill the player before the game has started.
469*/
470
471void MineField::cellClicked( int row, int col )
472{
473 if ( state() == GameOver )
474 return;
475 if ( state() == Waiting ) {
476 Mine* mine = (Mine*)item( row, col );
477 if ( !mine )
478 return;
479 mine->setState( Mine::Empty );
480 nonminecount--;
481 placeMines();
482 setState( Playing );
483 emit gameStarted();
484 updateMine( row, col );
485 } else { // state() == Playing
486 holdTimer->stop();
487 if ( ignoreClick )
488 ignoreClick = FALSE;
489 else
490 updateMine( row, col );
491 }
492}
493
494void MineField::updateMine( int row, int col )
495{
496 Mine* mine = (Mine*)item( row, col );
497 if ( !mine )
498 return;
499
500 bool wasFlagged = mine->state() == Mine::Flagged;
501 bool wasEmpty = mine->state() == Mine::Empty;
502
503 mine->activate( flagAction == NoAction );
504
505 if ( mine->state() == Mine::Exploded ) {
506 emit gameOver( FALSE );
507 setState( GameOver );
508 return;
509 } else if ( mine->state() == Mine::Empty ) {
510 setHint( mine );
511 if ( !wasEmpty )
512 nonminecount--;
513 }
514
515 if ( flagAction != NoAction ) {
516 if ( mine->state() == Mine::Flagged ) {
517 --mineguess;
518 emit mineCount( mineguess );
519 if ( mine->isMined() )
520 --minecount;
521 } else if ( wasFlagged ) {
522 ++mineguess;
523 emit mineCount( mineguess );
524 if ( mine->isMined() )
525 ++minecount;
526 }
527 }
528
529 updateCell( row, col );
530
531 if ( !minecount && !mineguess || !nonminecount ) {
532 emit gameOver( TRUE );
533 setState( GameOver );
534 }
535}
536
537void MineField::showMines()
538{
539 for ( int c = 0; c < numCols(); c++ )
540 for ( int r = 0; r < numRows(); r++ ) {
541 Mine* mine = (Mine*)item( r, c );
542 if ( !mine )
543 continue;
544 if ( mine->isMined() && mine->state() == Mine::Hidden )
545 mine->setState( Mine::Mined );
546 if ( !mine->isMined() && mine->state() == Mine::Flagged )
547 mine->setState( Mine::Wrong );
548
549 updateCell( r, c );
550 }
551}
552
553void MineField::paletteChange( const QPalette &o )
554{
555 Mine::paletteChange();
556 QTable::paletteChange( o );
557}
558
559void MineField::writeConfig(Config& cfg) const
560{
561 cfg.setGroup("Field");
562 cfg.writeEntry("Level",lev);
563 QString grid="";
564 if ( stat == Playing ) {
565 for ( int x = 0; x < numCols(); x++ )
566 for ( int y = 0; y < numRows(); y++ ) {
567 char code='A'+(x*17+y*101)%21; // Reduce the urge to cheat
568 Mine* mine = (Mine*)item( y, x );
569 int st = (int)mine->state(); if ( mine->isMined() ) st+=5;
570 grid += code + st;
571 }
572 }
573 cfg.writeEntry("Grid",grid);
574}
575
576void MineField::readConfig(Config& cfg)
577{
578 cfg.setGroup("Field");
579 lev = cfg.readNumEntry("Level",1);
580 setup(lev);
581 flagAction = NoAction;
582 ignoreClick = FALSE;
583 currRow = currCol = 0;
584 QString grid = cfg.readEntry("Grid");
585 if ( !grid.isEmpty() ) {
586 int i=0;
587 minecount=0;
588 mineguess=0;
589 for ( int x = 0; x < numCols(); x++ ) {
590 for ( int y = 0; y < numRows(); y++ ) {
591 char code='A'+(x*17+y*101)%21; // Reduce the urge to cheat
592 int st = (char)(QChar)grid[i++]-code;
593 Mine* mine = (Mine*)item( y, x );
594 if ( st >= 5 ) {
595 st-=5;
596 mine->setMined(TRUE);
597 minecount++;
598 mineguess++;
599 }
600 mine->setState((Mine::MineState)st);
601 switch ( mine->state() ) {
602 case Mine::Flagged:
603 if (mine->isMined())
604 minecount--;
605 mineguess--;
606 break;
607 case Mine::Empty:
608 --nonminecount;
609 }
610 }
611 }
612 for ( int x = 0; x < numCols(); x++ ) {
613 for ( int y = 0; y < numRows(); y++ ) {
614 Mine* mine = (Mine*)item( y, x );
615 if ( mine->state() == Mine::Empty )
616 mine->setHint(getHint(y,x));
617 }
618 }
619 }
620 setState( Playing );
621 emit mineCount( mineguess );
622}
623
diff --git a/noncore/games/minesweep/minefield.h b/noncore/games/minesweep/minefield.h
new file mode 100644
index 0000000..4ede435
--- a/dev/null
+++ b/noncore/games/minesweep/minefield.h
@@ -0,0 +1,87 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
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
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
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.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20#ifndef MINEFIELD_H
21#define MINEFIELD_H
22
23#include <qtable.h>
24
25class Mine;
26class Config;
27
28class MineField : public QTable
29{
30 Q_OBJECT
31public:
32 MineField( QWidget* parent = 0, const char* name = 0 );
33 ~MineField();
34
35 enum State { Waiting, Playing, GameOver };
36
37 State state() const { return stat; }
38
39 void readConfig(Config&);
40 void writeConfig(Config&) const;
41
42 int level() const { return lev; }
43
44public slots:
45 void setup( int level );
46
47 void showMines();
48
49signals:
50 void gameOver( bool won );
51 void gameStarted();
52 void mineCount( int );
53
54protected:
55 void paintFocus( QPainter*, const QRect& );
56 void viewportMousePressEvent( QMouseEvent* );
57 void viewportMouseReleaseEvent( QMouseEvent* );
58 void keyPressEvent( QKeyEvent* );
59 void keyReleaseEvent( QKeyEvent* );
60
61 int getHint( int row, int col );
62 void setHint( Mine* );
63 void updateMine( int row, int col );
64 void paletteChange( const QPalette & );
65
66protected slots:
67 void cellPressed( int row, int col );
68 void cellClicked( int row, int col );
69 void held();
70
71private:
72 State stat;
73 void MineField::setState( State st );
74 void MineField::placeMines();
75 enum FlagAction { NoAction, FlagOn, FlagNext };
76 FlagAction flagAction;
77 bool ignoreClick;
78 int currRow;
79 int currCol;
80 int minecount;
81 int mineguess;
82 int nonminecount;
83 int lev;
84 QTimer *holdTimer;
85};
86
87#endif // MINEFIELD_H
diff --git a/noncore/games/minesweep/minesweep.cpp b/noncore/games/minesweep/minesweep.cpp
new file mode 100644
index 0000000..6492462
--- a/dev/null
+++ b/noncore/games/minesweep/minesweep.cpp
@@ -0,0 +1,390 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
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
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
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.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include "minesweep.h"
22#include "minefield.h"
23
24#include <qpe/resource.h>
25#include <qpe/config.h>
26
27#include <qpe/qpetoolbar.h>
28#include <qpe/qpemenubar.h>
29#include <qpopupmenu.h>
30#include <qpushbutton.h>
31#include <qlcdnumber.h>
32#include <qmessagebox.h>
33#include <qtimer.h>
34#include <qpalette.h>
35#include <qapplication.h>
36#include <qlayout.h>
37#include <qlabel.h>
38
39#include <stdlib.h>
40#include <time.h>
41
42
43
44
45static const char *pix_new[]={
46"20 20 3 1",
47" c None",
48"# c #00FF00",
49". c #000000",
50" ",
51" ...... ",
52" ..######.. ",
53" .##########. ",
54" .############. ",
55" .##############. ",
56" .##############. ",
57" .################. ",
58" .################. ",
59" .################. ",
60" .################. ",
61" .################. ",
62" .################. ",
63" .##############. ",
64" .##############. ",
65" .############. ",
66" .##########. ",
67" ..######.. ",
68" ...... ",
69" "};
70
71
72/* XPM */
73static const char * happy_xpm[] = {
74"20 20 3 1",
75 " c None",
76 ".c #ffff3f ",
77 "#c #000000",
78" ",
79" ###### ",
80" ##......## ",
81" #..........# ",
82" #............# ",
83" #..............# ",
84" #..............# ",
85" #....##....##....# ",
86" #....##....##....# ",
87" #................# ",
88" #................# ",
89" #................# ",
90" #...#........#...# ",
91" #.##........##.# ",
92" #...########...# ",
93" #...######...# ",
94" #..........# ",
95" ##......## ",
96" ###### ",
97" "};
98
99
100/* XPM */
101static const char * worried_xpm[] = {
102"20 20 3 1",
103 " c None",
104 ".c #ffff3f",
105 "#c #000000",
106" ",
107" ###### ",
108" ##......## ",
109" #..........# ",
110" #............# ",
111" #..............# ",
112" #..............# ",
113" #....##....##....# ",
114" #....##....##....# ",
115" #................# ",
116" #................# ",
117" #................# ",
118" #................# ",
119" #....######....# ",
120" #..............# ",
121" #............# ",
122" #..........# ",
123" ##......## ",
124" ###### ",
125" "};
126
127
128/* XPM */
129static const char * dead_xpm[] = {
130"20 20 3 1",
131 " c None",
132 ".c #ffff3f",
133 "#c #000000",
134" ",
135" ###### ",
136" ##......## ",
137" #..........# ",
138" #............# ",
139" #..............# ",
140" #..#.#...#.#...# ",
141" #....#.....#.....# ",
142" #...#.#...#.#....# ",
143" #................# ",
144" #................# ",
145" #................# ",
146" #......####......# ",
147" #....# #....# ",
148" #...#......#...# ",
149" #............# ",
150" #..........# ",
151" ##......## ",
152" ###### ",
153" "};
154
155
156class ResultIndicator : private QLabel
157{
158public:
159 static void showResult( QWidget *ref, bool won );
160private:
161 ResultIndicator( QWidget *parent, const char *name, WFlags f)
162 :QLabel( parent, name, f ) {}
163
164 void timerEvent( QTimerEvent *);
165 void center();
166 bool twoStage;
167 int timerId;
168};
169
170void ResultIndicator::showResult( QWidget *ref, bool won )
171{
172 ResultIndicator *r = new ResultIndicator( ref, 0, WStyle_Customize | WStyle_Tool | WType_TopLevel );
173
174 r->setAlignment( AlignCenter );
175 r->setFrameStyle( Sunken|StyledPanel );
176 if ( won ) {
177 r->setText( MineSweep::tr("You won!") );
178 r->center();
179 r->show();
180 r->twoStage = FALSE;
181 r->timerId = r->startTimer(1500);
182 } else {
183 QPalette p( red );
184 r->setPalette( p );
185 r->setText( MineSweep::tr("You exploded!") );
186 r->resize( ref->size() );
187 r->move( ref->mapToGlobal(QPoint(0,0)) );
188 r->show();
189 r->twoStage = TRUE;
190 r->timerId =r->startTimer(200);
191 }
192}
193
194void ResultIndicator::center()
195{
196 QWidget *w = parentWidget();
197
198 QPoint pp = w->mapToGlobal( QPoint(0,0) );
199 QSize s = sizeHint()*3;
200 pp = QPoint( pp.x() + w->width()/2 - s.width()/2,
201 pp.y() + w->height()/ 2 - s.height()/2 );
202
203 setGeometry( QRect(pp, s) );
204
205}
206
207void ResultIndicator::timerEvent( QTimerEvent *te )
208{
209 if ( te->timerId() != timerId )
210 return;
211 killTimer( timerId );
212 if ( twoStage ) {
213 center();
214 twoStage = FALSE;
215 timerId = startTimer( 1000 );
216 } else {
217 delete this;
218 }
219}
220
221
222MineSweep::MineSweep( QWidget* parent, const char* name, WFlags f )
223: QMainWindow( parent, name, f )
224{
225 srand(::time(0));
226 setCaption( tr("Mine Hunt") );
227 setIcon( Resource::loadPixmap( "minesweep_icon" ) );
228
229 QPEToolBar *menuToolBar = new QPEToolBar( this );
230 QPEMenuBar *menuBar = new QPEMenuBar( menuToolBar );
231
232 QPopupMenu *gameMenu = new QPopupMenu( this );
233 gameMenu->insertItem( tr("Beginner"), this, SLOT( beginner() ) );
234 gameMenu->insertItem( tr("Advanced"), this, SLOT( advanced() ) );
235 gameMenu->insertItem( tr("Expert"), this, SLOT( expert() ) );
236
237 menuBar->insertItem( tr("Game"), gameMenu );
238
239 QPEToolBar *toolBar = new QPEToolBar( this );
240 toolBar->setHorizontalStretchable( TRUE );
241
242 guessLCD = new QLCDNumber( toolBar );
243 toolBar->setStretchableWidget( guessLCD );
244
245 QPalette lcdPal( red );
246 lcdPal.setColor( QColorGroup::Background, QApplication::palette().active().background() );
247 lcdPal.setColor( QColorGroup::Button, QApplication::palette().active().button() );
248
249// guessLCD->setPalette( lcdPal );
250 guessLCD->setSegmentStyle( QLCDNumber::Flat );
251 guessLCD->setFrameStyle( QFrame::NoFrame );
252 guessLCD->setNumDigits( 2 );
253 guessLCD->setBackgroundMode( PaletteButton );
254 newGameButton = new QPushButton( toolBar );
255 newGameButton->setPixmap( QPixmap( pix_new ) );
256 newGameButton->setFocusPolicy(QWidget::NoFocus);
257 connect( newGameButton, SIGNAL(clicked()), this, SLOT(newGame()) );
258
259 timeLCD = new QLCDNumber( toolBar );
260// timeLCD->setPalette( lcdPal );
261 timeLCD->setSegmentStyle( QLCDNumber::Flat );
262 timeLCD->setFrameStyle( QFrame::NoFrame );
263 timeLCD->setNumDigits( 5 ); // "mm:ss"
264 timeLCD->setBackgroundMode( PaletteButton );
265
266 setToolBarsMovable ( FALSE );
267
268 addToolBar( menuToolBar );
269 addToolBar( toolBar );
270
271 QFrame *mainframe = new QFrame( this );
272 mainframe->setFrameShape( QFrame::Box );
273 mainframe->setFrameShadow( QFrame::Raised );
274 mainframe->setMargin(5);
275 mainframe->setLineWidth(2);
276 QBoxLayout *box = new QVBoxLayout( mainframe );
277 field = new MineField( mainframe );
278 box->addWidget( field, 0, AlignCenter );
279 QFont fnt = field->font();
280 fnt.setBold( TRUE );
281 field->setFont( QFont( fnt ) );
282 field->setFocus();
283 setCentralWidget( mainframe );
284
285 connect( field, SIGNAL( gameOver( bool ) ), this, SLOT( gameOver( bool ) ) );
286 connect( field, SIGNAL( mineCount( int ) ), this, SLOT( setCounter( int ) ) );
287 connect( field, SIGNAL( gameStarted()), this, SLOT( startPlaying() ) );
288
289 timer = new QTimer( this );
290
291 connect( timer, SIGNAL( timeout() ), this, SLOT( updateTime() ) );
292
293 readConfig();
294}
295
296MineSweep::~MineSweep()
297{
298 writeConfig();
299}
300
301void MineSweep::gameOver( bool won )
302{
303 field->showMines();
304 if ( won ) {
305 newGameButton->setPixmap( QPixmap( happy_xpm ) );
306 } else {
307 newGameButton->setPixmap( QPixmap( dead_xpm ) );
308 }
309 ResultIndicator::showResult( this, won );
310 timer->stop();
311}
312
313void MineSweep::newGame()
314{
315 newGame(field->level());
316}
317
318void MineSweep::newGame(int level)
319{
320 timeLCD->display( "0:00" );
321 field->setup( level );
322 newGameButton->setPixmap( QPixmap( pix_new ) );
323 timer->stop();
324}
325
326void MineSweep::startPlaying()
327{
328 newGameButton->setPixmap( QPixmap( worried_xpm ) );
329 starttime = QDateTime::currentDateTime();
330 timer->start( 1000 );
331}
332
333void MineSweep::beginner()
334{
335 newGame(1);
336}
337
338void MineSweep::advanced()
339{
340 newGame(2);
341}
342
343void MineSweep::expert()
344{
345 newGame(3);
346}
347
348void MineSweep::setCounter( int c )
349{
350 if ( !guessLCD )
351 return;
352
353 guessLCD->display( c );
354}
355
356void MineSweep::updateTime()
357{
358 if ( !timeLCD )
359 return;
360
361 int s = starttime.secsTo(QDateTime::currentDateTime());
362 if ( s/60 > 99 )
363 timeLCD->display( "-----" );
364 else
365 timeLCD->display( QString().sprintf("%2d:%02d",s/60,s%60) );
366}
367
368void MineSweep::writeConfig() const
369{
370 Config cfg("MineSweep");
371 cfg.setGroup("Panel");
372 cfg.writeEntry("Time",
373 timer->isActive() ? starttime.secsTo(QDateTime::currentDateTime()) : -1);
374 field->writeConfig(cfg);
375}
376
377void MineSweep::readConfig()
378{
379 Config cfg("MineSweep");
380 field->readConfig(cfg);
381 cfg.setGroup("Panel");
382 int s = cfg.readNumEntry("Time",-1);
383 if ( s<0 ) {
384 newGame();
385 } else {
386 startPlaying();
387 starttime = QDateTime::currentDateTime().addSecs(-s);
388 updateTime();
389 }
390}
diff --git a/noncore/games/minesweep/minesweep.h b/noncore/games/minesweep/minesweep.h
new file mode 100644
index 0000000..e860573
--- a/dev/null
+++ b/noncore/games/minesweep/minesweep.h
@@ -0,0 +1,67 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
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
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
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.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20#ifndef MINESWEEP_H
21#define MINESWEEP_H
22
23#include <qmainwindow.h>
24#include <qdatetime.h>
25
26class MineField;
27class QLCDNumber;
28class QPushButton;
29
30class MineSweep : public QMainWindow
31{
32 Q_OBJECT
33public:
34 MineSweep( QWidget* parent = 0, const char* name = 0, WFlags f = 0 );
35 ~MineSweep();
36
37public slots:
38 void gameOver( bool won );
39 void newGame();
40
41protected slots:
42 void setCounter( int );
43 void updateTime();
44
45 void beginner();
46 void advanced();
47 void expert();
48
49private slots:
50 void startPlaying();
51
52private:
53 void readConfig();
54 void writeConfig() const;
55
56 void newGame(int);
57 MineField* field;
58 QLCDNumber* guessLCD;
59 QLCDNumber* timeLCD;
60 QPushButton* newGameButton;
61
62 QDateTime starttime;
63 QTimer* timer;
64};
65
66#endif // MINESWEEP_H
67
diff --git a/noncore/games/minesweep/minesweep.pro b/noncore/games/minesweep/minesweep.pro
new file mode 100644
index 0000000..87484bc
--- a/dev/null
+++ b/noncore/games/minesweep/minesweep.pro
@@ -0,0 +1,14 @@
1 TEMPLATE= app
2 CONFIG = qt warn_on release
3 DESTDIR = $(QPEDIR)/bin
4 HEADERS = minefield.h \
5 minesweep.h
6 SOURCES = main.cpp \
7 minefield.cpp \
8 minesweep.cpp
9INCLUDEPATH += $(QPEDIR)/include
10 DEPENDPATH+= $(QPEDIR)/include
11LIBS += -lqpe
12 INTERFACES=
13
14TRANSLATIONS = ../i18n/de/minesweep.ts
diff --git a/noncore/games/minesweep/qpe-minesweep.control b/noncore/games/minesweep/qpe-minesweep.control
new file mode 100644
index 0000000..36bc221
--- a/dev/null
+++ b/noncore/games/minesweep/qpe-minesweep.control
@@ -0,0 +1,9 @@
1Files: bin/minesweep apps/Games/minesweep.desktop
2Priority: optional
3Section: qpe/games
4Maintainer: Warwick Allison <warwick@trolltech.com>
5Architecture: arm
6Version: $QPE_VERSION-3
7Depends: qpe-base ($QPE_VERSION)
8Description: Game: find the mines
9 A game for the Qtopia environment.