Compare commits

...

7 Commits

150
main.c
View File

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