Draw walls seperately to maze and generate backwards
This commit is contained in:
parent
af9106bae5
commit
8c70d6a70f
52
main.c
52
main.c
@ -43,11 +43,8 @@ static const vec2_t steps[] = {
|
||||
[DOWN] = { .x = 0, .y = 2 },
|
||||
};
|
||||
|
||||
static void draw_maze(Display *dpy, Window w, GC gc, const maze_t *m)
|
||||
static void draw_walls(Display *dpy, Window w, GC gc)
|
||||
{
|
||||
XClearWindow(dpy, w);
|
||||
|
||||
// Draw walls
|
||||
XFillRectangle(
|
||||
dpy, w, gc, PX(MARGIN), PX(MARGIN), PX(GRID_SIZE + 2),
|
||||
PX(WALL_THICKNESS));
|
||||
@ -55,19 +52,27 @@ static void draw_maze(Display *dpy, Window w, GC gc, const maze_t *m)
|
||||
dpy, w, gc, PX(MARGIN), PX(MARGIN + WALL_THICKNESS + GRID_SIZE),
|
||||
PX(GRID_SIZE + 2), PX(WALL_THICKNESS));
|
||||
XFillRectangle(
|
||||
dpy, w, gc, PX(MARGIN), PX(MARGIN + WALL_THICKNESS + 1),
|
||||
PX(WALL_THICKNESS), PX(GRID_SIZE - 1));
|
||||
dpy, w, gc, PX(MARGIN), PX(MARGIN + WALL_THICKNESS),
|
||||
PX(WALL_THICKNESS), PX(GRID_SIZE));
|
||||
XFillRectangle(
|
||||
dpy, w, gc, PX(MARGIN + WALL_THICKNESS + GRID_SIZE),
|
||||
PX(MARGIN + WALL_THICKNESS), PX(WALL_THICKNESS), PX(GRID_SIZE));
|
||||
PX(MARGIN + WALL_THICKNESS), PX(WALL_THICKNESS), PX(GRID_SIZE - 1));
|
||||
|
||||
XFlush(dpy);
|
||||
}
|
||||
|
||||
static void draw_maze(Display *dpy, Window w, GC gc, const maze_t *m)
|
||||
{
|
||||
const int margin_px = PX(MARGIN + WALL_THICKNESS);
|
||||
XClearArea(
|
||||
dpy, w, margin_px, margin_px, PX(GRID_SIZE), PX(GRID_SIZE), false);
|
||||
|
||||
// Draw cells
|
||||
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 = PX(MARGIN + WALL_THICKNESS + x);
|
||||
const int top = PX(MARGIN + WALL_THICKNESS + y);
|
||||
const int left = margin_px + PX(x);
|
||||
const int top = margin_px + PX(y);
|
||||
XFillRectangle(dpy, w, gc, left, top, PX(1), PX(1));
|
||||
}
|
||||
}
|
||||
@ -76,14 +81,14 @@ static void draw_maze(Display *dpy, Window w, GC gc, const maze_t *m)
|
||||
}
|
||||
|
||||
static void
|
||||
generate_maze(Display *dpy, Window w, GC gc, maze_t *m, int x, int y)
|
||||
generate_maze(Display *dpy, Window w, GC gc, maze_t *m, vec2_t p, vec2_t g)
|
||||
{
|
||||
m->cells[x][y] = true;
|
||||
m->cells[p.x][p.y] = true;
|
||||
|
||||
draw_maze(dpy, w, gc, m);
|
||||
nanosleep(&pause, NULL);
|
||||
|
||||
if (GOAL == x && GOAL == y)
|
||||
if (p.x == g.x && p.y == g.y)
|
||||
return;
|
||||
|
||||
dir_t visit[] = { LEFT, RIGHT, UP, DOWN };
|
||||
@ -94,18 +99,19 @@ generate_maze(Display *dpy, Window w, GC gc, maze_t *m, int x, int y)
|
||||
visit[i] = tmp;
|
||||
}
|
||||
|
||||
vec2_t n;
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
const int xp = x + steps[visit[i]].x;
|
||||
const int yp = y + steps[visit[i]].y;
|
||||
n.x = p.x + steps[visit[i]].x;
|
||||
n.y = p.y + steps[visit[i]].y;
|
||||
|
||||
const bool x_in_bounds = xp >= 0 && xp < GRID_SIZE;
|
||||
const bool y_in_bounds = yp >= 0 && yp < GRID_SIZE;
|
||||
if (x_in_bounds && y_in_bounds && !m->cells[xp][yp]) {
|
||||
const int xi = (x + xp) / 2;
|
||||
const int yi = (y + yp) / 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(dpy, w, gc, m, xp, yp);
|
||||
generate_maze(dpy, w, gc, m, n, g);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -142,7 +148,9 @@ int main(void)
|
||||
// Generate and draw maze
|
||||
maze_t m;
|
||||
memset(&m, 0, sizeof(maze_t));
|
||||
generate_maze(dpy, w, gc, &m, 0, 0);
|
||||
draw_walls(dpy, w, gc);
|
||||
const vec2_t start = { GOAL, GOAL }, end = { 0, 0 };
|
||||
generate_maze(dpy, w, gc, &m, start, end);
|
||||
|
||||
// Wait for window exit
|
||||
bool is_del = false;
|
||||
|
Loading…
x
Reference in New Issue
Block a user