aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/maze.c72
-rw-r--r--src/maze.h11
-rw-r--r--src/mazerator.c9
-rw-r--r--src/position_stack.c6
-rw-r--r--src/position_stack.h4
5 files changed, 60 insertions, 42 deletions
diff --git a/src/maze.c b/src/maze.c
index 9830098..d2d7202 100644
--- a/src/maze.c
+++ b/src/maze.c
@@ -4,39 +4,48 @@
#include <string.h>
/**
- * Returns a filled maze grid.
+ * Returns a filled grid.
*
* Arguments:
- * - width: The width of the new maze
- * - height: The height of the new maze
- * - wall: The wall to fill the new maze with
+ * - width: The width of the new grid
+ * - height: The height of the new grid
+ * - fill: A string to fill the new grid with
*/
-char ***create_maze_grid(int width, int height, char *wall)
+char ***create_grid(int width, int height, char *fill)
{
- char ***maze;
+ char ***grid;
- // Fill the maze with walls
- maze = malloc(height * sizeof(char **));
+ // Fill the grid
+ grid = malloc(height * sizeof(char **));
for (int y = 0; y < height; y++)
{
- maze[y] = malloc(width * sizeof(char *));
+ grid[y] = malloc(width * sizeof(char *));
for (int x = 0; x < width; x++)
{
- maze[y][x] = wall;
+ grid[y][x] = fill;
}
}
- return maze;
+ return grid;
}
-struct Maze maze_create(struct MazeSize size, char *wall)
+struct Maze maze_create(struct Dimensions dimens, char *wall)
{
- struct Maze new_maze;
- new_maze.grid = create_maze_grid(size.width * 2 + 1, size.height * 2 + 1, wall);
- new_maze.size = size;
+ struct Maze maze;
+
+ maze.dimens = dimens;
+
+ struct Dimensions full_dimens = {
+ .width = dimens.width * 2 + 1,
+ .height = dimens.height * 2 + 1
+ };
+
+ maze.full_dimens = full_dimens;
- return new_maze;
+ maze.grid = create_grid(full_dimens.width, full_dimens.height, wall);
+
+ return maze;
}
void get_neighbours(struct Maze maze, struct Position pos, struct Position neighbours[3], int *neighbour_cnt)
@@ -47,7 +56,7 @@ void get_neighbours(struct Maze maze, struct Position pos, struct Position neigh
neighbours[*neighbour_cnt] = down_neighbour_pos;
(*neighbour_cnt)++;
}
- if (pos.y != maze.size.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};
neighbours[*neighbour_cnt] = up_neighbour_pos;
@@ -59,7 +68,7 @@ void get_neighbours(struct Maze maze, struct Position pos, struct Position neigh
neighbours[*neighbour_cnt] = left_neighbour_pos;
(*neighbour_cnt)++;
}
- if (pos.x != maze.size.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};
neighbours[*neighbour_cnt] = right_neighbour_pos;
@@ -67,6 +76,16 @@ void get_neighbours(struct Maze maze, struct Position pos, struct Position neigh
}
}
+int is_whole_maze_visited(struct Maze maze, int visited_pos_cnt)
+{
+ if (visited_pos_cnt == (maze.dimens.height * maze.dimens.width) - 1)
+ {
+ return 1;
+ }
+
+ return 0;
+}
+
/**
* Excavates a maze.
*
@@ -78,7 +97,7 @@ void get_neighbours(struct Maze maze, struct Position pos, struct Position neigh
*/
void maze_excavate(struct Maze maze, struct Position start_pos)
{
- struct PositionStack *path = create_pos_stack(maze.size.width * maze.size.height);
+ struct PositionStack *path = create_pos_stack(maze.dimens.width * maze.dimens.height);
pos_stack_push(path, start_pos);
@@ -96,9 +115,8 @@ void maze_excavate(struct Maze maze, struct Position start_pos)
if (neighbour_cnt == 0)
{
- if (visited_pos_cnt == (maze.size.height * maze.size.width) - 1)
+ if (is_whole_maze_visited(maze, visited_pos_cnt))
{
- // The whole maze have been visited
break;
}
@@ -118,14 +136,16 @@ void maze_excavate(struct Maze maze, struct Position start_pos)
}
}
-void maze_print(char ***maze, int width, int height)
+void maze_print(struct Maze maze)
{
- for (int y = 0; y < height; y++)
+ struct Dimensions full_dimens = maze.full_dimens;
+
+ for (int y = 0; y < full_dimens.height; y++)
{
- for (int x = 0; x < width; x++)
+ for (int x = 0; x < full_dimens.width; x++)
{
- printf("%s", maze[y][x]);
+ printf("%s", maze.grid[y][x]);
}
printf("\n");
}
-} \ No newline at end of file
+}
diff --git a/src/maze.h b/src/maze.h
index fc0e489..38dfe12 100644
--- a/src/maze.h
+++ b/src/maze.h
@@ -3,7 +3,7 @@
#include "position_stack.h"
-struct MazeSize
+struct Dimensions
{
int width;
int height;
@@ -12,13 +12,14 @@ struct MazeSize
struct Maze
{
char ***grid;
- struct MazeSize size;
+ struct Dimensions dimens;
+ struct Dimensions full_dimens;
};
-struct Maze maze_create(struct MazeSize size, char *wall);
+struct Maze maze_create(struct Dimensions dimens, char *wall);
void maze_excavate(struct Maze maze, struct Position start_pos);
-void maze_print(char ***maze, int width, int height);
+void maze_print(struct Maze maze);
-#endif \ No newline at end of file
+#endif
diff --git a/src/mazerator.c b/src/mazerator.c
index 2146b40..03bfce3 100644
--- a/src/mazerator.c
+++ b/src/mazerator.c
@@ -121,16 +121,13 @@ int main(int argc, char *argv[])
srand(seed);
- int full_maze_height = maze_height * 2 + 1;
- int full_maze_width = maze_width * 2 + 1;
+ struct Dimensions dimens = {.width = maze_width, .height = maze_height};
- struct MazeSize maze_size = {.width = maze_width, .height = maze_height};
-
- struct Maze maze = maze_create(maze_size, wall);
+ struct Maze maze = maze_create(dimens, wall);
struct Position start_pos = {.x = start_x, .y = start_y};
maze_excavate(maze, start_pos);
- maze_print(maze.grid, full_maze_width, full_maze_height);
+ maze_print(maze);
}
diff --git a/src/position_stack.c b/src/position_stack.c
index 3d546b8..90ace87 100644
--- a/src/position_stack.c
+++ b/src/position_stack.c
@@ -23,7 +23,7 @@ struct PositionStack *create_pos_stack(int capacity)
{
struct PositionStack *stack_pt = malloc(sizeof(struct PositionStack));
- stack_pt->max_size = capacity;
+ stack_pt->capacity = capacity;
stack_pt->top = -1;
stack_pt->items = malloc(sizeof(struct Position) * capacity);
@@ -34,7 +34,7 @@ struct PositionStack *create_pos_stack(int capacity)
void pos_stack_push(struct PositionStack *stack_pt, struct Position pos)
{
// Avoid a overflow by checking if the stack is full
- if (stack_pt->top == stack_pt->max_size - 1)
+ if (stack_pt->top == stack_pt->capacity - 1)
{
stack_error(STACK_ERR_OVERFLOW);
}
@@ -66,4 +66,4 @@ struct Position pos_stack_pop(struct PositionStack *stack_pt)
// Decrease the stack size by 1 and return the popped element
return stack_pt->items[stack_pt->top--];
-} \ No newline at end of file
+}
diff --git a/src/position_stack.h b/src/position_stack.h
index fe93e8c..82c1975 100644
--- a/src/position_stack.h
+++ b/src/position_stack.h
@@ -12,7 +12,7 @@ struct Position
struct PositionStack
{
- int max_size;
+ int capacity;
int top;
struct Position *items;
};
@@ -25,4 +25,4 @@ struct Position pos_stack_peek(struct PositionStack *stack_pt);
struct Position pos_stack_pop(struct PositionStack *stack_pt);
-#endif \ No newline at end of file
+#endif