-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.c
137 lines (117 loc) · 3 KB
/
maze.c
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
#include "maze.h"
#include "time.h"
void maze_init(maze *m, int maze_s) {
m->maze_size = maze_s;
m->cells = malloc(sizeof(cell) * maze_s * maze_s);
cell c;
for (int i = 0; i < (m->maze_size * m->maze_size); i++) {
cell_init(&c);
c.x = i % (m->maze_size);
c.y = i / (m->maze_size);
c.index = i;
m->cells[i] = c;
}
for (int i = 0; i < 4; i++) {
m->neighbor_list[i] = -1;
};
srand(time(NULL));
}
bool top_neighbor(cell *c, int *storage, maze *m) {
if ((c->y) > 0) {
*storage = (((c->y) - 1) * m->maze_size) + ((c->x));
return true;
}
return false;
};
bool bottom_neighbor(cell *c, int *storage, maze *m) {
if ((c->y) < (m->maze_size - 1)) {
*storage = (((c->y) + 1) * m->maze_size) + ((c->x));
return true;
}
return false;
};
bool left_neighbor(cell *c, int *storage, maze *m) {
if ((c->x) > 0) {
*storage = (((c->y)) * m->maze_size) + ((c->x) - 1);
return true;
}
return false;
};
bool right_neighbor(cell *c, int *storage, maze *m) {
if ((c->x) < (m->maze_size - 1)) {
*storage = (((c->y)) * m->maze_size) + ((c->x) + 1);
return true;
}
return false;
};
bool has_neighbor(cell *c, maze *m) {
bool has_neighbor = false;
int index;
if (top_neighbor(c, &index, m)) {
if (!(m->cells[index].visited)) {
m->neighbor_list[0] = index;
has_neighbor = true;
}
};
if (bottom_neighbor(c, &index, m)) {
if (!(m->cells[index].visited)) {
m->neighbor_list[1] = index;
has_neighbor = true;
}
};
if (left_neighbor(c, &index, m)) {
if (!(m->cells[index].visited)) {
m->neighbor_list[2] = index;
has_neighbor = true;
}
};
if (right_neighbor(c, &index, m)) {
if (!(m->cells[index].visited)) {
m->neighbor_list[3] = index;
has_neighbor = true;
}
};
return has_neighbor;
}
int random_neighbor(maze *m) {
int i;
while (true) {
int r = rand() % 4;
if (m->neighbor_list[r] != -1) {
i = m->neighbor_list[r];
break;
};
};
clear_neighbor_list(m);
return i;
}
void clear_neighbor_list(maze *m) {
for (int i = 0; i < 4; i++) {
m->neighbor_list[i] = -1;
}
}
void break_wall(maze *m, int *c_index, int *t_index) {
if ((*c_index - m->maze_size) == *t_index) {
m->cells[*c_index].top_wall = false;
m->cells[*t_index].bottom_wall = false;
};
if ((*c_index + m->maze_size) == *t_index) {
m->cells[*c_index].bottom_wall = false;
m->cells[*t_index].top_wall = false;
};
if ((*c_index - 1) == *t_index) {
m->cells[*c_index].left_wall = false;
m->cells[*t_index].right_wall = false;
};
if ((*c_index + 1) == *t_index) {
m->cells[*c_index].right_wall = false;
m->cells[*t_index].left_wall = false;
};
}
void print_maze(maze *m) {
for (int i = 0; i < (m->maze_size * m->maze_size); i++) {
cell c = m->cells[i];
print_cell(&c);
puts("******************");
}
}