Compare commits
10 Commits
7a2a589c45
...
03f4008247
Author | SHA1 | Date | |
---|---|---|---|
03f4008247 | |||
aaf400d881 | |||
bf145830cc | |||
6675c55916 | |||
f19881fd04 | |||
f4aa1bc01c | |||
cc43e870fe | |||
ce8195f3e8 | |||
a741490c4c | |||
95133052d3 |
13
README
Normal file
13
README
Normal 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
179
main.c
@ -12,6 +12,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#define MAZE_SIZE 24
|
#define MAZE_SIZE 24
|
||||||
|
|
||||||
@ -26,15 +27,19 @@
|
|||||||
|
|
||||||
typedef enum { LEFT, RIGHT, UP, DOWN } dir_t;
|
typedef enum { LEFT, RIGHT, UP, DOWN } dir_t;
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
bool cells[GRID_SIZE][GRID_SIZE];
|
|
||||||
} maze_t;
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int x, y;
|
int x, y;
|
||||||
} vec2_t;
|
} 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[] = {
|
static const vec2_t steps[] = {
|
||||||
[LEFT] = { .x = -2, .y = 0 },
|
[LEFT] = { .x = -2, .y = 0 },
|
||||||
@ -46,56 +51,31 @@ static const vec2_t steps[] = {
|
|||||||
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_walls(void)
|
static void clear_path(vec2_t p)
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
const int margin_px = PX(MARGIN + WALL_THICKNESS);
|
const int margin_px = PX(MARGIN + WALL_THICKNESS);
|
||||||
XClearArea(
|
const int left = margin_px + PX(p.x);
|
||||||
dpy, window, margin_px, margin_px, PX(GRID_SIZE), PX(GRID_SIZE),
|
const int top = margin_px + PX(p.y);
|
||||||
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);
|
|
||||||
XFillRectangle(dpy, window, ctx, left, top, PX(1), PX(1));
|
XFillRectangle(dpy, window, ctx, left, top, PX(1), PX(1));
|
||||||
}
|
XClearArea(dpy, window, left, top, PX(1), PX(1), false);
|
||||||
}
|
|
||||||
|
|
||||||
XFlush(dpy);
|
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;
|
const int margin_px = PX(MARGIN + WALL_THICKNESS);
|
||||||
|
const int left = margin_px + PX(p.x);
|
||||||
draw_maze(m);
|
const int top = margin_px + PX(p.y);
|
||||||
nanosleep(&pause, NULL);
|
XFillRectangle(dpy, window, ctx, left, top, PX(1), PX(1));
|
||||||
|
XFlush(dpy);
|
||||||
if (p.x == g.x && p.y == g.y)
|
}
|
||||||
return;
|
|
||||||
|
|
||||||
|
static bool
|
||||||
|
random_walk(coord_pred_t should_visit, visit_fn_t visit_fn, vec2_t start)
|
||||||
|
{
|
||||||
dir_t visit[] = { LEFT, RIGHT, UP, DOWN };
|
dir_t visit[] = { LEFT, RIGHT, UP, DOWN };
|
||||||
for (int i = 3; i > 0; --i) {
|
for (int i = 3; i > 0; --i) {
|
||||||
const int r = rand() % (i + 1);
|
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;
|
visit[i] = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec2_t n;
|
vec2_t next, im;
|
||||||
for (int i = 0; i < 4; ++i) {
|
for (int i = 0; i < 4; ++i) {
|
||||||
n.x = p.x + steps[visit[i]].x;
|
next.x = start.x + steps[visit[i]].x;
|
||||||
n.y = p.y + steps[visit[i]].y;
|
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 x_in_bounds = next.x >= 0 && next.x < GRID_SIZE;
|
||||||
const bool y_in_bounds = n.y >= 0 && n.y < GRID_SIZE;
|
const bool y_in_bounds = next.y >= 0 && next.y < GRID_SIZE;
|
||||||
if (x_in_bounds && y_in_bounds && !m->cells[n.x][n.y]) {
|
if (x_in_bounds && y_in_bounds && should_visit(next, im)) {
|
||||||
const int xi = (p.x + n.x) / 2;
|
if (visit_fn(next, im)
|
||||||
const int yi = (p.y + n.y) / 2;
|
|| random_walk(should_visit, visit_fn, next))
|
||||||
m->cells[xi][yi] = true;
|
return true;
|
||||||
|
|
||||||
generate_maze(m, n, g);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
int main(void)
|
||||||
@ -133,15 +146,21 @@ 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_SIZE), PX(WINDOW_SIZE),
|
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);
|
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));
|
||||||
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
|
// Map window
|
||||||
XSelectInput(dpy, window, StructureNotifyMask);
|
XSelectInput(dpy, window, StructureNotifyMask);
|
||||||
@ -150,12 +169,38 @@ int main(void)
|
|||||||
XNextEvent(dpy, &evt);
|
XNextEvent(dpy, &evt);
|
||||||
while (MapNotify != evt.type);
|
while (MapNotify != evt.type);
|
||||||
|
|
||||||
// Generate and draw maze
|
while (1) {
|
||||||
maze_t m;
|
// Draw black box for walls
|
||||||
memset(&m, 0, sizeof(maze_t));
|
XClearWindow(dpy, window);
|
||||||
draw_walls();
|
XSetForeground(dpy, ctx, wall_col);
|
||||||
const vec2_t start = { GOAL, GOAL }, end = { 0, 0 };
|
XFillRectangle(
|
||||||
generate_maze(&m, start, end);
|
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
|
// Wait for window exit
|
||||||
bool is_del = false;
|
bool is_del = false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user