Compare commits

...

7 Commits

158
main.c
View File

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