-
Notifications
You must be signed in to change notification settings - Fork 1
/
labyrinth2.cpp
546 lines (434 loc) · 13.1 KB
/
labyrinth2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#include<iostream>
#include<ctime>
#include<stack>
#include<stdlib.h>
#include <GL/glut.h> // GLUT, includes glu.h and gl.h
using namespace std;
//------------------------------------------------------------------------------------------------
#define WIDTH 600
#define HEIGHT 600
int keyflag = 0;
const int QUAD_DIMENTION = WIDTH / 37;
const int row = 37;
const int column = 37;
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
#define MOVE 1
#define QUIET 2
//-----------------------------------------------
void display();;
void keyboard(unsigned char c,int x,int y);
void idle();
//-----------------------------------------------
class Player {
float x,y; //-- Current position
float vx,vy; //-- Velocity vector
int state;
long time_remaining;
public:
Player();
void set_position(int x,int y);
void init_movement(int destination_x,int destination_y,int duration);
void integrate(long t);
void draw();
float getPositionX(){return x;}
float getPositionY(){return y;}
};
//-----------------------------------------------
Player::Player()
{
state=QUIET;
}
//-----------------------------------------------
void Player::set_position(int x,int y)
{
this->x = x;
this->y = y;
}
//-----------------------------------------------
void Player::init_movement(int destination_x,int destination_y,int duration)
{
vx = (destination_x - x)/duration;
vy = (destination_y - y)/duration;
state=MOVE;
time_remaining=duration;
}
//-----------------------------------------------
void Player::integrate(long t)
{
if(state==MOVE && t<time_remaining)
{
x = x + vx*t;
y = y + vy*t;
time_remaining-=t;
}
else if(state==MOVE && t>=time_remaining)
{
x = x + vx*time_remaining;
y = y + vy*time_remaining;
state=QUIET;
}
}
//-----------------------------------------------
void Player::draw()
{
glColor3f(1,1,1);
glBegin(GL_QUADS);
glVertex2i(x-6,y-6);
glVertex2i(x+6,y-6);
glVertex2i(x+6,y+6);
glVertex2i(x-6,y+6);
glEnd();
}
//-----------------------------------------------
// -- MAIN
//-----------------------------------------------
Player square;
long last_t=0;
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
enum SquareType { Corridor, Wall, Start };
class Square {
SquareType type;
char display; //what will square display 0 for wall, ' '(space) for corridor, S for start point
//variables of all possible neighbours of current square
bool neighbour_top;
bool neighbour_left;
bool neighbour_bottom;
bool neighbour_right;
public:
bool visited; //variable if square has been visited
Square() {
type = Wall;
visited = false;
}
//change the state
void changeType(SquareType t) { type = t; }
void SquareVisited(bool v) { visited = v; }
void changeDisplay(char d) { display = d; }
void changeNeighbourTop(bool a) { neighbour_top = a; }
void changeNeighbourLeft(bool a) { neighbour_left = a; }
void changeNeighbourRight(bool a) { neighbour_right = a; }
void changeNeighbourBottom(bool a) { neighbour_bottom = a; }
//Getters
SquareType getType() { return type; }
char returnDisplay() { return display; }
//bool isVisited() { return visited; }
bool availableNeighbourTop() { return neighbour_top; }
bool availableNeighbourBottom() { return neighbour_bottom; }
bool availableNeighbourLeft() { return neighbour_left; }
bool availableNeighbourRight() { return neighbour_right; }
};
void drawQuad(int i, int j, int width, int height);
class Maze {
//Square **matrix;
Square matrix[row][column];
//int row;
//int column;
public:
Maze(int r, int c) {
//setting all matrix to be a wall
for (int i = 0; i<r; i++) {
for (int j = 0; j<c; j++) {
matrix[i][j].changeDisplay('0');
matrix[i][j].SquareVisited(false);
matrix[i][j].changeNeighbourTop(true);;
matrix[i][j].changeNeighbourBottom(true);
matrix[i][j].changeNeighbourLeft(true);
matrix[i][j].changeNeighbourRight(true);
}
}
for (int i = 1; i<r - 1; i++) {
matrix[i][0].SquareVisited(true);
matrix[i][c - 1].SquareVisited(true);
matrix[i][1].SquareVisited(true);
matrix[i][c - 2].SquareVisited(true);
for (int j = 1; j<c - 1; j++) {
// Border Cells have fewer accessible walls
matrix[1][j].changeNeighbourTop(false);
matrix[r - 2][j].changeNeighbourBottom(false);
matrix[i][1].changeNeighbourLeft(false);
matrix[i][c - 2].changeNeighbourRight(false);
matrix[0][j].SquareVisited(true);
matrix[r - 1][j].SquareVisited(true);
matrix[1][j].SquareVisited(true);
matrix[r - 2][j].SquareVisited(true);
}
}
}
int getRow() { return row; }
int getColumn() { return column; }
void Redraw() {
for (int i = 0; i<row; i++) {
cout << endl;
matrix[i][0].changeDisplay(' ');
matrix[i][column - 1].changeDisplay(' ');
for (int j = 0; j < column; j++) {
cout << " " << matrix[i][j].returnDisplay();
matrix[0][j].changeDisplay(' ');
matrix[row - 1][j].changeDisplay(' ');
}
}
cout << endl;
int counterR;
int counterC;
for (size_t i = 0; i < row; i++)
{
for (size_t i = 0; i < column; i++)
{
}
}
}
void GraficDraw() {
for (size_t i = 0; i < row; i++)
{
for (size_t j = 0; j < column; j++)
{
if (matrix[i][j].returnDisplay() == '0') {
glColor3f(0, 0, 1);
drawQuad(j, i, QUAD_DIMENTION, QUAD_DIMENTION);
}
else if (matrix[i][j].returnDisplay() == ' ') {
glColor3f(0, 1, 0);
drawQuad(j, i, QUAD_DIMENTION, QUAD_DIMENTION);
}
else if (matrix[i][j].returnDisplay() == 'S') {
glColor3f(1, 0, 0);
drawQuad(j, i, QUAD_DIMENTION, QUAD_DIMENTION);
}
}
}
glutPostRedisplay();
}
void makeStartPositions() {
for (int i = row - 1; i > 0; i--)
{
for (int j = 0; j < column; j++)
{
if (matrix[i][j].returnDisplay() == ' ') {
matrix[i][j].changeType(Start);
matrix[i][j].changeDisplay('S');
return;
}
}
}
}
void makeEndPositions() {
for (int i = 0; i < row; i++)
{
for (int j = column; j > 0; j--)
{
if (matrix[i][j].returnDisplay() == ' ') {
matrix[i][j].changeType(Start);
matrix[i][j].changeDisplay('S');
return;
}
}
}
}
void make() {
srand(time(NULL));
int randX;
int randY;
do
{
randX = ((2 * rand()) + 1) % (column - 1);
} while (!(randX > 3 && randX < column - 4));
do
{
randY = ((2 * rand()) + 1) % (row - 1);
} while (!(randY > 3 && randY < row - 4));
int visitedX = randX;
int visitedY = randY;
int visitedCells = 1;
int totalCells = 0;
int brojac = 0;
/* if(visitedX % 2 == 0 && visitedY % 2 == 0)
totalCells = ((row - 3) / 2)*((column - 3) / 2);
else if (visitedX % 2 == 1 && visitedY % 2 == 0)
totalCells = ((row - 4) / 2)*((column - 3) / 2);
else if (visitedX % 2 == 0 && visitedY % 2 == 1)
totalCells = ((row - 3) / 2)*((column - 4) / 2);
else if (visitedX % 2 == 1 && visitedY % 2 == 1)
totalCells = ((row - 4) / 2)*((column - 4) / 2);*/
stack<int> back_trackX, back_trackY;
matrix[randY][randX].changeDisplay(' ');
matrix[randY][randX].SquareVisited(true);
do {
if (((matrix[randY - 2][randX].visited == false) && (matrix[randY][randX].availableNeighbourTop() == true) && (matrix[randY - 2][randX].availableNeighbourBottom() == true)) ||
((matrix[randY + 2][randX].visited == false) && (matrix[randY + 2][randX].availableNeighbourTop() == true) && (matrix[randY][randX].availableNeighbourBottom() == true)) ||
((matrix[randY][randX - 2].visited == false) && (matrix[randY][randX].availableNeighbourLeft() == true) && (matrix[randY][randX - 2].availableNeighbourRight() == true)) ||
((matrix[randY][randX + 2].visited == false) && (matrix[randY][randX + 2].availableNeighbourLeft() == true) && (matrix[randY][randX].availableNeighbourRight() == true))) {
int random = rand() % 4 + 1;
if ((random == 1) && (randY > 1)) {
if (matrix[randY - 2][randX].visited == false) {
matrix[randY - 1][randX].changeDisplay(' ');
matrix[randY - 1][randX].SquareVisited(true);
matrix[randY][randX].changeNeighbourTop(false);
back_trackX.push(randX);
back_trackY.push(randY);
randY -= 2;
matrix[randY][randX].changeDisplay(' ');
matrix[randY][randX].SquareVisited(true);
matrix[randY][randX].changeNeighbourBottom(false);
visitedCells++;
}
else
continue;
}
if ((random == 2) && (randY < row - 2)) {
if (matrix[randY + 2][randX].visited == false) {
matrix[randY + 1][randX].changeDisplay(' ');
matrix[randY + 1][randX].SquareVisited(true);
matrix[randY][randX].changeNeighbourBottom(false);
back_trackX.push(randX);
back_trackY.push(randY);
randY += 2;
matrix[randY][randX].changeDisplay(' ');
matrix[randY][randX].SquareVisited(true);
matrix[randY][randX].changeNeighbourTop(false);
visitedCells++;
}
else
continue;
}
if ((random == 3) && (randX > 1)) {
if (matrix[randY][randX - 2].visited == false) {
matrix[randY][randX - 1].changeDisplay(' ');
matrix[randY][randX - 1].SquareVisited(true);
matrix[randY][randX].changeNeighbourLeft(false);
back_trackX.push(randX);
back_trackY.push(randY);
randX -= 2;
matrix[randY][randX].changeDisplay(' ');
matrix[randY][randX].SquareVisited(true);
matrix[randY][randX].changeNeighbourRight(false);
visitedCells++;
}
else
continue;
}
if ((random == 4) && (randX < column - 2)) {
if (matrix[randY][randX + 2].visited == false) {
matrix[randY][randX + 1].changeDisplay(' ');
matrix[randY][randX + 1].SquareVisited(true);
matrix[randY][randX].changeNeighbourRight(false);
back_trackX.push(randX);
back_trackY.push(randY);
randX += 2;
matrix[randY][randX].changeDisplay(' ');
matrix[randY][randX].SquareVisited(true);
matrix[randY][randX].changeNeighbourLeft(false);
visitedCells++;
}
else
continue;
}
}
else {
randX = back_trackX.top();
back_trackX.pop();
randY = back_trackY.top();
back_trackY.pop();
brojac++;
}
} while (!back_trackX.empty());
}
};
Maze m(37, 37);
void display()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
//m.make();
// m.makeStartPositions();
// m.makeEndPositions();
m.GraficDraw();
square.draw();
glutSwapBuffers();
}
float xx = 55;
float yy = 536;
void keyboard(unsigned char c, int x, int y)
{
switch (c){
case 'w':
square.init_movement(xx,yy+10,200);
square.set_position(xx,yy);
yy +=10;
break;
case 'a':
square.init_movement(xx-10,yy,200);
square.set_position(xx,yy);
xx -=10;
break;
case 's':
square.init_movement(xx,yy-10,200);
square.set_position(xx,yy);
yy -=10;
break;
case 'd':
square.init_movement(xx+10,yy,200);
square.set_position(xx,yy);
xx +=10;
break;
case 'r':
m = Maze(m.getRow(), m.getColumn());
m.make();
m.makeStartPositions();
m.makeEndPositions();
break;
default:
break;
}
glutPostRedisplay();
};
void drawQuad(int i, int j, int width, int height) {
glBegin(GL_QUADS);
glVertex2i(i*width, j*height);
glVertex2i((i + 1)*width, j*height);
glVertex2i((i + 1)*width, (j + 1)*height);
glVertex2i(i*width, (j + 1)*height);
glEnd();
}
int main(int argc, char *argv[]) {
srand(time(NULL));
square.set_position(55,536);
m.make();
m.makeStartPositions();
m.makeEndPositions();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(50, 500);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Maze-game");
glutKeyboardFunc(keyboard);
glutIdleFunc(idle);
glutDisplayFunc(display);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, WIDTH - 1, 0, HEIGHT - 1);
glutMainLoop();
//system("pause");
return 0;
}
void idle()
{
long t;
t=glutGet(GLUT_ELAPSED_TIME);
if(last_t==0)
last_t=t;
else
{
square.integrate(t-last_t);
last_t=t;
}
glutPostRedisplay();
}