Draw the maze seperately to generation
This commit is contained in:
parent
e57b83d97a
commit
08e364e78b
199
main.c
199
main.c
@ -14,20 +14,19 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#define MAZE_WIDTH 100
|
#define MAZE_WIDTH 958
|
||||||
#define MAZE_HEIGHT 100
|
#define MAZE_HEIGHT 538
|
||||||
|
#define PX(x) (x)
|
||||||
#define MARGIN_X 2
|
|
||||||
#define MARGIN_Y 2
|
|
||||||
#define WALL_THICKNESS 1
|
|
||||||
#define PX(x) ((x) << 2)
|
|
||||||
|
|
||||||
#define GRID_WIDTH (2 * MAZE_WIDTH - 1)
|
#define GRID_WIDTH (2 * MAZE_WIDTH - 1)
|
||||||
#define GRID_HEIGHT (2 * MAZE_HEIGHT - 1)
|
#define GRID_HEIGHT (2 * MAZE_HEIGHT - 1)
|
||||||
#define WINDOW_WIDTH (GRID_WIDTH + 2 * (WALL_THICKNESS + MARGIN_X))
|
#define WINDOW_WIDTH (GRID_WIDTH + 2)
|
||||||
#define WINDOW_HEIGHT (GRID_HEIGHT + 2 * (WALL_THICKNESS + MARGIN_Y))
|
#define WINDOW_HEIGHT (GRID_HEIGHT + 2)
|
||||||
#define GOAL_X (GRID_WIDTH - 1)
|
|
||||||
#define GOAL_Y (GRID_HEIGHT - 1)
|
#define ENTRANCE_X (-1)
|
||||||
|
#define ENTRANCE_Y 0
|
||||||
|
#define EXIT_X GRID_WIDTH
|
||||||
|
#define EXIT_Y (GRID_HEIGHT - 1)
|
||||||
|
|
||||||
#define MAX_PATH_LENGTH (MAZE_WIDTH * MAZE_HEIGHT)
|
#define MAX_PATH_LENGTH (MAZE_WIDTH * MAZE_HEIGHT)
|
||||||
#define STACK_SIZE MAX_PATH_LENGTH
|
#define STACK_SIZE MAX_PATH_LENGTH
|
||||||
@ -46,8 +45,7 @@ typedef struct {
|
|||||||
bool visited : 1;
|
bool visited : 1;
|
||||||
} cell_t;
|
} cell_t;
|
||||||
|
|
||||||
static const struct timespec gen_pause = { .tv_nsec = 1000000 };
|
static const struct timespec path_draw_pause = { .tv_nsec = 100000 };
|
||||||
static const struct timespec solve_pause = { .tv_nsec = 4000000 };
|
|
||||||
|
|
||||||
static const vec2_t steps[] = {
|
static const vec2_t steps[] = {
|
||||||
[LEFT] = { .x = -2, .y = 0 },
|
[LEFT] = { .x = -2, .y = 0 },
|
||||||
@ -56,47 +54,40 @@ 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 cell_t maze[GRID_WIDTH][GRID_HEIGHT];
|
|
||||||
static int bg_col, wall_col, visited_col;
|
|
||||||
|
|
||||||
static void clear_path(vec2_t p)
|
static void draw_cell(int x, int y)
|
||||||
{
|
{
|
||||||
const int margin_x_px = PX(MARGIN_X + WALL_THICKNESS);
|
XFillRectangle(
|
||||||
const int margin_y_px = PX(MARGIN_Y + WALL_THICKNESS);
|
dpy, window, ctx, 1 + PX(x + 1), 1 + PX(y + 1), PX(1), PX(1));
|
||||||
const int left = margin_x_px + PX(p.x);
|
|
||||||
const int top = margin_y_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)
|
static void clear_cell(int x, int y)
|
||||||
{
|
{
|
||||||
const int margin_x_px = PX(MARGIN_X + WALL_THICKNESS);
|
XClearArea(
|
||||||
const int margin_y_px = PX(MARGIN_Y + WALL_THICKNESS);
|
dpy, window, 1 + PX(x + 1), 1 + PX(y + 1), PX(1), PX(1), false);
|
||||||
const int left = margin_x_px + PX(p.x);
|
|
||||||
const int top = margin_y_px + PX(p.y);
|
|
||||||
XFillRectangle(dpy, window, ctx, left, top, PX(1), PX(1));
|
|
||||||
XFlush(dpy);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool in_bounds(vec2_t p)
|
static bool in_bounds(int x, int y)
|
||||||
{
|
{
|
||||||
const bool valid_x = p.x >= 0 && p.x < GRID_WIDTH;
|
const bool valid_x = x >= 0 && x < GRID_WIDTH;
|
||||||
const bool valid_y = p.y >= 0 && p.y < GRID_HEIGHT;
|
const bool valid_y = y >= 0 && y < GRID_HEIGHT;
|
||||||
return valid_x && valid_y;
|
return valid_x && valid_y;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool finished_gen(vec2_t p)
|
static bool finished_gen(int x, int y)
|
||||||
{
|
{
|
||||||
vec2_t n;
|
int nx, ny;
|
||||||
for (int i = 0; i < 4; ++i) {
|
for (int i = 0; i < 4; ++i) {
|
||||||
n.x = p.x + steps[i].x;
|
nx = x + steps[i].x;
|
||||||
n.y = p.y + steps[i].y;
|
ny = y + steps[i].y;
|
||||||
if (in_bounds(n) && !maze[n.x][n.y].is_path)
|
if (in_bounds(nx, ny) && !maze[nx][ny].is_path)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -105,69 +96,69 @@ static bool finished_gen(vec2_t p)
|
|||||||
static void generate(vec2_t p)
|
static void generate(vec2_t p)
|
||||||
{
|
{
|
||||||
vec2_t stack[STACK_SIZE], *sp = stack;
|
vec2_t stack[STACK_SIZE], *sp = stack;
|
||||||
|
maze[p.x][p.y].is_path = true;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if (finished_gen(p)) {
|
if (finished_gen(p.x, p.y)) {
|
||||||
p = *(--sp);
|
p = *(--sp);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec2_t n;
|
int nx, ny;
|
||||||
do {
|
do {
|
||||||
dir_t d = rand() % 4;
|
dir_t d = rand() % 4;
|
||||||
n.x = p.x + steps[d].x;
|
nx = p.x + steps[d].x;
|
||||||
n.y = p.y + steps[d].y;
|
ny = p.y + steps[d].y;
|
||||||
} while (!in_bounds(n) || maze[n.x][n.y].is_path);
|
} while (!in_bounds(nx, ny) || maze[nx][ny].is_path);
|
||||||
|
|
||||||
const vec2_t im = {
|
const int imx = (p.x + nx) / 2;
|
||||||
.x = (p.x + n.x) / 2,
|
const int imy = (p.y + ny) / 2;
|
||||||
.y = (p.y + n.y) / 2,
|
maze[imx][imy].is_path = maze[nx][ny].is_path = true;
|
||||||
};
|
|
||||||
maze[im.x][im.y].is_path = maze[n.x][n.y].is_path = true;
|
|
||||||
clear_path(im);
|
|
||||||
clear_path(n);
|
|
||||||
|
|
||||||
*sp++ = p;
|
*sp++ = p;
|
||||||
p = n;
|
p.x = nx;
|
||||||
|
p.y = ny;
|
||||||
nanosleep(&gen_pause, NULL);
|
|
||||||
} while (sp != stack);
|
} while (sp != stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void solve(vec2_t p, vec2_t *sp, vec2_t **end)
|
static void solve(vec2_t p, vec2_t *sp, vec2_t **top)
|
||||||
{
|
{
|
||||||
|
maze[0][0].visited = true;
|
||||||
*sp++ = p;
|
*sp++ = p;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
if (GOAL_X == p.x && GOAL_Y == p.y) {
|
if (end.x == p.x && end.y == p.y) {
|
||||||
*sp++ = p;
|
*sp++ = p;
|
||||||
*end = sp;
|
*top = sp;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec2_t n, im;
|
int nx, ny, imx, imy;
|
||||||
bool got_n = false;
|
bool done = true;
|
||||||
for (int i = 0; i < 4; ++i) {
|
for (int i = 0; i < 4; ++i) {
|
||||||
n.x = p.x + steps[i].x;
|
nx = p.x + steps[i].x;
|
||||||
n.y = p.y + steps[i].y;
|
ny = p.y + steps[i].y;
|
||||||
if (!in_bounds(n))
|
if (!in_bounds(nx, ny))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
im.x = (p.x + n.x) / 2;
|
imx = (p.x + nx) / 2;
|
||||||
im.y = (p.y + n.y) / 2;
|
imy = (p.y + ny) / 2;
|
||||||
if (maze[im.x][im.y].is_path && !maze[n.x][n.y].visited) {
|
if (maze[imx][imy].is_path && !maze[nx][ny].visited) {
|
||||||
got_n = true;
|
done = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!got_n) {
|
if (done) {
|
||||||
p = *(--sp);
|
p = *(--sp);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
maze[im.x][im.y].visited = maze[n.x][n.y].visited = true;
|
maze[imx][imy].visited = maze[nx][ny].visited = true;
|
||||||
|
|
||||||
*sp++ = p;
|
*sp++ = p;
|
||||||
p = n;
|
p.x = nx;
|
||||||
|
p.y = ny;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -183,11 +174,11 @@ int main(void)
|
|||||||
assert(dpy);
|
assert(dpy);
|
||||||
|
|
||||||
// Create window and configure graphics context
|
// Create window and configure graphics context
|
||||||
wall_col = BlackPixel(dpy, DefaultScreen(dpy));
|
const int black = BlackPixel(dpy, DefaultScreen(dpy));
|
||||||
bg_col = WhitePixel(dpy, DefaultScreen(dpy));
|
const int white = WhitePixel(dpy, DefaultScreen(dpy));
|
||||||
window = XCreateSimpleWindow(
|
window = XCreateSimpleWindow(
|
||||||
dpy, DefaultRootWindow(dpy), 0, 0, PX(WINDOW_WIDTH),
|
dpy, DefaultRootWindow(dpy), 0, 0, PX(WINDOW_WIDTH),
|
||||||
PX(WINDOW_HEIGHT), 0, bg_col, bg_col);
|
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));
|
||||||
@ -197,7 +188,7 @@ int main(void)
|
|||||||
dpy, window, DefaultVisual(dpy, DefaultScreen(dpy)), AllocNone);
|
dpy, window, DefaultVisual(dpy, DefaultScreen(dpy)), AllocNone);
|
||||||
XColor xcol = { .red = 55555, .green = 10000, .blue = 10000 };
|
XColor xcol = { .red = 55555, .green = 10000, .blue = 10000 };
|
||||||
XAllocColor(dpy, cm, &xcol);
|
XAllocColor(dpy, cm, &xcol);
|
||||||
visited_col = xcol.pixel;
|
const int red = xcol.pixel;
|
||||||
|
|
||||||
// Map window
|
// Map window
|
||||||
XSelectInput(dpy, window, StructureNotifyMask);
|
XSelectInput(dpy, window, StructureNotifyMask);
|
||||||
@ -206,46 +197,54 @@ int main(void)
|
|||||||
XNextEvent(dpy, &evt);
|
XNextEvent(dpy, &evt);
|
||||||
while (MapNotify != evt.type);
|
while (MapNotify != evt.type);
|
||||||
|
|
||||||
while (1) {
|
// Draw entrance and exit
|
||||||
// Draw black box for walls
|
XSetForeground(dpy, ctx, white);
|
||||||
XClearWindow(dpy, window);
|
draw_cell(ENTRANCE_X, ENTRANCE_Y);
|
||||||
XSetForeground(dpy, ctx, wall_col);
|
draw_cell(EXIT_X, EXIT_Y);
|
||||||
XFillRectangle(
|
|
||||||
dpy, window, ctx, PX(MARGIN_X), PX(MARGIN_Y), PX(GRID_WIDTH + 2),
|
|
||||||
PX(GRID_HEIGHT + 2));
|
|
||||||
const vec2_t exit = { GOAL_X + 1, GOAL_Y };
|
|
||||||
clear_path(exit);
|
|
||||||
XFlush(dpy);
|
XFlush(dpy);
|
||||||
|
|
||||||
|
while (1) {
|
||||||
// Generate
|
// Generate
|
||||||
memset(&maze, 0, sizeof(maze));
|
memset(&maze, 0, sizeof(maze));
|
||||||
const vec2_t gen_start = { GOAL_X, GOAL_Y };
|
generate(end);
|
||||||
maze[GOAL_X][GOAL_Y].is_path = true;
|
|
||||||
clear_path(gen_start);
|
// Draw maze
|
||||||
generate(gen_start);
|
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
|
// Solve
|
||||||
const vec2_t solve_start = { 0, 0 };
|
|
||||||
vec2_t path[MAX_PATH_LENGTH], *path_end;
|
vec2_t path[MAX_PATH_LENGTH], *path_end;
|
||||||
maze[0][0].visited = true;
|
solve(start, path, &path_end);
|
||||||
solve(solve_start, path, &path_end);
|
|
||||||
|
|
||||||
// sleep(1);
|
|
||||||
|
|
||||||
// Draw solution path
|
// Draw solution path
|
||||||
XSetForeground(dpy, ctx, visited_col);
|
XSetForeground(dpy, ctx, red);
|
||||||
const vec2_t *prev = &solve_start;
|
draw_cell(ENTRANCE_X, ENTRANCE_Y);
|
||||||
|
const vec2_t *prev = &start;
|
||||||
for (const vec2_t *p = path; p < path_end; ++p) {
|
for (const vec2_t *p = path; p < path_end; ++p) {
|
||||||
const vec2_t im = {
|
const int imx = (prev->x + p->x) / 2;
|
||||||
.x = (prev->x + p->x) / 2,
|
const int imy = (prev->y + p->y) / 2;
|
||||||
.y = (prev->y + p->y) / 2,
|
|
||||||
};
|
draw_cell(imx, imy);
|
||||||
draw_visited(*p);
|
draw_cell(p->x, p->y);
|
||||||
draw_visited(im);
|
XFlush(dpy);
|
||||||
nanosleep(&solve_pause, NULL);
|
|
||||||
|
nanosleep(&path_draw_pause, NULL);
|
||||||
prev = p;
|
prev = p;
|
||||||
}
|
}
|
||||||
draw_visited(exit);
|
draw_cell(EXIT_X, EXIT_Y);
|
||||||
|
XFlush(dpy);
|
||||||
|
|
||||||
sleep(1);
|
sleep(1);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user