Skip to content

Commit 02bf5f3

Browse files
committed
Finished the program. Still need to optimize and clean it up a bit
1 parent 8104348 commit 02bf5f3

File tree

6 files changed

+82
-68
lines changed

6 files changed

+82
-68
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
bin/
22
obj/
33
*.png
4+
*.mp4

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ $(OBJ)/%.o: $(SRC)/%.c
3333
$(CC) $(CFLAGS) -c $< -o $@
3434

3535
clean:
36-
$(RM) -r $(BINDIR)/* $(OBJ)/* *.png *.mp4
36+
$(RM) -r $(BINDIR)/* $(OBJ)/* *.png *.mp4 null *.txt
3737

3838
valgrind:
3939
-valgrind -s --leak-check=full --keep-debuginfo=yes --track-origins=yes $(BIN)

null

-55
This file was deleted.

src/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
int main ()
88
{
9-
maze_t *maze = init_maze(10, 10, 10);
9+
maze_t *maze = init_maze(100, 100, 1);
1010

1111
maze = create_maze(maze, 0, 0);
1212

src/maze.c

+64-10
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,46 @@ maze_t *init_maze(size_t cols, size_t rows, int cell_size) {
2323
cell->bottom = true;
2424
cell->left = true;
2525
cell->visited = false;
26+
cell->current = false;
2627
}
2728
}
2829

2930
return maze;
3031
}
3132

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+
3264
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)
3466
return NULL;
3567
return maze->cells + maze->cols * y + x;
3668
}
@@ -60,12 +92,12 @@ void remove_walls(cell_t *cell_a, cell_t *cell_b) {
6092
}
6193
}
6294

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) {
6696
int x = cell->x;
6797
int y = cell->y;
6898

99+
cell->current = false;
100+
69101
cell_t *neighbors[4] = { cell_at(maze, x, y - 1),
70102
cell_at(maze, x + 1, y),
71103
cell_at(maze, x, y + 1),
@@ -75,30 +107,47 @@ cell_t *check_neighbors(maze_t *maze, cell_t *cell, int count) {
75107

76108
for (int i = 0; i < 4; i++) {
77109
cell_t *random_neighbor = neighbors[(random_num + i) % 4];
110+
if (random_neighbor)
78111
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+
80114
random_neighbor->visited = true;
115+
random_neighbor->current = true;
116+
81117
remove_walls(cell, random_neighbor);
82118
maze_to_img(maze, count, 5);
83119

84-
check_neighbors(maze, random_neighbor, ++count);
120+
check_neighbors(maze, visited, random_neighbor, ++count);
85121
return random_neighbor;
86122
}
87123
}
88124

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+
89133
return NULL;
90134
}
91135

92136
maze_t *create_maze(maze_t *maze, int x, int y) {
93137
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);
95138
start->visited = true;
139+
140+
stack_t *visited = init_stack(maze->cols * maze->rows);
141+
96142
srand(time(NULL));
97143

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);
100148

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");
102151

103152
return maze;
104153
}
@@ -142,6 +191,11 @@ int maze_to_img(maze_t *maze, int count, int scale) {
142191
pixel->red = 255;
143192
pixel->green = 0;
144193
pixel->blue = 0;
194+
}
195+
else if (cell->current) {
196+
pixel->red = 0;
197+
pixel->green = 0;
198+
pixel->blue = 255;
145199
}
146200
else if (cell->visited) {
147201
pixel->red = 0;

src/maze.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,29 @@ typedef struct {
2323
int rows;
2424
} maze_t;
2525

26+
typedef struct {
27+
int top;
28+
int capacity;
29+
cell_t **items;
30+
} stack_t;
31+
2632
cell_t *cell_at(maze_t *maze, int x, int y);
2733

2834
maze_t *init_maze(size_t cols, size_t rows, int cell_size);
2935

36+
stack_t *init_stack(int capacity);
37+
38+
void push(stack_t *stack, cell_t *cell);
39+
40+
cell_t *pop(stack_t *stack);
41+
42+
bool is_empty(stack_t *stack);
43+
3044
int maze_to_img(maze_t *maze, int count, int scale);
3145

3246
void remove_walls(cell_t *cell_a, cell_t *cell_b);
3347

34-
cell_t *check_neighbors(maze_t *maze, cell_t *cell, int iter);
48+
cell_t *check_neighbors(maze_t *maze, stack_t *visited, cell_t *cell, int iter);
3549

3650
maze_t *create_maze(maze_t *maze, int x, int y);
3751

0 commit comments

Comments
 (0)