Compare commits

..

10 Commits

2 changed files with 126 additions and 68 deletions

13
README Normal file
View File

@ -0,0 +1,13 @@
MAZE THING
I fancied making something nice and a bit graphical, decided on a maze
generator/solver as it seemed fun and chill. The build is handled
with CMake:
cmake -B build # Configure build
cmake --build build # Build
build/maze-thing # Run
I use Clang but the code is ISO C11 so it should work with other
compilers. Dependency-wise, it should only need the Xlib libraries
and headers.

179
main.c
View File

@ -12,6 +12,7 @@
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#define MAZE_SIZE 24
@ -26,15 +27,19 @@
typedef enum { LEFT, RIGHT, UP, DOWN } dir_t;
typedef struct {
bool cells[GRID_SIZE][GRID_SIZE];
} maze_t;
typedef struct {
int x, y;
} vec2_t;
static const struct timespec pause = { .tv_nsec = 5000000 };
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 wait = { .tv_nsec = 5000000 };
static const vec2_t steps[] = {
[LEFT] = { .x = -2, .y = 0 },
@ -46,56 +51,31 @@ static const vec2_t steps[] = {
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 draw_walls(void)
{
XFillRectangle(
dpy, window, ctx, PX(MARGIN), PX(MARGIN), PX(GRID_SIZE + 2),
PX(WALL_THICKNESS));
XFillRectangle(
dpy, window, ctx, PX(MARGIN),
PX(MARGIN + WALL_THICKNESS + GRID_SIZE), PX(GRID_SIZE + 2),
PX(WALL_THICKNESS));
XFillRectangle(
dpy, window, ctx, PX(MARGIN), PX(MARGIN + WALL_THICKNESS),
PX(WALL_THICKNESS), PX(GRID_SIZE));
XFillRectangle(
dpy, window, ctx, PX(MARGIN + WALL_THICKNESS + GRID_SIZE),
PX(MARGIN + WALL_THICKNESS), PX(WALL_THICKNESS), PX(GRID_SIZE - 1));
XFlush(dpy);
}
static void draw_maze(const maze_t *m)
static void clear_path(vec2_t p)
{
const int margin_px = PX(MARGIN + WALL_THICKNESS);
XClearArea(
dpy, window, margin_px, margin_px, PX(GRID_SIZE), PX(GRID_SIZE),
false);
for (int x = 0; x < GRID_SIZE; ++x) {
for (int y = 0; y < GRID_SIZE; ++y) {
if (m->cells[x][y])
continue;
const int left = margin_px + PX(x);
const int top = margin_px + PX(y);
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 generate_maze(maze_t *m, vec2_t p, vec2_t g)
static void draw_visited(vec2_t p)
{
m->cells[p.x][p.y] = true;
draw_maze(m);
nanosleep(&pause, NULL);
if (p.x == g.x && p.y == g.y)
return;
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 bool
random_walk(coord_pred_t should_visit, visit_fn_t visit_fn, vec2_t start)
{
dir_t visit[] = { LEFT, RIGHT, UP, DOWN };
for (int i = 3; i > 0; --i) {
const int r = rand() % (i + 1);
@ -104,21 +84,54 @@ static void generate_maze(maze_t *m, vec2_t p, vec2_t g)
visit[i] = tmp;
}
vec2_t n;
vec2_t next, im;
for (int i = 0; i < 4; ++i) {
n.x = p.x + steps[visit[i]].x;
n.y = p.y + steps[visit[i]].y;
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 = n.x >= 0 && n.x < GRID_SIZE;
const bool y_in_bounds = n.y >= 0 && n.y < GRID_SIZE;
if (x_in_bounds && y_in_bounds && !m->cells[n.x][n.y]) {
const int xi = (p.x + n.x) / 2;
const int yi = (p.y + n.y) / 2;
m->cells[xi][yi] = true;
generate_maze(m, n, g);
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 false;
}
static bool is_wall(vec2_t c, vec2_t im)
{
(void)im;
return !maze[c.x][c.y].is_path;
}
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(c);
nanosleep(&wait, NULL);
return false;
}
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;
}
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;
}
int main(void)
@ -133,15 +146,21 @@ int main(void)
assert(dpy);
// Create window and configure graphics context
const int black = BlackPixel(dpy, DefaultScreen(dpy));
const int white = WhitePixel(dpy, DefaultScreen(dpy));
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, white, white);
0, bg_col, bg_col);
Atom del = XInternAtom(dpy, "WM_DELETE_WINDOW", false);
XSetWMProtocols(dpy, window, &del, 1);
ctx = DefaultGC(dpy, DefaultScreen(dpy));
XSetForeground(dpy, ctx, black);
// 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);
@ -150,12 +169,38 @@ int main(void)
XNextEvent(dpy, &evt);
while (MapNotify != evt.type);
// Generate and draw maze
maze_t m;
memset(&m, 0, sizeof(maze_t));
draw_walls();
const vec2_t start = { GOAL, GOAL }, end = { 0, 0 };
generate_maze(&m, start, end);
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);
random_walk(is_wall, generation_visit, gen_start);
// Solve
const vec2_t solve_start = { 0, 0 };
maze[0][0].visited = true;
XSetForeground(dpy, ctx, visited_col);
draw_visited(solve_start);
random_walk(accessible_and_unvisited, solve_visit, 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;