Add 'visited' flag to each cell

This commit is contained in:
Camden Dixie O'Brien 2024-11-03 23:13:39 +00:00
parent ce8195f3e8
commit cc43e870fe

17
main.c
View File

@ -33,6 +33,11 @@ typedef struct {
typedef bool (*coord_pred_t)(vec2_t c, vec2_t im); typedef bool (*coord_pred_t)(vec2_t c, vec2_t im);
typedef bool (*visit_fn_t)(vec2_t c, vec2_t im); typedef bool (*visit_fn_t)(vec2_t c, vec2_t im);
typedef struct {
bool is_path : 1;
bool visited : 1;
} cell_t;
static const struct timespec pause = { .tv_nsec = 5000000 }; static const struct timespec pause = { .tv_nsec = 5000000 };
static const vec2_t steps[] = { static const vec2_t steps[] = {
@ -45,7 +50,7 @@ static const vec2_t steps[] = {
static Display *dpy; static Display *dpy;
static Window window; static Window window;
static GC ctx; static GC ctx;
static bool maze[GRID_SIZE][GRID_SIZE]; static cell_t maze[GRID_SIZE][GRID_SIZE];
static void draw_walls(void) static void draw_walls(void)
{ {
@ -75,7 +80,7 @@ static void draw_maze(void)
for (int x = 0; x < GRID_SIZE; ++x) { for (int x = 0; x < GRID_SIZE; ++x) {
for (int y = 0; y < GRID_SIZE; ++y) { for (int y = 0; y < GRID_SIZE; ++y) {
if (maze[x][y]) if (maze[x][y].is_path)
continue; continue;
const int left = margin_px + PX(x); const int left = margin_px + PX(x);
const int top = margin_px + PX(y); const int top = margin_px + PX(y);
@ -122,13 +127,13 @@ random_walk(coord_pred_t should_visit, visit_fn_t visit_fn, vec2_t start)
static bool is_wall(vec2_t c, vec2_t im) static bool is_wall(vec2_t c, vec2_t im)
{ {
(void)im; (void)im;
return !maze[c.x][c.y]; return !maze[c.x][c.y].is_path;
} }
static bool generation_visit(vec2_t c, vec2_t im) static bool generation_visit(vec2_t c, vec2_t im)
{ {
maze[c.x][c.y] = true; maze[c.x][c.y].is_path = true;
maze[im.x][im.y] = true; maze[im.x][im.y].is_path = true;
return false; return false;
} }
@ -166,7 +171,7 @@ int main(void)
// Generate // Generate
memset(&maze, 0, sizeof(maze)); memset(&maze, 0, sizeof(maze));
const vec2_t gen_start = { GOAL, GOAL }; const vec2_t gen_start = { GOAL, GOAL };
maze[GOAL][GOAL] = true; maze[GOAL][GOAL].is_path = true;
random_walk(is_wall, generation_visit, gen_start); random_walk(is_wall, generation_visit, gen_start);
// Wait for window exit // Wait for window exit