Diffstat (limited to 'noncore/games/minesweep/minesweep.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/games/minesweep/minesweep.cpp | 390 |
1 files changed, 390 insertions, 0 deletions
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 | |||
45 | static 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 */ | ||
73 | static 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 */ | ||
101 | static 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 */ | ||
129 | static 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 | |||
156 | class ResultIndicator : private QLabel | ||
157 | { | ||
158 | public: | ||
159 | static void showResult( QWidget *ref, bool won ); | ||
160 | private: | ||
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 | |||
170 | void 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 | |||
194 | void 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 | |||
207 | void 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 | |||
222 | MineSweep::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 | |||
296 | MineSweep::~MineSweep() | ||
297 | { | ||
298 | writeConfig(); | ||
299 | } | ||
300 | |||
301 | void 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 | |||
313 | void MineSweep::newGame() | ||
314 | { | ||
315 | newGame(field->level()); | ||
316 | } | ||
317 | |||
318 | void 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 | |||
326 | void MineSweep::startPlaying() | ||
327 | { | ||
328 | newGameButton->setPixmap( QPixmap( worried_xpm ) ); | ||
329 | starttime = QDateTime::currentDateTime(); | ||
330 | timer->start( 1000 ); | ||
331 | } | ||
332 | |||
333 | void MineSweep::beginner() | ||
334 | { | ||
335 | newGame(1); | ||
336 | } | ||
337 | |||
338 | void MineSweep::advanced() | ||
339 | { | ||
340 | newGame(2); | ||
341 | } | ||
342 | |||
343 | void MineSweep::expert() | ||
344 | { | ||
345 | newGame(3); | ||
346 | } | ||
347 | |||
348 | void MineSweep::setCounter( int c ) | ||
349 | { | ||
350 | if ( !guessLCD ) | ||
351 | return; | ||
352 | |||
353 | guessLCD->display( c ); | ||
354 | } | ||
355 | |||
356 | void 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 | |||
368 | void 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 | |||
377 | void 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 | } | ||