Compare commits

...

19 Commits

Author SHA1 Message Date
08e364e78b Draw the maze seperately to generation 2024-11-05 00:39:24 +00:00
e57b83d97a Support non-square mazes 2024-11-04 22:55:47 +00:00
15fd1e3405 Increase maze size and decrease cell size 2024-11-04 22:32:24 +00:00
eaed12277d Change pauses 2024-11-04 22:30:43 +00:00
048bfd2169 Rewrite solve procedure to be iterative instead of recursive 2024-11-04 22:30:33 +00:00
0720b2c0c2 Rewrite generate procedure to be iterative instead of recursive 2024-11-04 22:30:24 +00:00
c2a043a8b1 Draw shortest path to exit instead of all visited cells 2024-11-04 21:34:06 +00:00
b89b4b64b2 Seperate generate and solve procedures 2024-11-04 21:15:58 +00:00
bb44747770 Pause for longer while solving 2024-11-04 20:44:20 +00:00
ebbaac7db9 Write a README 2024-11-04 00:52:57 +00:00
aaf400d881 Loop after solving maze 2024-11-04 00:49:03 +00:00
bf145830cc Draw/clear as needed instead of redrawing window each time 2024-11-04 00:49:03 +00:00
6675c55916 Draw path from exit after solving 2024-11-04 00:08:52 +00:00
f19881fd04 Solve maze after generation 2024-11-04 00:06:21 +00:00
f4aa1bc01c Draw visited cells in a different colour 2024-11-04 00:03:17 +00:00
cc43e870fe Add 'visited' flag to each cell 2024-11-04 00:03:15 +00:00
ce8195f3e8 Generalise solve procedure to random walk 2024-11-04 00:02:56 +00:00
a741490c4c Remove early return in generation routine 2024-11-03 22:54:06 +00:00
95133052d3 Store maze in static variable 2024-11-03 22:50:23 +00:00
2 changed files with 183 additions and 78 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.

250
main.c
View File

@@ -12,29 +12,40 @@
#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_WIDTH 958
#define MAZE_HEIGHT 538
#define PX(x) (x)
#define GRID_SIZE (2 * MAZE_SIZE - 1) #define GRID_WIDTH (2 * MAZE_WIDTH - 1)
#define MARGIN 2 #define GRID_HEIGHT (2 * MAZE_HEIGHT - 1)
#define WALL_THICKNESS 1 #define WINDOW_WIDTH (GRID_WIDTH + 2)
#define WINDOW_SIZE (GRID_SIZE + 2 * (WALL_THICKNESS + MARGIN)) #define WINDOW_HEIGHT (GRID_HEIGHT + 2)
#define PX(x) ((x) << 3) #define ENTRANCE_X (-1)
#define ENTRANCE_Y 0
#define EXIT_X GRID_WIDTH
#define EXIT_Y (GRID_HEIGHT - 1)
#define GOAL (GRID_SIZE - 1) #define MAX_PATH_LENGTH (MAZE_WIDTH * MAZE_HEIGHT)
#define STACK_SIZE MAX_PATH_LENGTH
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 path_draw_pause = { .tv_nsec = 100000 };
static const vec2_t steps[] = { static const vec2_t steps[] = {
[LEFT] = { .x = -2, .y = 0 }, [LEFT] = { .x = -2, .y = 0 },
@@ -43,81 +54,111 @@ 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 void draw_walls(void) static void draw_cell(int x, int y)
{ {
XFillRectangle( XFillRectangle(
dpy, window, ctx, PX(MARGIN), PX(MARGIN), PX(GRID_SIZE + 2), dpy, window, ctx, 1 + PX(x + 1), 1 + PX(y + 1), PX(1), PX(1));
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_cell(int x, int y)
{ {
const int margin_px = PX(MARGIN + WALL_THICKNESS);
XClearArea( XClearArea(
dpy, window, margin_px, margin_px, PX(GRID_SIZE), PX(GRID_SIZE), dpy, window, 1 + PX(x + 1), 1 + PX(y + 1), PX(1), PX(1), false);
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));
}
} }
XFlush(dpy); static bool in_bounds(int x, int y)
}
static void generate_maze(maze_t *m, vec2_t p, vec2_t g)
{ {
m->cells[p.x][p.y] = true; const bool valid_x = x >= 0 && x < GRID_WIDTH;
const bool valid_y = y >= 0 && y < GRID_HEIGHT;
draw_maze(m); return valid_x && valid_y;
nanosleep(&pause, NULL);
if (p.x == g.x && p.y == g.y)
return;
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;
} }
vec2_t n; static bool finished_gen(int x, int y)
{
int nx, ny;
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4; ++i) {
n.x = p.x + steps[visit[i]].x; nx = x + steps[i].x;
n.y = p.y + steps[visit[i]].y; ny = y + steps[i].y;
if (in_bounds(nx, ny) && !maze[nx][ny].is_path)
const bool x_in_bounds = n.x >= 0 && n.x < GRID_SIZE; return false;
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);
} }
return true;
}
static void generate(vec2_t p)
{
vec2_t stack[STACK_SIZE], *sp = stack;
maze[p.x][p.y].is_path = true;
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)
{
maze[0][0].visited = true;
*sp++ = p;
while (1) {
if (end.x == p.x && end.y == p.y) {
*sp++ = p;
*top = sp;
return;
}
int nx, ny, imx, imy;
bool done = true;
for (int i = 0; i < 4; ++i) {
nx = p.x + steps[i].x;
ny = p.y + steps[i].y;
if (!in_bounds(nx, ny))
continue;
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;
} }
} }
@@ -136,12 +177,18 @@ int main(void)
const int black = BlackPixel(dpy, DefaultScreen(dpy)); const int black = BlackPixel(dpy, DefaultScreen(dpy));
const int white = WhitePixel(dpy, DefaultScreen(dpy)); const int white = 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_WIDTH),
0, white, white); PX(WINDOW_HEIGHT), 0, black, black);
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);
const int red = xcol.pixel;
// Map window // Map window
XSelectInput(dpy, window, StructureNotifyMask); XSelectInput(dpy, window, StructureNotifyMask);
@@ -150,12 +197,57 @@ int main(void)
XNextEvent(dpy, &evt); XNextEvent(dpy, &evt);
while (MapNotify != evt.type); while (MapNotify != evt.type);
// Generate and draw maze // Draw entrance and exit
maze_t m; XSetForeground(dpy, ctx, white);
memset(&m, 0, sizeof(maze_t)); draw_cell(ENTRANCE_X, ENTRANCE_Y);
draw_walls(); draw_cell(EXIT_X, EXIT_Y);
const vec2_t start = { GOAL, GOAL }, end = { 0, 0 }; XFlush(dpy);
generate_maze(&m, start, end);
while (1) {
// Generate
memset(&maze, 0, sizeof(maze));
generate(end);
// Draw maze
XSetForeground(dpy, ctx, white);
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
vec2_t path[MAX_PATH_LENGTH], *path_end;
solve(start, path, &path_end);
// Draw solution path
XSetForeground(dpy, ctx, red);
draw_cell(ENTRANCE_X, ENTRANCE_Y);
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);
sleep(1);
}
// Wait for window exit // Wait for window exit
bool is_del = false; bool is_del = false;