From b89b4b64b2c1bfaeed1f71ffdab95bcfc759bb6a Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Mon, 4 Nov 2024 21:15:58 +0000 Subject: [PATCH] Seperate generate and solve procedures --- main.c | 99 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 54 insertions(+), 45 deletions(-) diff --git a/main.c b/main.c index 9d153c9..022f9b0 100644 --- a/main.c +++ b/main.c @@ -74,8 +74,7 @@ static void draw_visited(vec2_t p) XFlush(dpy); } -static bool -random_walk(coord_pred_t should_visit, visit_fn_t visit_fn, vec2_t start) +static void generate(vec2_t start) { dir_t visit[] = { LEFT, RIGHT, UP, DOWN }; for (int i = 3; i > 0; --i) { @@ -85,56 +84,66 @@ random_walk(coord_pred_t should_visit, visit_fn_t visit_fn, vec2_t start) visit[i] = tmp; } - vec2_t next, im; for (int i = 0; i < 4; ++i) { - next.x = start.x + steps[visit[i]].x; - next.y = start.y + steps[visit[i]].y; - im.x = (start.x + next.x) / 2; - im.y = (start.y + next.y) / 2; - + const vec2_t next = { + .x = start.x + steps[visit[i]].x, + .y = start.y + steps[visit[i]].y, + }; + const vec2_t im = { + .x = (start.x + next.x) / 2, + .y = (start.y + next.y) / 2, + }; const bool x_in_bounds = next.x >= 0 && next.x < GRID_SIZE; const bool y_in_bounds = next.y >= 0 && next.y < GRID_SIZE; - if (x_in_bounds && y_in_bounds && should_visit(next, im)) { - if (visit_fn(next, im) - || random_walk(should_visit, visit_fn, next)) - return true; + + if (x_in_bounds && y_in_bounds) { + const bool is_wall = !maze[next.x][next.y].is_path; + if (is_wall) { + maze[next.x][next.y].is_path = true; + clear_path(next); + maze[im.x][im.y].is_path = true; + clear_path(im); + + nanosleep(&gen_pause, NULL); + generate(next); + } + } + } +} + +static bool solve(vec2_t start) +{ + for (int i = 0; i < 4; ++i) { + const vec2_t next = { + .x = start.x + steps[i].x, + .y = start.y + steps[i].y, + }; + const vec2_t im = { + .x = (start.x + next.x) / 2, + .y = (start.y + next.y) / 2, + }; + const bool x_in_bounds = next.x >= 0 && next.x < GRID_SIZE; + const bool y_in_bounds = next.y >= 0 && next.y < GRID_SIZE; + + if (x_in_bounds && y_in_bounds) { + const bool accessible = maze[im.x][im.y].is_path; + const bool unvisited = !maze[next.x][next.y].visited; + if (accessible && unvisited) { + maze[next.x][next.y].visited = true; + draw_visited(next); + maze[im.x][im.y].visited = true; + draw_visited(im); + + nanosleep(&solve_pause, NULL); + if ((GOAL == next.x && GOAL == next.y) || solve(next)) + return true; + } } } return false; } -static bool is_wall(vec2_t c, vec2_t im) -{ - (void)im; - return !maze[c.x][c.y].is_path; -} - -static bool generation_visit(vec2_t c, vec2_t im) -{ - maze[c.x][c.y].is_path = true; - maze[im.x][im.y].is_path = true; - clear_path(im); - clear_path(c); - nanosleep(&gen_pause, NULL); - return false; -} - -static bool accessible_and_unvisited(vec2_t c, vec2_t im) -{ - return maze[im.x][im.y].is_path && !maze[c.x][c.y].visited; -} - -static bool solve_visit(vec2_t c, vec2_t im) -{ - maze[c.x][c.y].visited = true; - maze[im.x][im.y].visited = true; - draw_visited(im); - draw_visited(c); - nanosleep(&solve_pause, NULL); - return GOAL == c.x && GOAL == c.y; -} - int main(void) { // Seed random number generation from time @@ -186,14 +195,14 @@ int main(void) const vec2_t gen_start = { GOAL, GOAL }; maze[GOAL][GOAL].is_path = true; clear_path(gen_start); - random_walk(is_wall, generation_visit, gen_start); + generate(gen_start); // Solve const vec2_t solve_start = { 0, 0 }; maze[0][0].visited = true; XSetForeground(dpy, ctx, visited_col); draw_visited(solve_start); - random_walk(accessible_and_unvisited, solve_visit, solve_start); + solve(solve_start); // Draw exit path const int x = PX(MARGIN + GRID_SIZE + 1), y = PX(MARGIN + GRID_SIZE);