summaryrefslogtreecommitdiff
path: root/noncore/games/mindbreaker/mindbreaker.cpp
Unidiff
Diffstat (limited to 'noncore/games/mindbreaker/mindbreaker.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/mindbreaker/mindbreaker.cpp455
1 files changed, 297 insertions, 158 deletions
diff --git a/noncore/games/mindbreaker/mindbreaker.cpp b/noncore/games/mindbreaker/mindbreaker.cpp
index 7802e38..e1f43d0 100644
--- a/noncore/games/mindbreaker/mindbreaker.cpp
+++ b/noncore/games/mindbreaker/mindbreaker.cpp
@@ -1,10 +1,10 @@
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**
@@ -17,41 +17,101 @@
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20 20
21#include "mindbreaker.h" 21#include "mindbreaker.h"
22 22
23#include <qpe/resource.h> 23#include <qtopia/resource.h>
24#include <qpe/config.h> 24#include <qtopia/config.h>
25#include <qtopia/qpeapplication.h>
26#include <qtoolbar.h>
25 27
26#include <qpainter.h> 28#include <qpainter.h>
27#include <qpixmap.h> 29#include <qpixmap.h>
28#include <qpe/qpetoolbar.h>
29#include <qtoolbutton.h> 30#include <qtoolbutton.h>
30#include <qpushbutton.h> 31#include <qpushbutton.h>
31#include <qmessagebox.h> 32#include <qmessagebox.h>
32#include <qlabel.h> 33#include <qlabel.h>
33#include <qstyle.h> 34#include <qlayout.h>
35#include <qtimer.h>
34 36
35#include <stdlib.h> 37#include <stdlib.h>
36#include <sys/time.h> 38#include <sys/time.h>
37#include <unistd.h> 39#include <unistd.h>
38 40
39static int pegRTTI = 3393393; 41static int pegRTTI = 3393393;
40 42
43static int adjusted_panel_height;
44static int adjusted_panel_width;
45
46static int adjusted_bin_margin;
47static int adjusted_peg_size;
48static int adjusted_answerpeg_size;
49
50static int adjusted_title_height;
51static int adjusted_title_width;
52
53static int adjusted_first_peg_x_diff;
54static int adjusted_first_peg_y_diff;
55static int adjusted_peg_spacing;
56
57static int adjusted_answerpegx;
58static int adjusted_answerpegy;
59static int adjusted_answerpeg_xdiff;
60static int adjusted_answerpeg_ydiff;
61
62static int adjusted_board_height;
63static int adjusted_board_width;
64
65static void setupBoardSize(int w, int h)
66{
67 adjusted_panel_width = w * 3/4;
68 adjusted_title_width = w * 3/4;
69
70 adjusted_title_height = h/10;
71 adjusted_panel_height = (h-adjusted_title_height)/9;
72
73 adjusted_bin_margin = w * 10/240;
74 adjusted_peg_size = adjusted_panel_height*3/4;
75 adjusted_answerpeg_size = QMIN(adjusted_panel_width*15/180,adjusted_panel_height*15/25);
76
77 // looks a bit dodgy on larger sizes
78 if ( adjusted_peg_size > 40 )
79 adjusted_peg_size = 40;
80
81 adjusted_first_peg_x_diff = w * 31/240-adjusted_peg_size/2;
82 adjusted_first_peg_y_diff = (adjusted_panel_height - adjusted_peg_size)/2;
83 adjusted_peg_spacing = w * 30/240;
84
85 // looks a bit dodgy on larger sizes (still does though, but not as much...)
86 if ( adjusted_answerpeg_size > 22 )
87 adjusted_answerpeg_size = 22;
88
89 adjusted_answerpegx = adjusted_panel_width * 159/180 - adjusted_answerpeg_size/2;
90 adjusted_answerpegy = adjusted_panel_height/3 - adjusted_answerpeg_size/2;
91 adjusted_answerpeg_xdiff = adjusted_panel_width * 10/180;
92 adjusted_answerpeg_ydiff = adjusted_panel_height * 9/25;
93
94 adjusted_board_height = adjusted_title_height + (adjusted_panel_height * 9);
95 adjusted_board_width = adjusted_panel_width + (adjusted_bin_margin * 2) + adjusted_peg_size;
96
97// qDebug("Adjusted width %d height %d", adjusted_board_width, adjusted_board_height);
98}
99
100
41/* helper class, */ 101/* helper class, */
42class Peg : public QCanvasRectangle 102class Peg : public QCanvasRectangle
43{ 103{
44public: 104public:
45 Peg(QCanvas *canvas, int type, int go = -1, int pos = -1); 105 Peg(QCanvas *canvas, int type, int go = -1, int pos = -1);
46 int rtti() const {return pegRTTI; } 106 int rtti() const {return pegRTTI; }
47 void advance(int phase); 107 void advance(int phase);
48 108
49 bool hit( const QPoint &) const; 109 bool hit( const QPoint &) const;
50 110
51/* a placed peg is one that has been set down on the board correctly and 111/* a placed peg is one that has been set down on the board correctly and
52 should not be moved, only copied */ 112 should not be moved, only copied */
53 bool placed() const; 113 bool placed() const;
54 void setPlaced(bool); 114 void setPlaced(bool);
55 115
56 int pegGo() const; 116 int pegGo() const;
57 int pegPos() const; 117 int pegPos() const;
@@ -74,69 +134,75 @@ private:
74 int pegtype; 134 int pegtype;
75 int peg_go; 135 int peg_go;
76 int peg_pos; 136 int peg_pos;
77 137
78 int aniStep; 138 int aniStep;
79}; 139};
80
81int Peg::eggLevel = 0; 140int Peg::eggLevel = 0;
82QVector<QImage> Peg::normalPegs; 141QVector<QImage> Peg::normalPegs;
83QVector<QImage> Peg::specialPegs; 142QVector<QImage> Peg::specialPegs;
84 143
85void Peg::buildImages() 144void Peg::buildImages()
86{ 145{
87
88 QImage pegs = Resource::loadImage("mindbreaker/pegs"); 146 QImage pegs = Resource::loadImage("mindbreaker/pegs");
89 int x = 0; 147 int x = 0;
90 int y = 0; 148 int y = 0;
91 int i; 149 int i;
92 eggLevel = 0; 150 eggLevel = 0;
93 normalPegs.resize(10); 151 normalPegs.resize(10);
94 for (i = 0; i < 6; i++) { 152 for (i = 0; i < 6; i++) {
95 normalPegs.insert(i, new QImage(pegs.copy(x, y, peg_size, peg_size))); 153 normalPegs.insert(i, new QImage(pegs.copy(x, y, peg_size, peg_size).
154 smoothScale(adjusted_peg_size, adjusted_peg_size) ));
96 x += peg_size; 155 x += peg_size;
97 } 156 }
98 specialPegs.resize(5); 157 specialPegs.resize(5);
99 for (i = 0; i < 5; i++) { 158 for (i = 0; i < 5; i++) {
100 specialPegs.insert(i, new QImage(pegs.copy(x,y,peg_size, peg_size))); 159 specialPegs.insert(i, new QImage(pegs.copy(x,y,peg_size, peg_size).
160 smoothScale(adjusted_peg_size, adjusted_peg_size) ));
101 x += peg_size; 161 x += peg_size;
102 } 162 }
103 163
104 QImage image = Resource::loadImage("mindbreaker/mindbreaker"); 164 QImage image = Resource::loadImage("mindbreaker/mindbreaker");
105 /* copy from master image to functional images */ 165 /* copy from master image to functional images */
106 x = 0; 166 x = 0;
107 y = panel_height; 167 y = panel_height;
108 normalPegs.insert(8, 168 normalPegs.insert(8,
109 new QImage(image.copy(x, y, panel_width, panel_height))); 169 new QImage( image.copy(x, y, panel_width, panel_height).
170 smoothScale( adjusted_panel_width, adjusted_panel_height)
171 ));
110 y += panel_height; 172 y += panel_height;
111 y += title_height; 173 y += title_height;
112 normalPegs.insert(9, 174 normalPegs.insert(9,
113 new QImage(image.copy(x, y, title_width, title_height))); 175 new QImage(image.copy(x, y, title_width, title_height).
176 smoothScale( adjusted_title_width, adjusted_title_height)
177 ));
114 y += title_height; 178 y += title_height;
115 179
116 x = 6 * peg_size; 180 x = 6 * peg_size;
117 normalPegs.insert(6, 181 normalPegs.insert(6,
118 new QImage(image.copy(x, y, answerpeg_size, answerpeg_size))); 182 new QImage(image.copy(x, y, answerpeg_size, answerpeg_size).
183 smoothScale( adjusted_answerpeg_size, adjusted_answerpeg_size) ));
119 x += answerpeg_size; 184 x += answerpeg_size;
120 normalPegs.insert(7, 185 normalPegs.insert(7,
121 new QImage(image.copy(x, y, answerpeg_size, answerpeg_size))); 186 new QImage(image.copy(x, y, answerpeg_size, answerpeg_size).
187 smoothScale( adjusted_answerpeg_size, adjusted_answerpeg_size) ));
122} 188}
123 189
124QImage Peg::imageForType(int t) 190QImage Peg::imageForType(int t)
125{ 191{
126 if (eggLevel > t ) { 192 if (eggLevel > t ) {
127 if( t < 5) { 193 if( t < 5) {
128 return *specialPegs[t]; 194 return *specialPegs[t];
129 } else { 195 } else {
130 return *normalPegs[rand() % 6]; 196 return *normalPegs[rand() % 6];
131 } 197 }
132 } 198 }
133 return *normalPegs[t]; 199 return *normalPegs[t];
134} 200}
135 201
136Peg::Peg(QCanvas *canvas , int t, int g, int p) 202Peg::Peg(QCanvas *canvas , int t, int g, int p)
137 : QCanvasRectangle(canvas) 203 : QCanvasRectangle(canvas)
138{ 204{
139 setSize(normalPegs[t]->width(), normalPegs[t]->height() ); 205 setSize(normalPegs[t]->width(), normalPegs[t]->height() );
140 pegtype = t; 206 pegtype = t;
141 isplaced = FALSE; 207 isplaced = FALSE;
142 peg_pos = p; 208 peg_pos = p;
@@ -154,15 +220,15 @@ void Peg::advance(int phase) {
154 } 220 }
155} 221}
156 222
157void Peg::drawShape(QPainter &p ) 223void Peg::drawShape(QPainter &p )
158{ 224{
159 if ((pegtype == 5) && eggLevel > 5) { 225 if ((pegtype == 5) && eggLevel > 5) {
160 p.drawImage(x(), y(), *normalPegs[aniStep]); 226 p.drawImage(int(x()), int(y()), *normalPegs[aniStep]);
161 } else 227 } else
162 p.drawImage(x(), y(), imageForType(pegtype)); 228 p.drawImage(int(x()), int(y()), imageForType(pegtype));
163} 229}
164 230
165bool Peg::hit( const QPoint &p ) const 231bool Peg::hit( const QPoint &p ) const
166{ 232{
167 int ix = p.x() - int(x()); 233 int ix = p.x() - int(x());
168 int iy = p.y() - int(y()); 234 int iy = p.y() - int(y());
@@ -184,114 +250,127 @@ inline int Peg::pegGo() const
184 250
185inline int Peg::pegPos() const 251inline int Peg::pegPos() const
186{ 252{
187 return peg_pos; 253 return peg_pos;
188} 254}
189 255
190inline void Peg::setPegPos(int p) 256inline void Peg::setPegPos(int p)
191{ 257{
192 peg_pos = p; 258 peg_pos = p;
193} 259}
194 260
195inline void Peg::setPlaced(bool p) 261inline void Peg::setPlaced(bool p)
196{ 262{
197 isplaced = p; 263 isplaced = p;
198} 264}
199 265
200inline int Peg::type() const 266inline int Peg::type() const
201{ 267{
202 return pegtype; 268 return pegtype;
203} 269}
204 270
205/* Load the main image, copy from it the pegs, the board, and the answer image 271/* Load the main image, copy from it the pegs, the board, and the answer image
206 * and use these to create the tray, answer and board 272 * and use these to create the tray, answer and board
207 */ 273 */
208MindBreaker::MindBreaker( QWidget *parent, const char *name, int wFlags ) 274MindBreaker::MindBreaker( QWidget *parent, const char *name, int wFlags )
209: QMainWindow(parent, name, wFlags), 275 : QMainWindow(parent, name, wFlags)
210 canvas(board_height, board_width)
211{ 276{
212 MindBreakerBoard *m = new MindBreakerBoard(canvas, this); 277 setCaption( tr("Mind Breaker"));
213 setCentralWidget(m); 278 QPEApplication::setInputMethodHint( this, QPEApplication::AlwaysOff );
214 279 setMinimumSize(160,210);
280
281 QWidget *w = new QWidget( this );
282 w->setBackgroundColor( black );
283 QHBoxLayout *hb = new QHBoxLayout( w );
284 hb->addStretch();
285 board = new MindBreakerBoard(w);
286 hb->addWidget( board, 100 );
287 hb->addStretch();
288
289 setCentralWidget(w);
290
215 setToolBarsMovable( FALSE ); 291 setToolBarsMovable( FALSE );
216 292
217 QToolBar *tb = new QToolBar(this); 293 QToolBar *tb = new QToolBar(this);
218 tb->setHorizontalStretchable( TRUE ); 294 tb->setHorizontalStretchable( TRUE );
219 295
220 QPixmap newicon = Resource::loadPixmap("new"); 296 QIconSet newicon = Resource::loadIconSet("new");
221 new QToolButton(newicon, tr("New Game"), 0, 297 new QToolButton(newicon, tr("New Game"), 0,
222 m, SLOT(clear()), tb, "NewGame"); 298 board, SLOT(clear()), tb, "NewGame");
223 299
224 score = new QToolButton(tb); 300 score = new QToolButton(tb);
225 score->setText(""); 301 score->setText("");
226 score->setMaximumHeight(20); 302 score->setMaximumHeight(20);
227 score->setUsesTextLabel(TRUE); 303 score->setUsesTextLabel(TRUE);
228 tb->setStretchableWidget(score); 304 tb->setStretchableWidget(score);
229 305
230 connect(m, SIGNAL(scoreChanged(int, int)), this, SLOT(setScore(int, int))); 306 connect(board, SIGNAL(scoreChanged(int,int)), this, SLOT(setScore(int,int)));
231 connect(score, SIGNAL(clicked()), m, SLOT(resetScore())); 307 connect(score, SIGNAL(clicked()), board, SLOT(resetScore()));
232 308
233 int a, b; 309 int a, b;
234 m->getScore(&a, &b); 310 board->getScore(&a, &b);
235 setScore(a,b); 311 setScore(a,b);
312
313 layout()->setResizeMode(QLayout::FreeResize);
236} 314}
237 315
238void MindBreaker::setScore(int turns, int games) 316void MindBreaker::setScore(int turns, int games)
239{ 317{
240 double average; 318 double average;
241 double total_turns = turns; 319 double total_turns = turns;
242 double total_games = games; 320 double total_games = games;
243 321
244 if(total_games > 0) 322 if(total_games > 0)
245 average = total_turns / total_games; 323 average = total_turns / total_games;
246 else 324 else
247 average = 0.0; 325 average = 0.0;
248 326
249 score->setText(tr("win avg: %1 turns (%2 games)").arg(average).arg(games)); 327 score->setText(tr("win avg: %1 turns (%2 games)").arg(average).arg(games));
250} 328}
251 329
330void MindBreaker::resizeEvent( QResizeEvent *e )
331{
332 board->fixSize();
333 QMainWindow::resizeEvent( e );
334}
335
252 336
253MindBreakerBoard::MindBreakerBoard( QCanvas &canv, QWidget *parent, 337MindBreakerBoard::MindBreakerBoard( QWidget *parent,
254 const char *name, int wFlags ) 338 const char *name, int wFlags )
255 : QCanvasView(&canv, parent, name, wFlags) 339 : QCanvasView(0, parent, name, wFlags),
340 moving(0), game_over(FALSE), total_turns(0), total_games(0)
256{ 341{
257 int i, x, y; 342 setFrameStyle( NoFrame );
343 setupBoardSize(qApp->desktop()->width(),qApp->desktop()->height());
344 cnv.resize(100,100);
345 setCanvas(&cnv);
346 setBackgroundColor( black );
347
258 struct timeval tv; 348 struct timeval tv;
259 349
260 current_go = 0; 350 current_go = 0;
261 gettimeofday(&tv, 0); 351 gettimeofday(&tv, 0);
262
263 srand(tv.tv_usec); 352 srand(tv.tv_usec);
264 353
265 canvas()->setAdvancePeriod(500); 354 canvas()->setAdvancePeriod(500);
355 current_highlight = 0;
266 356
267 QImage image = Resource::loadImage("mindbreaker/mindbreaker"); 357 widthTimer = new QTimer( this );
268 358 connect(widthTimer, SIGNAL(timeout()), this, SLOT(doFixSize()) );
269 /* copy from master image to functional images */
270 x = 0;
271 y = 0;
272 panelImage = image.copy(x,y, panel_width, panel_height);
273 y += panel_height;
274 y += panel_height;
275
276 titleImage = image.copy(x, y, title_width, title_height);
277
278 Peg::buildImages(); // must be done BEFORE any pegs are made
279
280 current_highlight = new Peg(canvas(), 8);
281 current_highlight->setPlaced(TRUE);
282 current_highlight->setX(0);
283 current_highlight->setY(board_height - ((current_go + 1) * panel_height));
284 current_highlight->setZ(0);
285 current_highlight->show();
286 359
360 setMaximumWidth( QMIN(qApp->desktop()->height(),qApp->desktop()->width()) );
361 //doFixSize(); // build images... needs to be done before reading config.
362 //readConfig(); // first read... to ensure initial labels and side look right.
363}
287 364
288 /* set up the game */ 365void MindBreakerBoard::readConfig()
366{
289 Config c("MindBreaker", Config::User); 367 Config c("MindBreaker", Config::User);
290 c.setGroup("Board"); 368 c.setGroup("Board");
291 game_over = FALSE; 369 game_over = FALSE;
370 int i;
292 if (c.readNumEntry("Answer0") < 0) { 371 if (c.readNumEntry("Answer0") < 0) {
293 for (i = 0; i < 4; i++) { 372 for (i = 0; i < 4; i++) {
294 answer[i] = rand() % 6; 373 answer[i] = rand() % 6;
295 current_guess[i] = 6; 374 current_guess[i] = 6;
296 } 375 }
297 total_turns = 0; 376 total_turns = 0;
@@ -306,13 +385,13 @@ MindBreakerBoard::MindBreakerBoard( QCanvas &canv, QWidget *parent,
306 if(total_games < 0) 385 if(total_games < 0)
307 total_games = 0; 386 total_games = 0;
308 387
309 388
310 checkScores(); 389 checkScores();
311 c.setGroup("Board"); 390 c.setGroup("Board");
312 for(i = 0; i < 4; i++) 391 for(i = 0; i < 4; i++)
313 answer[i] = c.readNumEntry(QString("Answer%1").arg(i)); 392 answer[i] = c.readNumEntry(QString("Answer%1").arg(i));
314 /* read, and parse past guesses */ 393 /* read, and parse past guesses */
315 current_go = 0; 394 current_go = 0;
316 for(j=0; j < 9; j++) { 395 for(j=0; j < 9; j++) {
317 current_guess[0] = c.readNumEntry(QString("Go%1p0").arg(j)); 396 current_guess[0] = c.readNumEntry(QString("Go%1p0").arg(j));
318 if (current_guess[0] < 0) 397 if (current_guess[0] < 0)
@@ -325,46 +404,47 @@ MindBreakerBoard::MindBreakerBoard( QCanvas &canv, QWidget *parent,
325 current_guess[3] = c.readNumEntry(QString("Go%1p3").arg(j)); 404 current_guess[3] = c.readNumEntry(QString("Go%1p3").arg(j));
326 placeGuessPeg(3, current_guess[3]); 405 placeGuessPeg(3, current_guess[3]);
327 checkGuess(); 406 checkGuess();
328 } 407 }
329 for(i = 0; i < 4; i++) { 408 for(i = 0; i < 4; i++) {
330 current_guess[i] = c.readNumEntry(QString("CurrentGo%1").arg(i)); 409 current_guess[i] = c.readNumEntry(QString("CurrentGo%1").arg(i));
331 if (current_guess[i] != 6) 410 if (current_guess[i] != 6)
332 placeGuessPeg(i, current_guess[i]); 411 placeGuessPeg(i, current_guess[i]);
333 } 412 }
334 } 413 }
335
336 /* draw initial screen */
337 drawBackground();
338 canvas()->update();
339} 414}
340 415
341MindBreakerBoard::~MindBreakerBoard() 416MindBreakerBoard::~MindBreakerBoard()
342{ 417{
343 int i, j; 418 int i;
344 if (game_over) { 419 if (game_over) {
345 current_go = 0; 420 current_go = 0;
346 /* clear the answer, clear the guess */ 421 /* clear the answer, clear the guess */
347 for (i = 0; i < 4; i++) { 422 for (i = 0; i < 4; i++) {
348 answer[i] = rand() % 6; 423 answer[i] = rand() % 6;
349 current_guess[i] = 6; 424 current_guess[i] = 6;
350 } 425 }
351 } 426 }
352 427 writeConfig();
428}
429
430void MindBreakerBoard::writeConfig()
431{
353 Config c("MindBreaker", Config::User); 432 Config c("MindBreaker", Config::User);
354 c.setGroup("Board"); 433 c.setGroup("Board");
355 c.clearGroup(); 434 c.clearGroup();
356 /* write the board */ 435 /* write the board */
436 int i,j;
357 for (i = 0; i < current_go; i++) { 437 for (i = 0; i < current_go; i++) {
358 for(j = 0; j < 4; j++) 438 for(j = 0; j < 4; j++)
359 c.writeEntry(tr("Go%1p%2").arg(i).arg(j), past_guesses[4*i+j]); 439 c.writeEntry(QString("Go%1p%2").arg(i).arg(j), past_guesses[4*i+j]);
360 } 440 }
361 for(j = 0; j < 4; j++) 441 for(j = 0; j < 4; j++)
362 c.writeEntry(tr("CurrentGo%1").arg(j), current_guess[j]); 442 c.writeEntry(QString("CurrentGo%1").arg(j), current_guess[j]);
363 for(j = 0; j < 4; j++) 443 for(j = 0; j < 4; j++)
364 c.writeEntry(tr("Answer%1").arg(j), answer[j]); 444 c.writeEntry(QString("Answer%1").arg(j), answer[j]);
365 445
366 c.setGroup("Score"); 446 c.setGroup("Score");
367 /* write the score */ 447 /* write the score */
368 448
369 c.writeEntry("Turns", total_turns); 449 c.writeEntry("Turns", total_turns);
370 c.writeEntry("Games", total_games); 450 c.writeEntry("Games", total_games);
@@ -374,17 +454,81 @@ void MindBreakerBoard::getScore(int *a, int *b)
374{ 454{
375 *a = total_turns; 455 *a = total_turns;
376 *b = total_games; 456 *b = total_games;
377 return; 457 return;
378} 458}
379 459
460void MindBreakerBoard::fixSize()
461{
462 hide();
463 setMaximumWidth( parentWidget()->height() );
464 widthTimer->start( 20, TRUE );
465}
466
467void MindBreakerBoard::doFixSize()
468{
469 QSize s = size();
470 int fw = frameWidth();
471 s.setWidth(s.width() - fw);
472 s.setHeight(s.height() - fw);
473
474 /* min size is 200 x 260 */
475/*
476 if (s.width() < adjusted_board_width)
477 s.setWidth(adjusted_board_width);
478
479 if (s.height() < adjusted_board_height)
480 s.setHeight(adjusted_board_height);
481*/
482
483 if ( current_highlight ) // non-first resize
484 writeConfig();
485
486 setupBoardSize(s.width() - fw, s.height() - fw);
487 canvas()->resize(s.width() - fw, s.height() - fw);
488 Peg::buildImages(); // must be done BEFORE any pegs are made
489
490 QImage image = Resource::loadImage("mindbreaker/mindbreaker");
491
492 /* copy from master image to functional images */
493 int x = 0;
494 int y = 0;
495 panelImage = image.copy(x, y, panel_width, panel_height).
496 smoothScale( adjusted_panel_width, adjusted_panel_height);
497
498 y += panel_height;
499 y += panel_height;
500
501 titleImage = image.copy(x, y, title_width, title_height).
502 smoothScale( adjusted_title_width, adjusted_title_height);
503 show();
504
505 delete current_highlight;
506 current_highlight = new Peg(canvas(), 8);
507 current_highlight->setPlaced(TRUE);
508 current_highlight->setX(0);
509 current_highlight->setY(adjusted_board_height - ((current_go + 1) * adjusted_panel_height));
510 current_highlight->setZ(0);
511 current_highlight->show();
512
513 /* set up the game */
514 //readConfig();
515
516 /* draw initial screen */
517 //drawBackground();
518 //canvas()->update();
519 clear();
520
521 readConfig();
522}
523
380void MindBreakerBoard::placeGuessPeg(int pos, int pegId) 524void MindBreakerBoard::placeGuessPeg(int pos, int pegId)
381{ 525{
382 int x = first_peg_x_diff + (pos * peg_spacing); 526 int x = adjusted_first_peg_x_diff + (pos * adjusted_peg_spacing);
383 int y = board_height - ((current_go + 1) * panel_height) 527 int y = adjusted_board_height - ((current_go + 1) * adjusted_panel_height)
384 + first_peg_y_diff; 528 + adjusted_first_peg_y_diff;
385 529
386 Peg *peg = new Peg(canvas(), pegId, current_go, pos); 530 Peg *peg = new Peg(canvas(), pegId, current_go, pos);
387 peg->setPegPos(pos); 531 peg->setPegPos(pos);
388 peg->setPlaced(TRUE); 532 peg->setPlaced(TRUE);
389 peg->setX(x); 533 peg->setX(x);
390 peg->setY(y); 534 peg->setY(y);
@@ -402,54 +546,54 @@ void MindBreakerBoard::drawBackground()
402 painter.fillRect(0, 0, canvas()->width(), canvas()->height(), QColor(0,0,0)); 546 painter.fillRect(0, 0, canvas()->width(), canvas()->height(), QColor(0,0,0));
403 /* very first thing is to draw the bins, as everything else needs 547 /* very first thing is to draw the bins, as everything else needs
404 * to be drawn over them */ 548 * to be drawn over them */
405 549
406 QPen pen(QColor(85, 45, 27), 4); 550 QPen pen(QColor(85, 45, 27), 4);
407 painter.setPen(pen); 551 painter.setPen(pen);
408 x_gap = canvas()->width() - (panel_width + (2 * bin_margin)); 552 x_gap = canvas()->width() - (adjusted_panel_width + (2 * adjusted_bin_margin));
409 //x_gap += peg_size >> 1; 553 //x_gap += peg_size >> 1;
410 if (x_gap < 1) 554 if (x_gap < 1)
411 x_gap = 1; 555 x_gap = 1;
412 556
413 y_gap = board_height / 6; 557 y_gap = adjusted_board_height / 6;
414 y_gap -= (2 * bin_margin); 558 y_gap -= (2 * adjusted_bin_margin);
415 //y_gap += peg_size >> 1; 559 //y_gap += peg_size >> 1;
416 if (y_gap < 1) 560 if (y_gap < 1)
417 y_gap = 1; 561 y_gap = 1;
418 x = panel_width + bin_margin - (peg_size >> 1); 562 x = adjusted_panel_width + adjusted_bin_margin - (adjusted_peg_size >> 1);
419 y = bin_margin - (peg_size >> 1) + 2; 563 y = adjusted_bin_margin - (adjusted_peg_size >> 1) + 2;
420 564
421 for (i = 0; i < 6; i++) { 565 for (i = 0; i < 6; i++) {
422 for (j = 0; j < 10; j++) { 566 for (j = 0; j < 10; j++) {
423 int rx = x + (rand() % x_gap); 567 int rx = x + (rand() % x_gap);
424 int ry = y + (rand() % y_gap); 568 int ry = y + (rand() % y_gap);
425 painter.drawImage(rx,ry, Peg::imageForType(i)); 569 painter.drawImage(rx,ry, Peg::imageForType(i));
426 } 570 }
427 y += board_height / 6; 571 y += adjusted_board_height / 6;
428 } 572 }
429 /* now draw the surrounding boxes */ 573 /* now draw the surrounding boxes */
430 x_gap = canvas()->width() - panel_width; 574 x_gap = canvas()->width() - adjusted_panel_width;
431 if (x_gap < 1) x_gap = 1; 575 if (x_gap < 1) x_gap = 1;
432 y_gap = board_height / 6; 576 y_gap = adjusted_board_height / 6;
433 x = panel_width; 577 x = adjusted_panel_width;
434 y = 1; 578 y = 1;
435 579
436 for (i = 0; i < 6; i++) { 580 for (i = 0; i < 6; i++) {
437 painter.drawRect(x, y, x_gap, y_gap); 581 painter.drawRect(x, y, x_gap, y_gap);
438 y += y_gap; 582 y += y_gap;
439 } 583 }
440 584
441 x = 0; 585 x = 0;
442 y = 0; 586 y = 0;
443 587
444 painter.drawImage(x,y, titleImage); 588 painter.drawImage(x,y, titleImage);
445 y = title_height; 589 y = adjusted_title_height;
446 /* now nine gues panels */ 590 /* now nine gues panels */
447 for (i = 0; i < 9; i ++) { 591 for (i = 0; i < 9; i ++) {
448 painter.drawImage(x, y, panelImage); 592 painter.drawImage(x, y, panelImage);
449 y += panel_height; 593 y += adjusted_panel_height;
450 } 594 }
451 595
452 painter.flush(); 596 painter.flush();
453 canvas()->setBackgroundPixmap(background); 597 canvas()->setBackgroundPixmap(background);
454} 598}
455 599
@@ -466,13 +610,13 @@ void MindBreakerBoard::checkGuess()
466 copy_guess[i] = current_guess[i]; 610 copy_guess[i] = current_guess[i];
467 if (current_guess[i] == 6) 611 if (current_guess[i] == 6)
468 return; 612 return;
469 if (answer[i] == current_guess[i]) { 613 if (answer[i] == current_guess[i]) {
470 num_black++; 614 num_black++;
471 copy_answer[i] = 6; 615 copy_answer[i] = 6;
472 copy_guess[i] = 7; 616 copy_guess[i] = 7;
473 } 617 }
474 } 618 }
475 619
476 /* now sure that user has completed a 'guess' */ 620 /* now sure that user has completed a 'guess' */
477 for (i = 0; i < 4; i++) { 621 for (i = 0; i < 4; i++) {
478 if (copy_guess[i] == 7) 622 if (copy_guess[i] == 7)
@@ -483,48 +627,48 @@ void MindBreakerBoard::checkGuess()
483 num_white++; 627 num_white++;
484 break; 628 break;
485 } 629 }
486 } 630 }
487 } 631 }
488 632
489 int x = answerpegx; 633 int x = adjusted_answerpegx;
490 int y = (board_height - ((current_go + 1) * panel_height)) + answerpegy; 634 int y = (adjusted_board_height - ((current_go + 1) * adjusted_panel_height)) + adjusted_answerpegy;
491 635
492 if (num_black == 4) 636 if (num_black == 4)
493 game_over = TRUE; 637 game_over = TRUE;
494 638
495 while(num_black > 0) { 639 while(num_black > 0) {
496 Peg *p = new Peg(canvas(), 7); 640 Peg *p = new Peg(canvas(), 7);
497 p->setPlaced(TRUE); 641 p->setPlaced(TRUE);
498 p->setX(x); 642 p->setX(x);
499 p->setY(y); 643 p->setY(y);
500 p->setZ(1); 644 p->setZ(1);
501 p->show(); 645 p->show();
502 num_black--; 646 num_black--;
503 647
504 if (x == answerpegx) 648 if (x == adjusted_answerpegx)
505 x = answerpegx + answerpeg_diff; 649 x = adjusted_answerpegx + adjusted_answerpeg_xdiff;
506 else { 650 else {
507 x = answerpegx; 651 x = adjusted_answerpegx;
508 y += answerpeg_diff; 652 y += adjusted_answerpeg_ydiff;
509 } 653 }
510 } 654 }
511 while(num_white > 0){ 655 while(num_white > 0){
512 Peg *p = new Peg(canvas(), 6); 656 Peg *p = new Peg(canvas(), 6);
513 p->setPlaced(TRUE); 657 p->setPlaced(TRUE);
514 p->setX(x); 658 p->setX(x);
515 p->setY(y); 659 p->setY(y);
516 p->setZ(1); 660 p->setZ(1);
517 p->show(); 661 p->show();
518 num_white--; 662 num_white--;
519 663
520 if (x == answerpegx) 664 if (x == adjusted_answerpegx)
521 x = answerpegx + answerpeg_diff; 665 x = adjusted_answerpegx + adjusted_answerpeg_xdiff;
522 else { 666 else {
523 x = answerpegx; 667 x = adjusted_answerpegx;
524 y += answerpeg_diff; 668 y += adjusted_answerpeg_ydiff;
525 } 669 }
526 } 670 }
527 /* move to next go */ 671 /* move to next go */
528 for(i = 0; i < 4; i++) { 672 for(i = 0; i < 4; i++) {
529 past_guesses[4*current_go+i] = current_guess[i]; 673 past_guesses[4*current_go+i] = current_guess[i];
530 current_guess[i] = 6; 674 current_guess[i] = 6;
@@ -532,33 +676,33 @@ void MindBreakerBoard::checkGuess()
532 676
533 current_go++; 677 current_go++;
534 if((current_go > 8) || game_over) { 678 if((current_go > 8) || game_over) {
535 total_games++; 679 total_games++;
536 if(!game_over) 680 if(!game_over)
537 total_turns += 10; 681 total_turns += 10;
538 else 682 else
539 total_turns += current_go; 683 total_turns += current_go;
540 684
541 emit scoreChanged(total_turns, total_games); 685 emit scoreChanged(total_turns, total_games);
542 Peg *p = new Peg(canvas(), 9); 686 Peg *p = new Peg(canvas(), 9);
543 game_over = TRUE; 687 game_over = TRUE;
544 p->setPlaced(TRUE); 688 p->setPlaced(TRUE);
545 p->setX(0); 689 p->setX(0);
546 p->setY(0); 690 p->setY(0);
547 p->setZ(0); 691 p->setZ(0);
548 p->show(); 692 p->show();
549 693
550 for (i = 0; i < 4; i++) { 694 for (i = 0; i < 4; i++) {
551 p = new Peg(canvas(), answer[i], -1); 695 p = new Peg(canvas(), answer[i], -1);
552 p->setX(first_peg_x_diff + (i * peg_spacing)); 696 p->setX(adjusted_first_peg_x_diff + (i * adjusted_peg_spacing));
553 p->setY(5); 697 p->setY(adjusted_first_peg_y_diff);
554 p->setZ(3); 698 p->setZ(3);
555 p->show(); 699 p->show();
556 } 700 }
557 } else { 701 } else {
558 current_highlight->setY(board_height - ((current_go + 1) * panel_height)); 702 current_highlight->setY(adjusted_board_height - ((current_go + 1) * adjusted_panel_height));
559 } 703 }
560 canvas()->update(); 704 canvas()->update();
561} 705}
562 706
563void MindBreakerBoard::clear() 707void MindBreakerBoard::clear()
564{ 708{
@@ -583,23 +727,23 @@ void MindBreakerBoard::clear()
583 if (*it == current_highlight) 727 if (*it == current_highlight)
584 continue; 728 continue;
585 if (*it) 729 if (*it)
586 delete *it; 730 delete *it;
587 } 731 }
588 732
589 current_highlight->setY(board_height - ((current_go + 1) * panel_height)); 733 current_highlight->setY(adjusted_board_height - ((current_go + 1) * adjusted_panel_height));
590 checkScores(); 734 checkScores();
591 drawBackground(); 735 drawBackground();
592 canvas()->update(); 736 canvas()->update();
593} 737}
594 738
595void MindBreakerBoard::resetScore() 739void MindBreakerBoard::resetScore()
596{ 740{
597 /* are u sure */ 741 /* are u sure */
598 742
599 if (QMessageBox::information(this, tr( "Reset Statistics" ), 743 if (QMessageBox::information(this, tr( "Reset Statistics" ),
600 tr( "Reset the win ratio?" ), 744 tr( "Reset the win ratio?" ),
601 tr( "OK" ), tr( "Cancel" ) ) == 0) { 745 tr( "OK" ), tr( "Cancel" ) ) == 0) {
602 total_turns = 0; 746 total_turns = 0;
603 total_games = 0; 747 total_games = 0;
604 Peg::eggLevel = 0; 748 Peg::eggLevel = 0;
605 drawBackground(); 749 drawBackground();
@@ -609,27 +753,34 @@ void MindBreakerBoard::resetScore()
609} 753}
610 754
611/* EVENTS */ 755/* EVENTS */
612 756
613void MindBreakerBoard::contentsMousePressEvent(QMouseEvent *e) 757void MindBreakerBoard::contentsMousePressEvent(QMouseEvent *e)
614{ 758{
759 if (game_over) {
760 null_press = TRUE;
761 null_point = e->pos();
762 moving = 0;
763 return;
764 }
765
615 copy_press = FALSE; 766 copy_press = FALSE;
616 null_press = FALSE; 767 null_press = FALSE;
617 /* ok, first work out if it is one of the bins that 768 /* ok, first work out if it is one of the bins that
618 got clicked */ 769 got clicked */
619 if (e->x() > panel_width) { 770 if (e->x() > adjusted_panel_width) {
620 /* its a bin, but which bin */ 771 /* its a bin, but which bin */
621 if(e->y() > board_height) 772 int bin = (e->y() + 2) / (adjusted_board_height / 6);
773 if (bin > 5)
622 return; // missed everything 774 return; // missed everything
623 int bin = (e->y() + 2) / (board_height / 6);
624 775
625 /* make new peg... set it moving */ 776 /* make new peg... set it moving */
626 moving_pos = e->pos(); 777 moving_pos = e->pos();
627 moving = new Peg(canvas(), bin, current_go); 778 moving = new Peg(canvas(), bin, current_go);
628 moving->setX(e->x() - (peg_size >> 1)); 779 moving->setX(e->x() - (adjusted_peg_size >> 1));
629 moving->setY(e->y() - (peg_size >> 1)); 780 moving->setY(e->y() - (adjusted_peg_size >> 1));
630 moving->setZ(5); 781 moving->setZ(5);
631 moving->show(); 782 moving->show();
632 canvas()->update(); 783 canvas()->update();
633 return; 784 return;
634 } 785 }
635 786
@@ -649,16 +800,16 @@ void MindBreakerBoard::contentsMousePressEvent(QMouseEvent *e)
649 if(item->pegGo() == -1) 800 if(item->pegGo() == -1)
650 return; 801 return;
651 if(item->pegGo() == current_go) { 802 if(item->pegGo() == current_go) {
652 copy_press = TRUE; 803 copy_press = TRUE;
653 copy_peg = item; 804 copy_peg = item;
654 } 805 }
655 moving = new Peg(canvas(), 806 moving = new Peg(canvas(),
656 item->type(), current_go); 807 item->type(), current_go);
657 moving->setX(e->x() - (peg_size >> 1)); 808 moving->setX(e->x() - (adjusted_peg_size >> 1));
658 moving->setY(e->y() - (peg_size >> 1)); 809 moving->setY(e->y() - (adjusted_peg_size >> 1));
659 moving->setZ(5); 810 moving->setZ(5);
660 moving->show(); 811 moving->show();
661 moving_pos = QPoint(e->x(), e->y()); 812 moving_pos = QPoint(e->x(), e->y());
662 canvas()->update(); 813 canvas()->update();
663 return; 814 return;
664 } 815 }
@@ -678,25 +829,25 @@ void MindBreakerBoard::contentsMouseMoveEvent(QMouseEvent* e)
678 if (moving ) { 829 if (moving ) {
679 moving->moveBy(e->pos().x() - moving_pos.x(), 830 moving->moveBy(e->pos().x() - moving_pos.x(),
680 e->pos().y() - moving_pos.y()); 831 e->pos().y() - moving_pos.y());
681 moving_pos = e->pos(); 832 moving_pos = e->pos();
682 canvas()->update(); 833 canvas()->update();
683 return; 834 return;
684 } 835 }
685} 836}
686 837
687void MindBreakerBoard::contentsMouseReleaseEvent(QMouseEvent* e) 838void MindBreakerBoard::contentsMouseReleaseEvent(QMouseEvent* e)
688{ 839{
689 /* time to put down the peg */ 840 /* time to put down the peg */
690 if(moving) { 841 if(moving) {
691 if(copy_press) { 842 if(copy_press) {
692 /* check if collided with original. if so, delete both */ 843 /* check if collided with original. if so, delete both */
693 copy_press = FALSE; 844 copy_press = FALSE;
694 QCanvasItemList l = canvas()->collisions(e->pos()); 845 QCanvasItemList l = canvas()->collisions(e->pos());
695 for (QCanvasItemList::Iterator it=l.begin(); it !=l.end(); ++it) { 846 for (QCanvasItemList::Iterator it=l.begin(); it !=l.end(); ++it) {
696 if (*it == copy_peg) 847 if (*it == copy_peg)
697 copy_press = TRUE; 848 copy_press = TRUE;
698 } 849 }
699 if (copy_press) { 850 if (copy_press) {
700 current_guess[copy_peg->pegPos()] = 6; 851 current_guess[copy_peg->pegPos()] = 6;
701 delete copy_peg; 852 delete copy_peg;
702 delete moving; 853 delete moving;
@@ -704,51 +855,51 @@ void MindBreakerBoard::contentsMouseReleaseEvent(QMouseEvent* e)
704 moving = 0; 855 moving = 0;
705 copy_peg = 0; 856 copy_peg = 0;
706 canvas()->update(); 857 canvas()->update();
707 return; 858 return;
708 } 859 }
709 } 860 }
710 861
711 /* first work out if in y */ 862 /* first work out if in y */
712 if (e->y() > (board_height - (current_go * panel_height))) { 863 if (e->y() > (adjusted_board_height - (current_go * adjusted_panel_height))) {
713 delete moving; 864 delete moving;
714 moving = 0; 865 moving = 0;
715 canvas()->update(); 866 canvas()->update();
716 return; 867 return;
717 } 868 }
718 if (e->y() < (board_height - ((current_go + 1) * panel_height))) { 869 if (e->y() < (adjusted_board_height - ((current_go + 1) * adjusted_panel_height))) {
719 delete moving; 870 delete moving;
720 moving = 0; 871 moving = 0;
721 canvas()->update(); 872 canvas()->update();
722 return; 873 return;
723 } 874 }
724 /* ok, a valid go, but which peg */ 875 /* ok, a valid go, but which peg */
725 int x_bar = first_peg_x_diff - (peg_size >> 1); 876 int x_bar = adjusted_first_peg_x_diff - (adjusted_peg_size >> 1);
726 x_bar += peg_spacing; 877 x_bar += adjusted_peg_spacing;
727 int pos = 0; 878 int pos = 0;
728 if (e->x() > x_bar) 879 if (e->x() > x_bar)
729 pos = 1; 880 pos = 1;
730 x_bar += peg_spacing; 881 x_bar += adjusted_peg_spacing;
731 if (e->x() > x_bar) 882 if (e->x() > x_bar)
732 pos = 2; 883 pos = 2;
733 x_bar += peg_spacing; 884 x_bar += adjusted_peg_spacing;
734 if (e->x() > x_bar) 885 if (e->x() > x_bar)
735 pos = 3; 886 pos = 3;
736 x_bar += peg_spacing; 887 x_bar += adjusted_peg_spacing;
737 888
738 if (e->x() > x_bar) { 889 if (e->x() > x_bar) {
739 /* invalid x */ 890 /* invalid x */
740 delete moving; 891 delete moving;
741 moving = 0; 892 moving = 0;
742 canvas()->update(); 893 canvas()->update();
743 return; 894 return;
744 } 895 }
745 896
746 int x = first_peg_x_diff + (pos * peg_spacing); 897 int x = adjusted_first_peg_x_diff + (pos * adjusted_peg_spacing);
747 int y = board_height - ((current_go + 1) * panel_height) 898 int y = adjusted_board_height - ((current_go + 1) * adjusted_panel_height)
748 + first_peg_y_diff; 899 + adjusted_first_peg_y_diff;
749 moving->setPegPos(pos); 900 moving->setPegPos(pos);
750 moving->setX(x); 901 moving->setX(x);
751 moving->setY(y); 902 moving->setY(y);
752 moving->setZ(2); 903 moving->setZ(2);
753 904
754 /* remove all other pegs from this position */ 905 /* remove all other pegs from this position */
@@ -766,53 +917,41 @@ void MindBreakerBoard::contentsMouseReleaseEvent(QMouseEvent* e)
766 canvas()->update(); 917 canvas()->update();
767 return; 918 return;
768 } 919 }
769 moving = 0; 920 moving = 0;
770 null_point -= e->pos(); 921 null_point -= e->pos();
771 if(null_point.manhattanLength() < 6) { 922 if(null_point.manhattanLength() < 6) {
772 if (game_over) 923 if (game_over)
773 clear(); 924 clear();
774 else 925 else
775 checkGuess(); 926 checkGuess();
776 } 927 }
777} 928}
778 929
779void MindBreakerBoard::resizeEvent(QResizeEvent *e) 930void MindBreakerBoard::resizeEvent(QResizeEvent *e)
780{ 931{
781 QSize s = e->size(); 932 QCanvasView::resizeEvent(e);
782 int fw = style().defaultFrameWidth(); 933 fixSize();
783 s.setWidth(s.width() - fw);
784 s.setHeight(s.height() - fw);
785
786 /* min size is 200 x 260 */
787 if (s.width() < board_width)
788 s.setWidth(board_width);
789
790 if (s.height() < board_height)
791 s.setHeight(board_height);
792
793 canvas()->resize(s.width() - fw, s.height() - fw);
794 drawBackground();
795} 934}
796 935
797 936
798/* Easter egg function... beat the clock */ 937/* Easter egg function... beat the clock */
799void MindBreakerBoard::checkScores() 938void MindBreakerBoard::checkScores()
800{ 939{
801 double games = total_games; 940 double games = total_games;
802 double turns = total_turns; 941 double turns = total_turns;
803 double g = games / 10.0; 942 double g = games / 10.0;
804 Peg::eggLevel = 0; 943 Peg::eggLevel = 0;
805 944
806 double break_even = 5.0; 945 double break_even = 5.0;
807 if (g < 1.0) 946 if (g < 1.0)
808 return; 947 return;
809 double avg = turns / games; 948 double avg = turns / games;
810 g--; 949 g--;
811 while (break_even >= 0.0) { 950 while (break_even >= 0.0) {
812 if (avg >= (break_even + g)) 951 if (avg >= (break_even + g))
813 return; 952 return;
814 // score a peg. 953 // score a peg.
815 break_even -= 1.0; 954 break_even -= 1.0;
816 Peg::eggLevel = int(5.0 - break_even); 955 Peg::eggLevel = int(5.0 - break_even);
817 } 956 }
818} 957}