Compare commits
19 Commits
7a2a589c45
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 08e364e78b | |||
| e57b83d97a | |||
| 15fd1e3405 | |||
| eaed12277d | |||
| 048bfd2169 | |||
| 0720b2c0c2 | |||
| c2a043a8b1 | |||
| b89b4b64b2 | |||
| bb44747770 | |||
| ebbaac7db9 | |||
| 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.
|
||||
252
main.c
252
main.c
@@ -12,29 +12,40 @@
|
||||
#include <string.h>
|
||||
#include <sys/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 MARGIN 2
|
||||
#define WALL_THICKNESS 1
|
||||
#define WINDOW_SIZE (GRID_SIZE + 2 * (WALL_THICKNESS + MARGIN))
|
||||
#define GRID_WIDTH (2 * MAZE_WIDTH - 1)
|
||||
#define GRID_HEIGHT (2 * MAZE_HEIGHT - 1)
|
||||
#define WINDOW_WIDTH (GRID_WIDTH + 2)
|
||||
#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 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 path_draw_pause = { .tv_nsec = 100000 };
|
||||
|
||||
static const vec2_t steps[] = {
|
||||
[LEFT] = { .x = -2, .y = 0 },
|
||||
@@ -43,81 +54,111 @@ static const vec2_t steps[] = {
|
||||
[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 Window window;
|
||||
static GC ctx;
|
||||
|
||||
static void draw_walls(void)
|
||||
static void draw_cell(int x, int y)
|
||||
{
|
||||
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);
|
||||
dpy, window, ctx, 1 + PX(x + 1), 1 + PX(y + 1), PX(1), PX(1));
|
||||
}
|
||||
|
||||
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(
|
||||
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);
|
||||
XFillRectangle(dpy, window, ctx, left, top, PX(1), PX(1));
|
||||
}
|
||||
}
|
||||
|
||||
XFlush(dpy);
|
||||
dpy, window, 1 + PX(x + 1), 1 + PX(y + 1), PX(1), PX(1), false);
|
||||
}
|
||||
|
||||
static void generate_maze(maze_t *m, vec2_t p, vec2_t g)
|
||||
static bool in_bounds(int x, int y)
|
||||
{
|
||||
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;
|
||||
return valid_x && valid_y;
|
||||
}
|
||||
|
||||
draw_maze(m);
|
||||
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) {
|
||||
n.x = p.x + steps[visit[i]].x;
|
||||
n.y = p.y + steps[visit[i]].y;
|
||||
|
||||
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);
|
||||
nx = x + steps[i].x;
|
||||
ny = y + steps[i].y;
|
||||
if (in_bounds(nx, ny) && !maze[nx][ny].is_path)
|
||||
return false;
|
||||
}
|
||||
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 white = WhitePixel(dpy, DefaultScreen(dpy));
|
||||
window = XCreateSimpleWindow(
|
||||
dpy, DefaultRootWindow(dpy), 0, 0, PX(WINDOW_SIZE), PX(WINDOW_SIZE),
|
||||
0, white, white);
|
||||
dpy, DefaultRootWindow(dpy), 0, 0, PX(WINDOW_WIDTH),
|
||||
PX(WINDOW_HEIGHT), 0, black, black);
|
||||
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);
|
||||
const int red = xcol.pixel;
|
||||
|
||||
// Map window
|
||||
XSelectInput(dpy, window, StructureNotifyMask);
|
||||
@@ -150,12 +197,57 @@ 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);
|
||||
// Draw entrance and exit
|
||||
XSetForeground(dpy, ctx, white);
|
||||
draw_cell(ENTRANCE_X, ENTRANCE_Y);
|
||||
draw_cell(EXIT_X, EXIT_Y);
|
||||
XFlush(dpy);
|
||||
|
||||
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
|
||||
bool is_del = false;
|
||||
|
||||
Reference in New Issue
Block a user