diff options
author | HampusM <hampus@hampusmat.com> | 2022-01-01 13:51:51 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-01-01 13:51:51 +0100 |
commit | 1bed3ac57906b26ef05b25c2bc5c1dca424dba4a (patch) | |
tree | bd445f7800d27112b3c45199c797e8a048b0306b /src/maze.c | |
parent | 31c6239cb2fcb75aa2ec846ce88bd57a631bbd32 (diff) |
refactor: fix memory leaks & general improvements
Diffstat (limited to 'src/maze.c')
-rw-r--r-- | src/maze.c | 85 |
1 files changed, 43 insertions, 42 deletions
@@ -6,10 +6,9 @@ /** * Returns a filled grid. * - * Arguments: - * - width: The width of the new grid - * - height: The height of the new grid - * - fill: A string to fill the new grid with + * @param width The width of the new grid + * @param height The height of the new grid + * @param fill A string to fill the new grid with */ char ***create_grid(int width, int height, char *fill) { @@ -30,15 +29,14 @@ char ***create_grid(int width, int height, char *fill) return grid; } -struct Maze maze_create(struct Dimensions dimens, char *wall) +Maze maze_create(Dimensions dimens, char *wall) { - struct Maze maze; + Maze maze; maze.dimens = dimens; - struct Dimensions full_dimens = { - .width = dimens.width * 2 + 1, - .height = dimens.height * 2 + 1}; + Dimensions full_dimens = {.width = dimens.width * 2 + 1, + .height = dimens.height * 2 + 1}; maze.full_dimens = full_dimens; @@ -47,37 +45,49 @@ struct Maze maze_create(struct Dimensions dimens, char *wall) return maze; } -void get_neighbours(struct Maze maze, struct Position pos, struct Position neighbours[3], int *neighbour_cnt) +void maze_destroy(Maze *maze) { - if (pos.y != 0 && strcmp(maze.grid[pos.y * 2 - 1][pos.x * 2 + 1], " ") != 0) + // Deallocate the memory of the maze's grid + for (int y = 0; y < maze->full_dimens.height; y++) { - struct Position down_neighbour_pos = {.x = pos.x, .y = pos.y - 1}; + free(maze->grid[y]); + } + free(maze->grid); +} + +void get_neighbours(Maze *maze, Position pos, Position neighbours[3], int *neighbour_cnt) +{ + if (pos.y != 0 && strcmp(maze->grid[pos.y * 2 - 1][pos.x * 2 + 1], " ") != 0) + { + Position down_neighbour_pos = {.x = pos.x, .y = pos.y - 1}; neighbours[*neighbour_cnt] = down_neighbour_pos; (*neighbour_cnt)++; } - if (pos.y != maze.dimens.height - 1 && strcmp(maze.grid[(pos.y + 1) * 2 + 1][pos.x * 2 + 1], " ") != 0) + if (pos.y != maze->dimens.height - 1 && + strcmp(maze->grid[(pos.y + 1) * 2 + 1][pos.x * 2 + 1], " ") != 0) { - struct Position up_neighbour_pos = {.x = pos.x, .y = pos.y + 1}; + Position up_neighbour_pos = {.x = pos.x, .y = pos.y + 1}; neighbours[*neighbour_cnt] = up_neighbour_pos; (*neighbour_cnt)++; } - if (pos.x != 0 && strcmp(maze.grid[pos.y * 2 + 1][pos.x * 2 - 1], " ") != 0) + if (pos.x != 0 && strcmp(maze->grid[pos.y * 2 + 1][pos.x * 2 - 1], " ") != 0) { - struct Position left_neighbour_pos = {.x = pos.x - 1, .y = pos.y}; + Position left_neighbour_pos = {.x = pos.x - 1, .y = pos.y}; neighbours[*neighbour_cnt] = left_neighbour_pos; (*neighbour_cnt)++; } - if (pos.x != maze.dimens.width - 1 && strcmp(maze.grid[pos.y * 2 + 1][(pos.x + 1) * 2 + 1], " ") != 0) + if (pos.x != maze->dimens.width - 1 && + strcmp(maze->grid[pos.y * 2 + 1][(pos.x + 1) * 2 + 1], " ") != 0) { - struct Position right_neighbour_pos = {.x = pos.x + 1, .y = pos.y}; + Position right_neighbour_pos = {.x = pos.x + 1, .y = pos.y}; neighbours[*neighbour_cnt] = right_neighbour_pos; (*neighbour_cnt)++; } } -int is_whole_maze_visited(struct Maze maze, int visited_pos_cnt) +int is_whole_maze_visited(Maze *maze, int visited_pos_cnt) { - if (visited_pos_cnt == (maze.dimens.height * maze.dimens.width) - 1) + if (visited_pos_cnt == (maze->dimens.height * maze->dimens.width) - 1) { return 1; } @@ -85,29 +95,20 @@ int is_whole_maze_visited(struct Maze maze, int visited_pos_cnt) return 0; } -/** - * Excavates a maze. - * - * This is what creates the actual maze. - * - * Arguments: - * - maze: The maze to excavate - * - start_pos: Start position - */ -void maze_excavate(struct Maze maze, struct Position start_pos) +void maze_excavate(Maze *maze, Position start_pos) { - struct PositionStack *path = create_pos_stack(maze.dimens.width * maze.dimens.height); + PositionStack *path = pos_stack_create(maze->dimens.width * maze->dimens.height); pos_stack_push(path, start_pos); int visited_pos_cnt = 0; while (1) { - struct Position pos = pos_stack_peek(path); + Position pos = pos_stack_peek(path); - maze.grid[pos.y * 2 + 1][pos.x * 2 + 1] = " "; + maze->grid[pos.y * 2 + 1][pos.x * 2 + 1] = " "; - struct Position neighbours[3] = {}; + Position neighbours[3] = {}; int neighbour_cnt = 0; get_neighbours(maze, pos, neighbours, &neighbour_cnt); @@ -126,22 +127,22 @@ void maze_excavate(struct Maze maze, struct Position start_pos) visited_pos_cnt++; - struct Position next_pos = neighbours[rand() % neighbour_cnt]; + Position next_pos = neighbours[rand() % neighbour_cnt]; - maze.grid[pos.y * 2 - (pos.y - next_pos.y) + 1] - [pos.x * 2 - (pos.x - next_pos.x) + 1] = " "; + maze->grid[pos.y * 2 - (pos.y - next_pos.y) + 1] + [pos.x * 2 - (pos.x - next_pos.x) + 1] = " "; pos_stack_push(path, next_pos); } + + pos_stack_destroy(path); } -void maze_print(struct Maze maze) +void maze_print(Maze maze) { - struct Dimensions full_dimens = maze.full_dimens; - - for (int y = 0; y < full_dimens.height; y++) + for (int y = 0; y < maze.full_dimens.height; y++) { - for (int x = 0; x < full_dimens.width; x++) + for (int x = 0; x < maze.full_dimens.width; x++) { printf("%s", maze.grid[y][x]); } |