/* * Copyright (c) Camden Dixie O'Brien * SPDX-License-Identifier: AGPL-3.0-only */ #define _POSIX_C_SOURCE 199309L #include #include #include #include #include #include #include #include #define MAZE_SIZE 24 #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 GOAL (GRID_SIZE - 1) typedef enum { LEFT, RIGHT, UP, DOWN } dir_t; typedef struct { int x, y; } vec2_t; typedef bool (*coord_pred_t)(vec2_t c, vec2_t im); typedef bool (*visit_fn_t)(vec2_t c, vec2_t im); typedef struct { bool is_path : 1; bool visited : 1; } cell_t; static const struct timespec gen_pause = { .tv_nsec = 5000000 }; static const struct timespec solve_pause = { .tv_nsec = 10000000 }; static const vec2_t steps[] = { [LEFT] = { .x = -2, .y = 0 }, [RIGHT] = { .x = 2, .y = 0 }, [UP] = { .x = 0, .y = -2 }, [DOWN] = { .x = 0, .y = 2 }, }; static Display *dpy; static Window window; static GC ctx; static cell_t maze[GRID_SIZE][GRID_SIZE]; static int bg_col, wall_col, visited_col; static void clear_path(vec2_t p) { const int margin_px = PX(MARGIN + WALL_THICKNESS); 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 draw_visited(vec2_t p) { const int margin_px = PX(MARGIN + WALL_THICKNESS); 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 void generate(vec2_t start) { 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; } for (int i = 0; i < 4; ++i) { 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) { 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; } int main(void) { // Seed random number generation from time struct timeval tv; gettimeofday(&tv, NULL); srand(tv.tv_usec); XEvent evt; dpy = XOpenDisplay(NULL); assert(dpy); // Create window and configure graphics context wall_col = BlackPixel(dpy, DefaultScreen(dpy)); bg_col = WhitePixel(dpy, DefaultScreen(dpy)); window = XCreateSimpleWindow( dpy, DefaultRootWindow(dpy), 0, 0, PX(WINDOW_SIZE), PX(WINDOW_SIZE), 0, bg_col, bg_col); Atom del = XInternAtom(dpy, "WM_DELETE_WINDOW", false); XSetWMProtocols(dpy, window, &del, 1); ctx = DefaultGC(dpy, DefaultScreen(dpy)); // Create colormap and allocate colour for visited cells Colormap cm = XCreateColormap( dpy, window, DefaultVisual(dpy, DefaultScreen(dpy)), AllocNone); XColor xcol = { .red = 55555, .green = 10000, .blue = 10000 }; XAllocColor(dpy, cm, &xcol); visited_col = xcol.pixel; // Map window XSelectInput(dpy, window, StructureNotifyMask); XMapWindow(dpy, window); do XNextEvent(dpy, &evt); while (MapNotify != evt.type); while (1) { // Draw black box for walls XClearWindow(dpy, window); 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); // Generate memset(&maze, 0, sizeof(maze)); const vec2_t gen_start = { GOAL, GOAL }; maze[GOAL][GOAL].is_path = true; clear_path(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); solve(solve_start); // 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); } // Wait for window exit bool is_del = false; do { XNextEvent(dpy, &evt); if (ClientMessage == evt.type) is_del = (unsigned long)evt.xclient.data.l[0] == del; } while (!is_del); XCloseDisplay(dpy); return EXIT_SUCCESS; }