Compare commits

..

1 Commits

Author SHA1 Message Date
03f4008247 Write README 2024-11-04 00:52:08 +00:00

241
main.c
View File

@@ -14,22 +14,16 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#define MAZE_WIDTH 958 #define MAZE_SIZE 24
#define MAZE_HEIGHT 538
#define PX(x) (x)
#define GRID_WIDTH (2 * MAZE_WIDTH - 1) #define GRID_SIZE (2 * MAZE_SIZE - 1)
#define GRID_HEIGHT (2 * MAZE_HEIGHT - 1) #define MARGIN 2
#define WINDOW_WIDTH (GRID_WIDTH + 2) #define WALL_THICKNESS 1
#define WINDOW_HEIGHT (GRID_HEIGHT + 2) #define WINDOW_SIZE (GRID_SIZE + 2 * (WALL_THICKNESS + MARGIN))
#define ENTRANCE_X (-1) #define PX(x) ((x) << 3)
#define ENTRANCE_Y 0
#define EXIT_X GRID_WIDTH
#define EXIT_Y (GRID_HEIGHT - 1)
#define MAX_PATH_LENGTH (MAZE_WIDTH * MAZE_HEIGHT) #define GOAL (GRID_SIZE - 1)
#define STACK_SIZE MAX_PATH_LENGTH
typedef enum { LEFT, RIGHT, UP, DOWN } dir_t; typedef enum { LEFT, RIGHT, UP, DOWN } dir_t;
@@ -45,7 +39,7 @@ typedef struct {
bool visited : 1; bool visited : 1;
} cell_t; } cell_t;
static const struct timespec path_draw_pause = { .tv_nsec = 100000 }; static const struct timespec wait = { .tv_nsec = 5000000 };
static const vec2_t steps[] = { static const vec2_t steps[] = {
[LEFT] = { .x = -2, .y = 0 }, [LEFT] = { .x = -2, .y = 0 },
@@ -54,112 +48,90 @@ static const vec2_t steps[] = {
[DOWN] = { .x = 0, .y = 2 }, [DOWN] = { .x = 0, .y = 2 },
}; };
static const vec2_t start = { 0, 0 };
static const vec2_t end = { GRID_WIDTH - 1, GRID_HEIGHT - 1 };
static cell_t maze[GRID_WIDTH][GRID_HEIGHT];
static Display *dpy; static Display *dpy;
static Window window; static Window window;
static GC ctx; static GC ctx;
static cell_t maze[GRID_SIZE][GRID_SIZE];
static int bg_col, wall_col, visited_col;
static void draw_cell(int x, int y) static void clear_path(vec2_t p)
{ {
XFillRectangle( const int margin_px = PX(MARGIN + WALL_THICKNESS);
dpy, window, ctx, 1 + PX(x + 1), 1 + PX(y + 1), PX(1), PX(1)); const int left = margin_px + PX(p.x);
const int top = margin_px + PX(p.y);
XFillRectangle(dpy, window, ctx, left, top, PX(1), PX(1));
XClearArea(dpy, window, left, top, PX(1), PX(1), false);
XFlush(dpy);
} }
static void clear_cell(int x, int y) static void draw_visited(vec2_t p)
{ {
XClearArea( const int margin_px = PX(MARGIN + WALL_THICKNESS);
dpy, window, 1 + PX(x + 1), 1 + PX(y + 1), PX(1), PX(1), false); const int left = margin_px + PX(p.x);
const int top = margin_px + PX(p.y);
XFillRectangle(dpy, window, ctx, left, top, PX(1), PX(1));
XFlush(dpy);
} }
static bool in_bounds(int x, int y) static bool
random_walk(coord_pred_t should_visit, visit_fn_t visit_fn, vec2_t start)
{ {
const bool valid_x = x >= 0 && x < GRID_WIDTH; dir_t visit[] = { LEFT, RIGHT, UP, DOWN };
const bool valid_y = y >= 0 && y < GRID_HEIGHT; 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(int x, int y) visit[i] = tmp;
{
int nx, ny;
for (int i = 0; i < 4; ++i) {
nx = x + steps[i].x;
ny = y + steps[i].y;
if (in_bounds(nx, ny) && !maze[nx][ny].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;
}
}
return false;
} }
static void generate(vec2_t p) static bool is_wall(vec2_t c, vec2_t im)
{ {
vec2_t stack[STACK_SIZE], *sp = stack; (void)im;
maze[p.x][p.y].is_path = true; return !maze[c.x][c.y].is_path;
do {
if (finished_gen(p.x, p.y)) {
p = *(--sp);
continue;
}
int nx, ny;
do {
dir_t d = rand() % 4;
nx = p.x + steps[d].x;
ny = p.y + steps[d].y;
} while (!in_bounds(nx, ny) || maze[nx][ny].is_path);
const int imx = (p.x + nx) / 2;
const int imy = (p.y + ny) / 2;
maze[imx][imy].is_path = maze[nx][ny].is_path = true;
*sp++ = p;
p.x = nx;
p.y = ny;
} while (sp != stack);
} }
static void solve(vec2_t p, vec2_t *sp, vec2_t **top) static bool generation_visit(vec2_t c, vec2_t im)
{ {
maze[0][0].visited = true; maze[c.x][c.y].is_path = true;
*sp++ = p; maze[im.x][im.y].is_path = true;
clear_path(im);
clear_path(c);
nanosleep(&wait, NULL);
return false;
}
while (1) { static bool accessible_and_unvisited(vec2_t c, vec2_t im)
if (end.x == p.x && end.y == p.y) { {
*sp++ = p; return maze[im.x][im.y].is_path && !maze[c.x][c.y].visited;
*top = sp; }
return;
}
int nx, ny, imx, imy; static bool solve_visit(vec2_t c, vec2_t im)
bool done = true; {
for (int i = 0; i < 4; ++i) { maze[c.x][c.y].visited = true;
nx = p.x + steps[i].x; maze[im.x][im.y].visited = true;
ny = p.y + steps[i].y; draw_visited(im);
if (!in_bounds(nx, ny)) draw_visited(c);
continue; nanosleep(&wait, NULL);
return GOAL == c.x && GOAL == c.y;
imx = (p.x + nx) / 2;
imy = (p.y + ny) / 2;
if (maze[imx][imy].is_path && !maze[nx][ny].visited) {
done = false;
break;
}
}
if (done) {
p = *(--sp);
continue;
}
maze[imx][imy].visited = maze[nx][ny].visited = true;
*sp++ = p;
p.x = nx;
p.y = ny;
}
} }
int main(void) int main(void)
@@ -174,11 +146,11 @@ int main(void)
assert(dpy); assert(dpy);
// Create window and configure graphics context // Create window and configure graphics context
const int black = BlackPixel(dpy, DefaultScreen(dpy)); wall_col = BlackPixel(dpy, DefaultScreen(dpy));
const int white = WhitePixel(dpy, DefaultScreen(dpy)); bg_col = WhitePixel(dpy, DefaultScreen(dpy));
window = XCreateSimpleWindow( window = XCreateSimpleWindow(
dpy, DefaultRootWindow(dpy), 0, 0, PX(WINDOW_WIDTH), dpy, DefaultRootWindow(dpy), 0, 0, PX(WINDOW_SIZE), PX(WINDOW_SIZE),
PX(WINDOW_HEIGHT), 0, black, black); 0, bg_col, bg_col);
Atom del = XInternAtom(dpy, "WM_DELETE_WINDOW", false); Atom del = XInternAtom(dpy, "WM_DELETE_WINDOW", false);
XSetWMProtocols(dpy, window, &del, 1); XSetWMProtocols(dpy, window, &del, 1);
ctx = DefaultGC(dpy, DefaultScreen(dpy)); ctx = DefaultGC(dpy, DefaultScreen(dpy));
@@ -188,7 +160,7 @@ int main(void)
dpy, window, DefaultVisual(dpy, DefaultScreen(dpy)), AllocNone); dpy, window, DefaultVisual(dpy, DefaultScreen(dpy)), AllocNone);
XColor xcol = { .red = 55555, .green = 10000, .blue = 10000 }; XColor xcol = { .red = 55555, .green = 10000, .blue = 10000 };
XAllocColor(dpy, cm, &xcol); XAllocColor(dpy, cm, &xcol);
const int red = xcol.pixel; visited_col = xcol.pixel;
// Map window // Map window
XSelectInput(dpy, window, StructureNotifyMask); XSelectInput(dpy, window, StructureNotifyMask);
@@ -197,53 +169,34 @@ int main(void)
XNextEvent(dpy, &evt); XNextEvent(dpy, &evt);
while (MapNotify != evt.type); while (MapNotify != evt.type);
// Draw entrance and exit while (1) {
XSetForeground(dpy, ctx, white); // Draw black box for walls
draw_cell(ENTRANCE_X, ENTRANCE_Y); XClearWindow(dpy, window);
draw_cell(EXIT_X, EXIT_Y); XSetForeground(dpy, ctx, wall_col);
XFillRectangle(
dpy, window, ctx, PX(MARGIN), PX(MARGIN), PX(GRID_SIZE + 2),
PX(GRID_SIZE + 2));
const vec2_t exit = { GOAL + 1, GOAL };
clear_path(exit);
XFlush(dpy); XFlush(dpy);
while (1) {
// Generate // Generate
memset(&maze, 0, sizeof(maze)); memset(&maze, 0, sizeof(maze));
generate(end); const vec2_t gen_start = { GOAL, GOAL };
maze[GOAL][GOAL].is_path = true;
// Draw maze clear_path(gen_start);
XSetForeground(dpy, ctx, white); random_walk(is_wall, generation_visit, gen_start);
draw_cell(ENTRANCE_X, ENTRANCE_Y);
for (int y = 0; y < GRID_HEIGHT; ++y) {
for (int x = 0; x < GRID_WIDTH; ++x) {
if (maze[x][y].is_path)
draw_cell(x, y);
else
clear_cell(x, y);
}
}
draw_cell(EXIT_X, EXIT_Y);
XFlush(dpy);
sleep(1);
// Solve // Solve
vec2_t path[MAX_PATH_LENGTH], *path_end; const vec2_t solve_start = { 0, 0 };
solve(start, path, &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);
// Draw solution path // Draw exit path
XSetForeground(dpy, ctx, red); const int x = PX(MARGIN + GRID_SIZE + 1), y = PX(MARGIN + GRID_SIZE);
draw_cell(ENTRANCE_X, ENTRANCE_Y); XFillRectangle(dpy, window, ctx, x, y, PX(1), PX(1));
const vec2_t *prev = &start;
for (const vec2_t *p = path; p < path_end; ++p) {
const int imx = (prev->x + p->x) / 2;
const int imy = (prev->y + p->y) / 2;
draw_cell(imx, imy);
draw_cell(p->x, p->y);
XFlush(dpy);
nanosleep(&path_draw_pause, NULL);
prev = p;
}
draw_cell(EXIT_X, EXIT_Y);
XFlush(dpy); XFlush(dpy);
sleep(1); sleep(1);