Compare commits

..

No commits in common. "15fd1e34050c0fef2d8ff1b97d5d89622724ff83" and "ebbaac7db9bd074f520b996a2b6890d7437c319e" have entirely different histories.

160
main.c
View File

@ -14,19 +14,16 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#define MAZE_SIZE 100 #define MAZE_SIZE 24
#define GRID_SIZE (2 * MAZE_SIZE - 1) #define GRID_SIZE (2 * MAZE_SIZE - 1)
#define MARGIN 2 #define MARGIN 2
#define WALL_THICKNESS 1 #define WALL_THICKNESS 1
#define WINDOW_SIZE (GRID_SIZE + 2 * (WALL_THICKNESS + MARGIN)) #define WINDOW_SIZE (GRID_SIZE + 2 * (WALL_THICKNESS + MARGIN))
#define PX(x) ((x) << 2) #define PX(x) ((x) << 3)
#define GOAL (GRID_SIZE - 1) #define GOAL (GRID_SIZE - 1)
#define MAX_PATH_LENGTH (MAZE_SIZE * MAZE_SIZE)
#define STACK_SIZE (MAZE_SIZE * MAZE_SIZE)
typedef enum { LEFT, RIGHT, UP, DOWN } dir_t; typedef enum { LEFT, RIGHT, UP, DOWN } dir_t;
@ -42,8 +39,7 @@ typedef struct {
bool visited : 1; bool visited : 1;
} cell_t; } cell_t;
static const struct timespec gen_pause = { .tv_nsec = 1000000 }; static const struct timespec wait = { .tv_nsec = 5000000 };
static const struct timespec solve_pause = { .tv_nsec = 4000000 };
static const vec2_t steps[] = { static const vec2_t steps[] = {
[LEFT] = { .x = -2, .y = 0 }, [LEFT] = { .x = -2, .y = 0 },
@ -77,92 +73,65 @@ static void draw_visited(vec2_t p)
XFlush(dpy); XFlush(dpy);
} }
static bool in_bounds(vec2_t p) static bool
random_walk(coord_pred_t should_visit, visit_fn_t visit_fn, vec2_t start)
{ {
const bool valid_x = p.x >= 0 && p.x < GRID_SIZE; dir_t visit[] = { LEFT, RIGHT, UP, DOWN };
const bool valid_y = p.y >= 0 && p.y < GRID_SIZE; for (int i = 3; i > 0; --i) {
return valid_x && valid_y; const int r = rand() % (i + 1);
} const dir_t tmp = visit[r];
visit[r] = visit[i];
static bool finished_gen(vec2_t p) visit[i] = tmp;
{
vec2_t n;
for (int i = 0; i < 4; ++i) {
n.x = p.x + steps[i].x;
n.y = p.y + steps[i].y;
if (in_bounds(n) && !maze[n.x][n.y].is_path)
return false;
} }
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 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; return true;
} }
static void generate(vec2_t p)
{
vec2_t stack[STACK_SIZE], *sp = stack;
do {
if (finished_gen(p)) {
p = *(--sp);
continue;
} }
vec2_t n; return false;
do { }
dir_t d = rand() % 4;
n.x = p.x + steps[d].x;
n.y = p.y + steps[d].y;
} while (!in_bounds(n) || maze[n.x][n.y].is_path);
const vec2_t im = { static bool is_wall(vec2_t c, vec2_t im)
.x = (p.x + n.x) / 2, {
.y = (p.y + n.y) / 2, (void)im;
}; return !maze[c.x][c.y].is_path;
maze[im.x][im.y].is_path = maze[n.x][n.y].is_path = true; }
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(im);
clear_path(n); clear_path(c);
nanosleep(&wait, NULL);
*sp++ = p; return false;
p = n;
nanosleep(&gen_pause, NULL);
} while (sp != stack);
} }
static void solve(vec2_t p, vec2_t *sp, vec2_t **end) static bool accessible_and_unvisited(vec2_t c, vec2_t im)
{ {
*sp++ = p; return maze[im.x][im.y].is_path && !maze[c.x][c.y].visited;
while (1) { }
if (GOAL == p.x && GOAL == p.y) {
*sp++ = p;
*end = sp;
return;
}
vec2_t n, im; static bool solve_visit(vec2_t c, vec2_t im)
bool got_n = false; {
for (int i = 0; i < 4; ++i) { maze[c.x][c.y].visited = true;
n.x = p.x + steps[i].x; maze[im.x][im.y].visited = true;
n.y = p.y + steps[i].y; draw_visited(im);
if (!in_bounds(n)) draw_visited(c);
continue; nanosleep(&wait, NULL);
return GOAL == c.x && GOAL == c.y;
im.x = (p.x + n.x) / 2;
im.y = (p.y + n.y) / 2;
if (maze[im.x][im.y].is_path && !maze[n.x][n.y].visited) {
got_n = true;
break;
}
}
if (!got_n) {
p = *(--sp);
continue;
}
maze[im.x][im.y].visited = maze[n.x][n.y].visited = true;
*sp++ = p;
p = n;
}
} }
int main(void) int main(void)
@ -216,30 +185,19 @@ int main(void)
const vec2_t gen_start = { GOAL, GOAL }; const vec2_t gen_start = { GOAL, GOAL };
maze[GOAL][GOAL].is_path = true; maze[GOAL][GOAL].is_path = true;
clear_path(gen_start); clear_path(gen_start);
generate(gen_start); random_walk(is_wall, generation_visit, gen_start);
// Solve // Solve
const vec2_t solve_start = { 0, 0 }; const vec2_t solve_start = { 0, 0 };
vec2_t path[MAX_PATH_LENGTH], *path_end;
maze[0][0].visited = true; maze[0][0].visited = true;
solve(solve_start, path, &path_end);
sleep(1);
// Draw solution path
XSetForeground(dpy, ctx, visited_col); XSetForeground(dpy, ctx, visited_col);
const vec2_t *prev = &solve_start; draw_visited(solve_start);
for (const vec2_t *p = path; p < path_end; ++p) { random_walk(accessible_and_unvisited, solve_visit, solve_start);
const vec2_t im = {
.x = (prev->x + p->x) / 2, // Draw exit path
.y = (prev->y + p->y) / 2, const int x = PX(MARGIN + GRID_SIZE + 1), y = PX(MARGIN + GRID_SIZE);
}; XFillRectangle(dpy, window, ctx, x, y, PX(1), PX(1));
draw_visited(*p); XFlush(dpy);
draw_visited(im);
nanosleep(&solve_pause, NULL);
prev = p;
}
draw_visited(exit);
sleep(1); sleep(1);
} }