|
@@ -0,0 +1,378 @@
|
|
1
|
+// Todo implement game modes: 1= square upper left, 2 = rect upper left, etc..
|
|
2
|
+
|
|
3
|
+#include "maingamewindow.h"
|
|
4
|
+#include <QDebug>
|
|
5
|
+#include <QTimer>
|
|
6
|
+#include <QTime>
|
|
7
|
+#include <QEventLoop>
|
|
8
|
+#include <QApplication>
|
|
9
|
+
|
|
10
|
+//
|
|
11
|
+// Initialices some of the data members
|
|
12
|
+//
|
|
13
|
+
|
|
14
|
+void MainGameWindow::initMembers() {
|
|
15
|
+ s = new QGraphicsScene(this);
|
|
16
|
+
|
|
17
|
+ s->setSceneRect(0,0,600,600);
|
|
18
|
+ this->setGeometry(0,0,600,600);
|
|
19
|
+
|
|
20
|
+ v = new QGraphicsView(s,this);
|
|
21
|
+ v->setGeometry(0,0,600,600);
|
|
22
|
+
|
|
23
|
+ pct_margin = .1;
|
|
24
|
+
|
|
25
|
+ srand(time(NULL));
|
|
26
|
+
|
|
27
|
+}
|
|
28
|
+
|
|
29
|
+//
|
|
30
|
+// Starts the timer that is used for animation purposes.
|
|
31
|
+//
|
|
32
|
+
|
|
33
|
+void MainGameWindow::startTheTimer() {
|
|
34
|
+ timer = new QTimer(s);
|
|
35
|
+ timer->connect(timer, SIGNAL(timeout()), s, SLOT(advance()));
|
|
36
|
+ timer->start(10);
|
|
37
|
+}
|
|
38
|
+
|
|
39
|
+//
|
|
40
|
+// Constructor that accepts a game mode argument. See the modes
|
|
41
|
+// definition in the .h.
|
|
42
|
+//
|
|
43
|
+
|
|
44
|
+MainGameWindow::MainGameWindow(Mode gameMode) : QMainWindow(0) {
|
|
45
|
+ initMembers();
|
|
46
|
+
|
|
47
|
+ // the initial row/col positions of the robot
|
|
48
|
+ uint initialX, initialY;
|
|
49
|
+
|
|
50
|
+ _rows = rand() % 9 + 2;
|
|
51
|
+ switch (gameMode) {
|
|
52
|
+ case Mode::SQUARE_TOP_LEFT:
|
|
53
|
+ _cols = _rows;
|
|
54
|
+ initialX = initialY = 0;
|
|
55
|
+ break;
|
|
56
|
+ case Mode::RECT_TOP_LEFT:
|
|
57
|
+ _cols = rand() % 9 + 2;
|
|
58
|
+ initialX = initialY = 0;
|
|
59
|
+ break;
|
|
60
|
+ case Mode::RECT_RANDOM:
|
|
61
|
+ _cols = rand() % 9 + 2;
|
|
62
|
+ initialX = rand() % _cols;
|
|
63
|
+ initialY = rand() % _rows;
|
|
64
|
+ break;
|
|
65
|
+ case Mode::PYRAMID_RANDOM:
|
|
66
|
+ _cols = (rand() % 4 + 2) * 2 + 1;
|
|
67
|
+ createPiramid();
|
|
68
|
+ assignValidPos(initialX, initialY);
|
|
69
|
+ break;
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ // This creates rectangular mazes
|
|
73
|
+ if (_maze == "") {
|
|
74
|
+ for (uint r = 0; r < _rows; r++) {
|
|
75
|
+ for (uint c = 0; c < _cols; c++) {
|
|
76
|
+ _maze.push_back('x');
|
|
77
|
+ }
|
|
78
|
+ _maze.push_back('\n');
|
|
79
|
+ }
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+ qDebug() << "MainGameWindow::MainGameWindow(): width: " << this->width();
|
|
83
|
+ qDebug() << "_maze: " << _maze;
|
|
84
|
+
|
|
85
|
+ paintMaze(initialX, initialY);
|
|
86
|
+
|
|
87
|
+ startTheTimer();
|
|
88
|
+
|
|
89
|
+ v->show();
|
|
90
|
+}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+// Creates this type of pyramid, e.g. _cols = 7
|
|
94
|
+// 000x000
|
|
95
|
+// 00xxx00
|
|
96
|
+// 0xxxxx0
|
|
97
|
+// xxxxxxx
|
|
98
|
+
|
|
99
|
+void MainGameWindow::createPiramid() {
|
|
100
|
+ _maze = "";
|
|
101
|
+ _rows = _cols/2;
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+ for (uint r = 0; r <= _rows + 1; r++) {
|
|
105
|
+ for (uint c = 0; c < _cols; c++) {
|
|
106
|
+ _maze.push_back( abs((int)c - (int)_rows) <= (int)r ? 'x':' ');
|
|
107
|
+ }
|
|
108
|
+ _maze.push_back('\n');
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ _rows++;
|
|
112
|
+}
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+//
|
|
116
|
+// Randomly determines a valid initial position for the robot
|
|
117
|
+//
|
|
118
|
+
|
|
119
|
+void MainGameWindow::assignValidPos(uint &initialX, uint &initialY) {
|
|
120
|
+ qDebug() << _maze;
|
|
121
|
+ if (_cols <= 0 || _rows <= 0) {
|
|
122
|
+ qDebug() << "assignValidPos was called with 0 rows or cols!";
|
|
123
|
+ exit(1);
|
|
124
|
+ }
|
|
125
|
+
|
|
126
|
+ // throw the dice to determine initial random position
|
|
127
|
+ do {
|
|
128
|
+ initialX = rand() % _cols;
|
|
129
|
+ initialY = rand() % _rows;
|
|
130
|
+ } while (_maze.toStdString()[initialY * (_cols + 1) + initialX] != 'x');
|
|
131
|
+}
|
|
132
|
+
|
|
133
|
+//
|
|
134
|
+// Constructor that accepts number of rows and columns.
|
|
135
|
+//
|
|
136
|
+
|
|
137
|
+MainGameWindow::MainGameWindow(uint rows, uint cols) : QMainWindow(0) {
|
|
138
|
+ _rows = rows;
|
|
139
|
+ _cols = cols;
|
|
140
|
+
|
|
141
|
+ initMembers();
|
|
142
|
+
|
|
143
|
+ _maze = "";
|
|
144
|
+ for (uint r = 0; r < _rows; r++) {
|
|
145
|
+ for (uint c = 0; c < _cols; c++) {
|
|
146
|
+ _maze.push_back('x');
|
|
147
|
+ }
|
|
148
|
+ _maze.push_back('\n');
|
|
149
|
+ }
|
|
150
|
+
|
|
151
|
+ qDebug() << "MainGameWindow::MainGameWindow(): width: " << this->width();
|
|
152
|
+ qDebug() << "_maze: " << _maze;
|
|
153
|
+
|
|
154
|
+ paintMaze(0,0);
|
|
155
|
+
|
|
156
|
+ startTheTimer();
|
|
157
|
+
|
|
158
|
+ v->show();
|
|
159
|
+}
|
|
160
|
+
|
|
161
|
+MainGameWindow::MainGameWindow(const QString &maze) {
|
|
162
|
+ _maze = maze;
|
|
163
|
+ initMembers();
|
|
164
|
+
|
|
165
|
+ if (validMaze() ) {
|
|
166
|
+ qDebug() << "valid maze!!!!";
|
|
167
|
+ paintMaze(0,0);
|
|
168
|
+ startTheTimer();
|
|
169
|
+ }
|
|
170
|
+
|
|
171
|
+}
|
|
172
|
+
|
|
173
|
+//
|
|
174
|
+// Given a string that represents the maze, determines the number
|
|
175
|
+// of rows and cols. Returns false if the maze is malformed.
|
|
176
|
+//
|
|
177
|
+
|
|
178
|
+bool MainGameWindow::validMaze() {
|
|
179
|
+ char c;
|
|
180
|
+ uint ctr = 0;
|
|
181
|
+ _cols = 0; _rows = 0;
|
|
182
|
+ for (int i = 0; i < _maze.length(); i++){
|
|
183
|
+ c = _maze.toStdString()[i];
|
|
184
|
+ if (c == 'x' || c == ' ') ctr++;
|
|
185
|
+ else if (c == '\n') {
|
|
186
|
+ if (_cols == 0) _cols = ctr;
|
|
187
|
+ else if (_cols != ctr) return false;
|
|
188
|
+ ctr = 0;
|
|
189
|
+ _rows++;
|
|
190
|
+ }
|
|
191
|
+ }
|
|
192
|
+ qDebug() << "rows:" << _rows << " cols: " << _cols;
|
|
193
|
+ return true;
|
|
194
|
+}
|
|
195
|
+
|
|
196
|
+//
|
|
197
|
+// This is a delay that lets other events in the event loop
|
|
198
|
+// continue executing.
|
|
199
|
+//
|
|
200
|
+
|
|
201
|
+void delay(int ms)
|
|
202
|
+{
|
|
203
|
+ QTime dieTime= QTime::currentTime().addMSecs(ms);
|
|
204
|
+ while( QTime::currentTime() < dieTime )
|
|
205
|
+ QApplication::processEvents(QEventLoop::AllEvents,10);
|
|
206
|
+}
|
|
207
|
+
|
|
208
|
+//
|
|
209
|
+// Determines the underlying character of the maze.
|
|
210
|
+// This is used to determine if the robot is off the grid
|
|
211
|
+// or has entered a wall.
|
|
212
|
+//
|
|
213
|
+
|
|
214
|
+char MainGameWindow::posToChar() {
|
|
215
|
+ uint r, c;
|
|
216
|
+
|
|
217
|
+ posToRC(r, c);
|
|
218
|
+
|
|
219
|
+ if (r >= _rows || c >= _cols) return -1;
|
|
220
|
+ return _maze.toStdString()[r * (_cols + 1) + c];
|
|
221
|
+
|
|
222
|
+}
|
|
223
|
+
|
|
224
|
+//
|
|
225
|
+// Determines the poisition in row, column, based on the x, y position
|
|
226
|
+// of the robot.
|
|
227
|
+//
|
|
228
|
+
|
|
229
|
+void MainGameWindow::posToRC(uint &r, uint &c) {
|
|
230
|
+ uint realX = robot->myX - s->width() * pct_margin;
|
|
231
|
+ uint realY = robot->myY - s->height() * pct_margin ;
|
|
232
|
+
|
|
233
|
+ r = round(static_cast<float>(realY) / rowHeight);
|
|
234
|
+ c = round(static_cast<float>(realX) / colWidth);
|
|
235
|
+}
|
|
236
|
+
|
|
237
|
+//
|
|
238
|
+// Given the direction determines if the robot may move in that
|
|
239
|
+// direction.
|
|
240
|
+//
|
|
241
|
+
|
|
242
|
+bool MainGameWindow::canMove(char dir) {
|
|
243
|
+
|
|
244
|
+ uint r, c;
|
|
245
|
+
|
|
246
|
+ posToRC(r, c);
|
|
247
|
+
|
|
248
|
+ switch(dir) {
|
|
249
|
+ case 'W': c--; break;
|
|
250
|
+ case 'E': c++; break;
|
|
251
|
+ case 'N': r--; break;
|
|
252
|
+ case 'S': r++; break;
|
|
253
|
+ default:
|
|
254
|
+ display("Bad direction: " + QString(dir));
|
|
255
|
+ return false;
|
|
256
|
+ }
|
|
257
|
+
|
|
258
|
+ if (r >= _rows || c >= _cols) return false;
|
|
259
|
+ else return _maze.toStdString()[r * (_cols + 1) + c] == 'x';
|
|
260
|
+}
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+//
|
|
264
|
+// Given a character 'N', 'S', 'E', or 'W', moves the robot
|
|
265
|
+// one cell in that direction. If the target cell was invalid
|
|
266
|
+// then it destroys the robot.
|
|
267
|
+//
|
|
268
|
+
|
|
269
|
+void MainGameWindow::moveRobot(char dir) {
|
|
270
|
+ if (robot->isAlive()) {
|
|
271
|
+ uint deltaX = colWidth / 10;
|
|
272
|
+ uint deltaY = rowHeight / 10;
|
|
273
|
+
|
|
274
|
+ qDebug() << "car at:" << robot->myX;
|
|
275
|
+ switch(dir) {
|
|
276
|
+ case 'N': robot->toY = robot->myY - rowHeight; robot->myDirY = -deltaY; break;
|
|
277
|
+ case 'S': robot->toY = robot->myY + rowHeight; robot->myDirY = +deltaY; break;
|
|
278
|
+ case 'E': robot->toX = robot->myX + colWidth; robot->myDirX = +deltaX; break;
|
|
279
|
+ case 'W': robot->toX = robot->myX - colWidth; robot->myDirX = -deltaX; break;
|
|
280
|
+ default: display("Bad direction: " + QString(dir)); return;
|
|
281
|
+ }
|
|
282
|
+
|
|
283
|
+ qDebug() <<"colwidth" << colWidth;
|
|
284
|
+ qDebug() << "move to at:" << robot->toX;
|
|
285
|
+
|
|
286
|
+ while (robot->myDirX != 0 || robot->myDirY != 0)
|
|
287
|
+ delay(500);
|
|
288
|
+ robot->myDirX = robot->myDirY = 0;
|
|
289
|
+
|
|
290
|
+ if (posToChar() != 'x')
|
|
291
|
+ robot->kill();
|
|
292
|
+ }
|
|
293
|
+}
|
|
294
|
+
|
|
295
|
+//
|
|
296
|
+// Given a string, displays it in large letters.
|
|
297
|
+//
|
|
298
|
+
|
|
299
|
+void MainGameWindow::display(const QString &st) {
|
|
300
|
+ if (robot->isAlive()) {
|
|
301
|
+ msg->setHtml("<center>" + st + "</center>");
|
|
302
|
+ msg->show();
|
|
303
|
+ delay(500);
|
|
304
|
+ msg->hide();
|
|
305
|
+ }
|
|
306
|
+}
|
|
307
|
+
|
|
308
|
+//
|
|
309
|
+// Given an int, displays it in big letters.
|
|
310
|
+//
|
|
311
|
+
|
|
312
|
+void MainGameWindow::display(int n) {
|
|
313
|
+ display(QString::number(n));
|
|
314
|
+}
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+// Paints the maze based on the contents of the _maze string.
|
|
318
|
+// Also creates the robot and the text box for display.
|
|
319
|
+
|
|
320
|
+void MainGameWindow::paintMaze(uint initialX, uint initialY) {
|
|
321
|
+
|
|
322
|
+ pct_margin = 1.0/( _cols > _rows ? _cols + 2 : _rows + 2);
|
|
323
|
+
|
|
324
|
+ qDebug() << "pct_margin " << pct_margin;
|
|
325
|
+ uint fromY = s->height()*pct_margin, toY = s->height()*(1-pct_margin);
|
|
326
|
+
|
|
327
|
+ qDebug() << "toX, toY" << fromY << "," << toY ;
|
|
328
|
+ uint fromX = s->width()*pct_margin, toX = s->width()*(1-pct_margin);
|
|
329
|
+
|
|
330
|
+ rowHeight = (toY - fromY) / _rows;
|
|
331
|
+ colWidth = (toX - fromX) / _cols;
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+ uint strPos = 0;
|
|
335
|
+ QBrush black(Qt::black);
|
|
336
|
+ QBrush white(Qt::white);
|
|
337
|
+
|
|
338
|
+ for (uint i = 0; i < _rows; i++) {
|
|
339
|
+ uint y = i * rowHeight + s->height()*pct_margin;
|
|
340
|
+ for (uint j = 0; j < _cols; j++) {
|
|
341
|
+ uint x = j * colWidth + s->width() * pct_margin;
|
|
342
|
+ qDebug() << x << " " << y;
|
|
343
|
+ s->addRect(x, y, colWidth, rowHeight, QPen(Qt::SolidLine),
|
|
344
|
+ _maze.at(strPos) == 'x' ? white : black);
|
|
345
|
+ strPos++;
|
|
346
|
+ }
|
|
347
|
+ strPos++;
|
|
348
|
+ }
|
|
349
|
+
|
|
350
|
+ // // Create the robot
|
|
351
|
+ //
|
|
352
|
+ // int initialX, initialY;
|
|
353
|
+ //
|
|
354
|
+ // // throw the dice to determine initial random position
|
|
355
|
+ // do {
|
|
356
|
+ // initialX = rand() % _cols;
|
|
357
|
+ // initialY = rand() % _rows;
|
|
358
|
+ //
|
|
359
|
+ // } while (_maze.toStdString()[initialY * (_cols + 1) + initialX] != 'x');
|
|
360
|
+
|
|
361
|
+ robot = new GenericScrollingObject(":robot.png", s,
|
|
362
|
+ colWidth * initialX + s->width() * pct_margin,
|
|
363
|
+ rowHeight * initialY + s->height() * pct_margin, colWidth,rowHeight,
|
|
364
|
+ QColor(Qt::white), this);
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+ // Create the text box that will be used for the display
|
|
368
|
+
|
|
369
|
+ msg = new QGraphicsTextItem;
|
|
370
|
+ msg->setPos(0, s->height()/2);
|
|
371
|
+ msg->setTextWidth(s->width());
|
|
372
|
+ msg->setHtml("<center>Barev</center>");
|
|
373
|
+ msg->setFont(QFont("Helvetica",90));
|
|
374
|
+ msg->setDefaultTextColor(Qt::green);
|
|
375
|
+ msg->hide();
|
|
376
|
+ s->addItem(msg);
|
|
377
|
+
|
|
378
|
+}
|