-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeMaker.c
More file actions
105 lines (92 loc) · 2.75 KB
/
MazeMaker.c
File metadata and controls
105 lines (92 loc) · 2.75 KB
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
#include "MazeMaker.h"
#include "common.h"
void CreateMaze( )
{
srand(time(NULL));
int (*arrMap)[MAPPOS_MAXIMUM_X];
arrMap = GAMEDATA.arrMapInfo;
int width = MAPPOS_MAXIMUM_X / 2;
int height = MAPPOS_MAXIMUM_Y / 2;
maze_t maze = (maze_t)calloc(width * height, sizeof(cell_t));
maze_t mp, maze_top;
char paths [4];
int visits, directions;
visits = width * height - 1;
mp = maze;
maze_top = mp + (width * height) - 1;
while (visits) {
directions = 0;
if ((mp - width) >= maze && cell_empty (mp - width))
paths [directions++] = UP;
if (mp < maze_top && ((mp - maze + 1) % width) && cell_empty (mp + 1))
paths [directions++] = RIGHT;
if ((mp + width) <= maze_top && cell_empty (mp + width))
paths [directions++] = DOWN;
if (mp > maze && ((mp - maze) % width) && cell_empty (mp - 1))
paths [directions++] = LEFT;
if (directions) {
visits--;
directions = ((unsigned) rand () % directions);
switch (paths [directions]) {
case UP:
mp->up = TRUE;
(mp -= width)->down = TRUE;
break;
case RIGHT:
mp->right = TRUE;
(++mp)->left = TRUE;
break;
case DOWN:
mp->down = TRUE;
(mp += width)->up = TRUE;
break;
case LEFT:
mp->left = TRUE;
(--mp)->right = TRUE;
break;
default:
break;
}
} else {
do {
if (++mp > maze_top)
mp = maze;
} while (cell_empty (mp));
}
}
mp = maze;
int w, h;
int idx = 0;
mp->up = TRUE;
(mp + (width * height) - 1)->down = TRUE;
for (w = 0; w < MAPPOS_MAXIMUM_X; w++) {
arrMap[0][idx++] = 1; //벽
if ((mp + w)->up)
arrMap[0][idx++] = ((mp + w)->path) ? 1 : 0;
else
arrMap[0][idx++] = 1;
}
arrMap[0][idx++] = 1;
for (h = 0; h < height; h++) {
idx = 0;
for (w = 0; w < width; w++) {
if ((mp + w)->left)
arrMap[(h+1)*2-1][idx++] = ((mp + w)->path && (mp + w - 1)->path) ? 1 : 0;
else
arrMap[(h +1) * 2 - 1][idx++] = 1;
arrMap[(h +1) * 2 - 1][idx++] = ((mp + w)->path) ? 1 : 0;
}
arrMap[(h +1) * 2 - 1][idx++] = 1;
for (idx = 0, w = 0; w < width; w++) {
arrMap[(h +1) * 2][idx++] =1;
if ((mp + w)->down)
arrMap[(h +1) * 2][idx++] = ((mp + w)->path && (h == height - 1 ||
(mp + w + width)->path)) ? 1 : 0;
else
arrMap[(h + 1) * 2][idx++] = 1;
}
arrMap[(h + 1) * 2][idx++] = 1;
mp += width;
}
free(maze);
}