Store maze in static variable

This commit is contained in:
Camden Dixie O'Brien 2024-11-03 22:50:23 +00:00
parent 7a2a589c45
commit 95133052d3

26
main.c
View File

@ -26,10 +26,6 @@
typedef enum { LEFT, RIGHT, UP, DOWN } dir_t; typedef enum { LEFT, RIGHT, UP, DOWN } dir_t;
typedef struct {
bool cells[GRID_SIZE][GRID_SIZE];
} maze_t;
typedef struct { typedef struct {
int x, y; int x, y;
} vec2_t; } vec2_t;
@ -46,6 +42,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 void draw_walls(void) static void draw_walls(void)
{ {
@ -66,7 +63,7 @@ static void draw_walls(void)
XFlush(dpy); XFlush(dpy);
} }
static void draw_maze(const maze_t *m) static void draw_maze(void)
{ {
const int margin_px = PX(MARGIN + WALL_THICKNESS); const int margin_px = PX(MARGIN + WALL_THICKNESS);
XClearArea( XClearArea(
@ -75,7 +72,7 @@ static void draw_maze(const maze_t *m)
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 (m->cells[x][y]) if (maze[x][y])
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);
@ -86,11 +83,11 @@ static void draw_maze(const maze_t *m)
XFlush(dpy); XFlush(dpy);
} }
static void generate_maze(maze_t *m, vec2_t p, vec2_t g) static void generate_maze(vec2_t p, vec2_t g)
{ {
m->cells[p.x][p.y] = true; maze[p.x][p.y] = true;
draw_maze(m); draw_maze();
nanosleep(&pause, NULL); nanosleep(&pause, NULL);
if (p.x == g.x && p.y == g.y) if (p.x == g.x && p.y == g.y)
@ -111,12 +108,12 @@ static void generate_maze(maze_t *m, vec2_t p, vec2_t g)
const bool x_in_bounds = n.x >= 0 && n.x < GRID_SIZE; const bool x_in_bounds = n.x >= 0 && n.x < GRID_SIZE;
const bool y_in_bounds = n.y >= 0 && n.y < GRID_SIZE; const bool y_in_bounds = n.y >= 0 && n.y < GRID_SIZE;
if (x_in_bounds && y_in_bounds && !m->cells[n.x][n.y]) { if (x_in_bounds && y_in_bounds && !maze[n.x][n.y]) {
const int xi = (p.x + n.x) / 2; const int xi = (p.x + n.x) / 2;
const int yi = (p.y + n.y) / 2; const int yi = (p.y + n.y) / 2;
m->cells[xi][yi] = true; maze[xi][yi] = true;
generate_maze(m, n, g); generate_maze(n, g);
} }
} }
} }
@ -151,11 +148,10 @@ int main(void)
while (MapNotify != evt.type); while (MapNotify != evt.type);
// Generate and draw maze // Generate and draw maze
maze_t m; memset(&maze, 0, sizeof(maze));
memset(&m, 0, sizeof(maze_t));
draw_walls(); draw_walls();
const vec2_t start = { GOAL, GOAL }, end = { 0, 0 }; const vec2_t start = { GOAL, GOAL }, end = { 0, 0 };
generate_maze(&m, start, end); generate_maze(start, end);
// Wait for window exit // Wait for window exit
bool is_del = false; bool is_del = false;