@@ -23,14 +23,46 @@ maze_t *init_maze(size_t cols, size_t rows, int cell_size) {
23
23
cell -> bottom = true;
24
24
cell -> left = true;
25
25
cell -> visited = false;
26
+ cell -> current = false;
26
27
}
27
28
}
28
29
29
30
return maze ;
30
31
}
31
32
33
+ stack_t * init_stack (int capacity ) {
34
+ stack_t * stack = malloc (sizeof (stack_t ));
35
+ stack -> top = -1 ;
36
+ stack -> capacity = capacity ;
37
+ stack -> items = malloc (sizeof (cell_t ) * capacity );
38
+
39
+ return stack ;
40
+ }
41
+
42
+ bool is_empty (stack_t * stack ) {
43
+ return stack -> top == -1 ;
44
+ }
45
+
46
+ void push (stack_t * stack , cell_t * cell ) {
47
+ if (stack -> top == stack -> capacity * -1 ) {
48
+ printf ("ERROR - Stack overflow\n" );
49
+ exit (EXIT_FAILURE );
50
+ }
51
+
52
+ stack -> items [++ stack -> top ] = cell ;
53
+ }
54
+
55
+ cell_t * pop (stack_t * stack ) {
56
+ if (is_empty (stack )) {
57
+ printf ("ERROR - Stack underflow\n" );
58
+ exit (EXIT_FAILURE );
59
+ }
60
+
61
+ return stack -> items [stack -> top -- ];
62
+ }
63
+
32
64
cell_t * cell_at (maze_t * maze , int x , int y ) {
33
- if (x < 0 || y < 0 || x > maze -> cols - 1 || x > maze -> rows - 1 )
65
+ if (x < 0 || y < 0 || x > maze -> cols - 1 || y > maze -> rows - 1 )
34
66
return NULL ;
35
67
return maze -> cells + maze -> cols * y + x ;
36
68
}
@@ -60,12 +92,12 @@ void remove_walls(cell_t *cell_a, cell_t *cell_b) {
60
92
}
61
93
}
62
94
63
- cell_t * check_neighbors (maze_t * maze , cell_t * cell , int count ) {
64
- cell_t * start = cell_at (maze , 0 , 0 );
65
- printf ("x: %i, y: %i, top: %i, right: %i, bottom: %i, left: %i, visited: %i\n" , start -> x , start -> y , start -> top , start -> right , start -> bottom , start -> left , start -> visited );
95
+ cell_t * check_neighbors (maze_t * maze , stack_t * visited , cell_t * cell , int count ) {
66
96
int x = cell -> x ;
67
97
int y = cell -> y ;
68
98
99
+ cell -> current = false;
100
+
69
101
cell_t * neighbors [4 ] = { cell_at (maze , x , y - 1 ),
70
102
cell_at (maze , x + 1 , y ),
71
103
cell_at (maze , x , y + 1 ),
@@ -75,30 +107,47 @@ cell_t *check_neighbors(maze_t *maze, cell_t *cell, int count) {
75
107
76
108
for (int i = 0 ; i < 4 ; i ++ ) {
77
109
cell_t * random_neighbor = neighbors [(random_num + i ) % 4 ];
110
+ if (random_neighbor )
78
111
if (random_neighbor && !random_neighbor -> visited ) {
79
- printf ("\n\nCOUNT: %i\ncell->x: %i, cell->y: %i, visited: %i\nneighbor->x: %i, neighbor->y: %i, visited: %i\n" , count , cell -> x , cell -> y , cell -> visited , random_neighbor -> x , random_neighbor -> y , random_neighbor -> visited );
112
+ push (visited , cell );
113
+
80
114
random_neighbor -> visited = true;
115
+ random_neighbor -> current = true;
116
+
81
117
remove_walls (cell , random_neighbor );
82
118
maze_to_img (maze , count , 5 );
83
119
84
- check_neighbors (maze , random_neighbor , ++ count );
120
+ check_neighbors (maze , visited , random_neighbor , ++ count );
85
121
return random_neighbor ;
86
122
}
87
123
}
88
124
125
+ if (!is_empty (visited )) {
126
+ cell = pop (visited );
127
+ cell -> current = true;
128
+ maze_to_img (maze , count , 5 );
129
+
130
+ check_neighbors (maze , visited , cell , ++ count );
131
+ }
132
+
89
133
return NULL ;
90
134
}
91
135
92
136
maze_t * create_maze (maze_t * maze , int x , int y ) {
93
137
cell_t * start = cell_at (maze , 0 , 0 );
94
- printf ("x: %i, y: %i, top: %i, right: %i, bottom: %i, left: %i, visited: %i\n" , start -> x , start -> y , start -> top , start -> right , start -> bottom , start -> left , start -> visited );
95
138
start -> visited = true;
139
+
140
+ stack_t * visited = init_stack (maze -> cols * maze -> rows );
141
+
96
142
srand (time (NULL ));
97
143
98
- printf ("x: %i, y: %i, top: %i, right: %i, bottom: %i, left: %i, visited: %i\n" , start -> x , start -> y , start -> top , start -> right , start -> bottom , start -> left , start -> visited );
99
- check_neighbors (maze , start , 0 );
144
+ check_neighbors (maze , visited , start , 0 );
145
+
146
+ free (visited -> items );
147
+ free (visited );
100
148
101
- system ("ffmpeg -i maze_%d.png -vcodec libx265 -crf 28 -filter:v 'setpts=3.0*PTS' video.mp4 >null 2>null" );
149
+ system ("ffmpeg -i maze_%d.png -vcodec libx265 -crf 28 -filter:v 'setpts=6.0*PTS' video_slow.mp4 >/dev/null 2>/dev/null" );
150
+ system ("ffmpeg -i maze_%d.png -vcodec libx265 -crf 28 -filter:v 'setpts=2.0*PTS' video_fast.mp4 >/dev/null 2>/dev/null" );
102
151
103
152
return maze ;
104
153
}
@@ -142,6 +191,11 @@ int maze_to_img(maze_t *maze, int count, int scale) {
142
191
pixel -> red = 255 ;
143
192
pixel -> green = 0 ;
144
193
pixel -> blue = 0 ;
194
+ }
195
+ else if (cell -> current ) {
196
+ pixel -> red = 0 ;
197
+ pixel -> green = 0 ;
198
+ pixel -> blue = 255 ;
145
199
}
146
200
else if (cell -> visited ) {
147
201
pixel -> red = 0 ;
0 commit comments