-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaze_generator_god_mode.h
439 lines (390 loc) · 11.5 KB
/
maze_generator_god_mode.h
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
#ifndef MAZE_GENERATOR_GOD_MODE_H
#define MAZE_GENERATOR_GOD_MODE_H
#include <iostream>
#include <cstdlib>
#include "structs.h"
#include <vector>
#include <conio.h>
#include <fstream>
using namespace std;
int ** initializeMatrix(int, int);
void drawGodMaze(int **&arr, int n, int m);
void drawBorders(int **&arr, int n, int m);
void drawGhostHouse(int **&arr, int n, int m);
void drawTempLine(int **&arr, int n, int m);
void updateStartingPositions(int **&arr, int n, int m, int margin, vector<Coords> &startingPoints);
Coords chooseRandomStartingPoint(vector<Coords> startingPoints);
void drawNxNWall(int **&arr, int i, int j, int n);
void expandWall(int **&arr, int n, int m, Coords point);
bool checkNxNIsEmplty(int **arr, int i, int j, int n);
void expandPoint(int **&arr, int n, int m, Coords point);
void printMatrix(int **arr, int n, int m);
void mirrorMaze(int **&map, int n, int m);
bool canExpandToRight(int **arr, int n, int m, int i, int j);
bool canExpandToDown(int **arr, int n, int m, int i, int j);
void updateCherryPossiblePoints(int **arr, int n, int m, vector<Coords> &points);
void drawRandomCherries(int **&arr, int n, int m);
// functions that are not being used
bool removeDeadends(int **&arr, int n, int m);
vector<char> deadends(int **arr, int i, int j);
bool isASingleDot(int **arr, int n, int m, int i, int j);
void updateCherryPossiblePoints(int **arr, int n, int m, vector<Coords> &points){
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(arr[i][j] == 0){
Coords p;
p.i = i;
p.j = j;
points.push_back(p);
}
}
}
}
void drawRandomCherries(int **&arr, int n, int m){
vector<Coords> points;
for(int i = 0; i < 4; i++){
updateCherryPossiblePoints(arr,n,m,points);
Coords point = chooseRandomStartingPoint(points);
arr[point.i][point.j] = 3;
points.clear();
}
}
void drawTempLine(int **&arr, int n, int m){
int width = m/2+1;
for(int i = 0; i < n; i ++){
arr[i][width] = 1;
}
}
int height = 4;
int width = 4;
void drawGhostHouse(int **&arr, int n, int m){
int midWidth = m/2;
for(int i = n/4; i < (n/4)+ height; i++){
for(int j = (m/2)-width; j < (m/2)+width; j++){
arr[i][j] = 1;
copy(arr,arr+n,arr);
}
}
}
void blankGhostHouse(int **&arr, int n, int m){
int midWidth = m/2;
for(int i = n/4+1; i < (n/4)+ height-1; i++){
for(int j = (m/2)-width+1; j < (m/2)+width-1; j++){
arr[i][j] = -1;
copy(arr,arr+n,arr);
}
}
//putting the door
for(int j = midWidth-2; j < midWidth+2; j++){
arr[n/4][j] = -1;
copy(arr,arr+n,arr);
}
}
Coords chooseRandomStartingPoint(vector<Coords> startingPoints){
srand((unsigned) time(NULL));
Coords res = startingPoints[rand() % startingPoints.size()];
return res;
}
bool checkNxNIsEmplty(int **arr, int i, int j, int n){
for(int di = 0; di < n; di++){
for(int dj = 0; dj<n; dj++){
if(arr[i+di][j+dj] != 0){
return false;
}
}
}
return true;
}
void drawNxNWall(int **&arr, int i, int j, int n){
for(int di = 0; di < n; di++){
for(int dj = 0; dj < n; dj++){
arr[i+1+di][j+1+dj] = 1;
}
}
}
void updateStartingPositions(int **&arr, int n, int m, int margin, vector<Coords> &startingPoints){
startingPoints.clear();
// copy(startingPoints.begin(), startingPoints.end(), startingPoints);
for(int i = 0; i < n-margin; i++){
for(int j = 0; j < m-margin; j++){
if(checkNxNIsEmplty(arr, i, j, margin)){
Coords point;
point.i = i;
point.j = j;
startingPoints.push_back(point);
}
}
}
// cout<<startingPoints.size()<<endl;
}
void expandWall(int **&arr, int n, int m, Coords point){
bool upperWall = false, leftWall = false, rightWall = false, downWall = false;
Coords topLeft = point;
Coords topRight = {point.i, point.j+3};
Coords bottomLeft = {point.i+3, point.j};
int margin = 2;
if(topLeft.j - margin >= 0){
for(int di = 0; di < 4; di++){
if(arr[topLeft.i+di][topLeft.j-margin] == 1 && arr[topLeft.i+di][topLeft.j-1] == 0){
leftWall = true;
break;
}
}
}
if(topRight.j + margin < m){
for(int di = 0; di < 4; di++){
if(arr[topRight.i+di][topRight.j+margin] == 1 && arr[topRight.i+di][topRight.j+1] == 0){
rightWall = true;
break;
}
}
}
if(topLeft.i - margin >= 0){
for(int dj = 0; dj < 4; dj++){
if(arr[topLeft.i-margin][topLeft.j+dj] == 1 && arr[topLeft.i-1][topLeft.j+dj] == 0){
upperWall = true;
break;
}
}
}
if(bottomLeft.i + margin < n){
for(int dj = 0; dj < 4; dj++){
if(arr[bottomLeft.i+margin][bottomLeft.j+dj] == 1 && arr[bottomLeft.i+1][bottomLeft.j+dj] == 0){
downWall = true;
break;
}
}
}
if(leftWall){
arr[topLeft.i + 1][topLeft.j] = 1;
arr[topLeft.i + 2][topLeft.j] = 1;
}
if(rightWall){
arr[topRight.i + 1][topRight.j] = 1;
arr[topRight.i + 2][topRight.j] = 1;
}
if(upperWall){
arr[topRight.i][topRight.j-1] = 1;
arr[topRight.i][topRight.j-2] = 1;
}
if(downWall){
arr[bottomLeft.i][bottomLeft.j+1] = 1;
arr[bottomLeft.i][bottomLeft.j+2] = 1;
}
}
void expandPoint(int **&arr, int n, int m, Coords point){
// expand right
int rightExpand = 1;
while(true){
if(canExpandToRight(arr,n,m,point.i, point.j+rightExpand)){
arr[point.i][point.j+rightExpand] = 1;
rightExpand++;
}
else{
break;
}
}
// expand down
int downExapnd = 1;
while(true){
if(canExpandToDown(arr,n,m,point.i+downExapnd, point.j)){
arr[point.i+downExapnd][point.j] = 1;
downExapnd++;
}
else{
break;
}
}
}
void drawGodMaze(int **&arr, int n, int m){
drawBorders(arr,n,m);
drawGhostHouse(arr,n,m);
drawTempLine(arr,n,m);
vector<Coords> startingPoints;
updateStartingPositions(arr,n,m,4,startingPoints);
while(!startingPoints.empty()){
Coords start = chooseRandomStartingPoint(startingPoints);
drawNxNWall(arr, start.i, start.j, 2);
expandWall(arr,n,m, start);
updateStartingPositions(arr,n,m,4,startingPoints);
// system("clear");
// printMatrix(arr,n,m);
// usleep(50000);
}
for(int i = 1; i < n-1; i++){
for(int j = 1; j < m -1 ; j++){
Coords start;
start.i = i;
start.j = j;
if(checkNxNIsEmplty(arr, i, j, 3)){
arr[start.i+1][start.j+1] = 1;
copy(arr, arr+n, arr);
// system("clear");
// printMatrix(arr,n,m);
// usleep(600000);
Coords newPoint;
newPoint.i = start.i +1;
newPoint.j = start.j +1;
expandPoint(arr,n,m,newPoint);
// system("clear");
// printMatrix(arr,n,m);
// usleep(600000);
}
}
}
blankGhostHouse(arr,n,m);
mirrorMaze(arr,n,m);
drawRandomCherries(arr,n,m);
// system("clear");
// printMatrix(arr,n,m);
}
void drawBorders(int **&arr, int n, int m){
// in the matrix, 1 represents wall block, while 0 represents path
// this function turns every number on the border of the matrix to 1
for(int i = 0; i < n; i++){
arr[i][0] = 1;
arr[i][m-1] = 1;
}
for(int j = 0; j < m; j++){
arr[0][j] = 1;
arr[n-1][j] = 1;
}
}
void printMatrix(int **arr, int n, int m){
int dotCounter; //if there is no dot in the map, you are the winner!
bool flag = 1; //checks if there is pacman in map
for(int i = 0; i < n; i++){
for(int j = 0; j<m;j++){
switch (arr[i][j])
{
case 0:
// path
cout<<".";
dotCounter++;
break;
case 1:
// wall
cout<<"#";
break;
case 2:
// pacman
cout<<"*";
flag = 0;
break;
case -1:
// blank
cout<<" ";
break;
case -2:
// ghost
cout << "@";
break;
case 3:
// cherry
cout << "C";
break;
}
}
cout<<endl;
}
}
bool removeDeadends(int **&arr, int n, int m){
for(int i = 1; i < n -1; i++){
for(int j = 1; j < m-1; j++){
if(deadends(arr, i, j).size() == 3 && arr[i][j] == 0){
vector<char> deadendDirs = deadends(arr,i,j);
srand((unsigned) time(NULL));
char dir = deadendDirs[rand()%deadendDirs.size()];
switch (dir)
{
case 'a':
arr[i][j-1] = 0;
break;
case 'd':
arr[i][j+1] = 0;
break;
case 'w':
arr[i-1][j] = 0;
break;
case 's':
arr[i+1][j] = 0;
break;
}
// usleep(50000);
// system("clear");
// printMatrix(arr,n,m);
}
}
}
}
vector<char> deadends(int **arr, int i, int j){
vector<char> dirs;
if(arr[i+1][j] == 1) dirs.push_back('s');
if(arr[i][j+1] == 1) dirs.push_back('d');
if(arr[i][j-1] == 1) dirs.push_back('a');
if(arr[i-1][j] == 1) dirs.push_back('w');
return dirs;
}
void mirrorMaze(int **&map, int n, int m){
int half = m/2 +1;
// if( m % 2 ==0){
// half = m/2;
// }
for(int i = 0; i < n; i++){
for(int j = 0; j < half; j++){
map[i][m-1-j] = map[i][j];
}
}
}
bool isASingleDot(int **arr, int n, int m, int i, int j){
if(i-1 < 0 || i+1 >= n || j-1 < 0 || j + 1 >= m ){
return false;
}
if(arr[i + 1][j] != 0
&& arr[i - 1][j] != 0
&& arr[i][j + 1] != 0
&& arr[i][j - 1] != 0
&& arr[i - 1][j - 1] != 0
&& arr[i + 1][j - 1] != 0
&& arr[i - 1][j + 1] != 0
&& arr[i + 1][j + 1] != 0
){
return true;
}
return false;
}
bool canExpandToRight(int **arr, int n, int m, int i, int j){
if(arr[i+1][j] == 0
&& arr[i-1][j] == 0
&& arr[i-1][j+1] == 0
&& arr[i+1][j+1] == 0
&& arr[i][j+1] == 0
){
return true;
}
return false;
}
bool canExpandToDown(int **arr, int n, int m, int i, int j){
if(arr[i][j+1] == 0
&& arr[i][j-1] == 0
&& arr[i+1][j-1] == 0
&& arr[i+1][j+1] == 0
&& arr[i+1][j] == 0
){
return true;
}
return false;
}
int ** initializeMatrix(int n, int m){
//creating the matrix
int ** new_arr = new int*[n];
for (int i = 0; i < n; ++i) {
new_arr[i] = new int[m];
for (int j = 0; j < m; ++j) {
new_arr[i][j] = 0;
}
}
drawGodMaze(new_arr,n,m);
return new_arr;
}
#endif