-
Notifications
You must be signed in to change notification settings - Fork 1
/
maze.c
150 lines (139 loc) · 3.82 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
138
139
140
141
142
143
144
145
146
147
148
149
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
typedef struct square {
char borderN;
char borderE;
} square;
typedef struct maze {
int width;
int height;
/*
* squares[0] is top left
* squares[1] is 2nd left on the top
* ...
* squares[width*height-2] is 2nd right on the bottom
* squares[width*height-1] is bottom right
*/
square* squares;
} maze;
maze *new_maze(int width, int height) {
maze* maze = malloc(sizeof(maze));
maze->width = width;
maze->height = height;
maze->squares = malloc(width*height*sizeof(square));
return maze;
}
void free_maze(maze *maze) {
free(maze->squares);
free(maze);
}
void print_maze(maze *maze) {
for (int y=0; y < maze->height; y++) {
// top border
for (int x=0; x < maze->width; x++) {
printf("█");
if (maze->squares[maze->width*y +x].borderN) {
printf("█");
} else {
printf(" ");
}
}
printf("█\n");
// East border and blank for square
for (int x=0; x < maze->width; x++) {
if (maze->squares[maze->width*y +x].borderE) {
printf("█");
} else {
printf(" ");
}
printf(" ");
}
printf("█\n");
}
// Very bottom border
for (int x=0; x < maze->width-1; x++) {
printf("██");
}
printf("█ █\n");
}
void remove_maze_boundary(maze *m, int square_a, int square_b) {
if (square_a > square_b) {
int tmp = square_a;
square_a = square_b;
square_b = tmp;
}
if (square_b - square_a == 1) {
m->squares[square_b].borderE = 0;
} else {
m->squares[square_b].borderN = 0;
}
}
void create_passages(maze *m) {
// Set all borders to on
memset(m->squares, 1, m->width*m->height*sizeof(square));
GArray *explored = g_array_sized_new(FALSE, FALSE, sizeof(int), m->width*m->height);
int i =0;
g_array_append_val(explored, i);
enum square_status {EXPLORED=0, UNEXPLORED=1};
char square_statuses[m->width * m->height];
memset(square_statuses, (int)UNEXPLORED, sizeof(square_statuses));
square_statuses[0] = EXPLORED;
m->squares[0].borderN = 0;
while( explored->len ) {
int explored_index = g_random_int_range(0, explored->len);
int explored_cell_index = g_array_index(explored, int, explored_index);
int y = explored_cell_index/m->width;
int x = explored_cell_index - y*m->width;
GArray *bordering_squares = g_array_sized_new(FALSE, FALSE, sizeof(int), 4);
int bordering_square;
if (y>0) {
bordering_square = explored_cell_index - m->width;
if (square_statuses[bordering_square] == UNEXPLORED) {
g_array_append_val(bordering_squares, bordering_square);
}
}
if (x< m->width-1) {
bordering_square = explored_cell_index +1;
if (square_statuses[bordering_square] == UNEXPLORED) {
g_array_append_val(bordering_squares, bordering_square);
}
}
if (y< m->height-1) {
bordering_square = explored_cell_index + m->width;
if (square_statuses[bordering_square] == UNEXPLORED) {
g_array_append_val(bordering_squares, bordering_square);
}
}
if (x > 0) {
bordering_square = explored_cell_index -1;
if (square_statuses[bordering_square] == UNEXPLORED) {
g_array_append_val(bordering_squares, bordering_square);
}
}
if (bordering_squares->len==0) {
g_array_remove_index_fast(explored, explored_index);
} else {
bordering_square = g_array_index(
bordering_squares, int, g_random_int_range(0, bordering_squares->len)
);
remove_maze_boundary(m, bordering_square, explored_cell_index);
g_array_append_val(explored, bordering_square);
square_statuses[bordering_square] = EXPLORED;
}
g_array_free(bordering_squares, TRUE);
}
g_array_free(explored, TRUE);
}
int main(int argc, char **argv) {
int width = (argc < 3) ? 0 : atoi(argv[1]);
int height = (argc < 3) ? 0 : atoi(argv[2]);
if (width < 2) width = 10;
if (height < 2) height = 10;
maze *m = new_maze(width, height);
create_passages(m);
print_maze(m);
free_maze(m);
return 0;
}