-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmazeGenerator.c
More file actions
127 lines (107 loc) · 2.34 KB
/
mazeGenerator.c
File metadata and controls
127 lines (107 loc) · 2.34 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "includes.h"
void initializeMaze(const int width, const int height, Node **nodes)
{
int i, j;
Node *current;
*nodes = calloc(width * height, sizeof(Node));
for (i = 0; i < width; i++)
{
for (j = 0; j < height; j++)
{
current = (*nodes) + i + j * width;
if (i * j % 2)
{
current->x = i;
current->y = j;
current->directionsToExplore = 15;
current->displayCharacter = ' ';
}
else
current->displayCharacter = '#';
current->isVisited = false;
}
}
}
void setExitAndEntrance(const int width, const int height, Node **nodes)
{
(*nodes)[width].displayCharacter = ' ';
(*nodes)[((width) * (height - 1)) - 1].displayCharacter = ' ';
}
Node *linkNodes(Node *current, const int width, const int height, Node **nodes)
{
int x, y, numberOfDirections = 4;
char dir;
Node *dest;
enum directions{right = 1, down = 2, left = 4, up = 8};
if (current == NULL)
return NULL;
while (current->directionsToExplore)
{
dir = (1 << (rand() % numberOfDirections));
if (~current->directionsToExplore & dir)
continue;
current->directionsToExplore &= ~dir;
switch (dir)
{
case right:
if (current->x + 2 < width)
{
x = current->x + 2;
y = current->y;
}
else continue;
break;
case down:
if (current->y + 2 < height)
{
x = current->x;
y = current->y + 2;
}
else continue;
break;
case left:
if (current->x - 2 >= 0)
{
x = current->x - 2;
y = current->y;
}
else continue;
break;
case up:
if (current->y - 2 >= 0)
{
x = current->x;
y = current->y - 2;
}
else continue;
break;
}
dest = (*nodes) + x + y * width;
if (dest->displayCharacter == ' ')
{
if (dest->parent != NULL) continue;
dest->parent = current;
(*nodes)[current->x + (x - current->x) / 2 + (current->y + (y - current->y) / 2) * width].displayCharacter = ' ';
return dest;
}
}
return current->parent;
}
void setupStartNodeForGenerator(Node ** start, Node ** last, Node ** nodes,const int width)
{
*start = *nodes + 1 + width;
(*start)->parent = *start;
*last = *start;
}
void resetLabirynth(int *width, int *height, Node **nodes)
{
free(*nodes);
*nodes = NULL;
*width = 0;
*height = 0;
}
void getValuesFromCommandLineArguments(int *width, int *height, char ***argv)
{
*width = atoi(argv[1]);
*height = atoi(argv[2]);
}