Draw maze while generating it
This commit is contained in:
parent
e55ae6f355
commit
af9106bae5
82
main.c
82
main.c
@ -3,12 +3,15 @@
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
#define _POSIX_C_SOURCE 199309L
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
|
||||
#define MAZE_SIZE 24
|
||||
|
||||
@ -31,6 +34,8 @@ typedef struct {
|
||||
int x, y;
|
||||
} vec2_t;
|
||||
|
||||
static const struct timespec pause = { .tv_nsec = 5000000 };
|
||||
|
||||
static const vec2_t steps[] = {
|
||||
[LEFT] = { .x = -2, .y = 0 },
|
||||
[RIGHT] = { .x = 2, .y = 0 },
|
||||
@ -38,44 +43,10 @@ static const vec2_t steps[] = {
|
||||
[DOWN] = { .x = 0, .y = 2 },
|
||||
};
|
||||
|
||||
static void generate_step(maze_t *m, int x, int y)
|
||||
{
|
||||
m->cells[x][y] = true;
|
||||
if (GOAL == x && GOAL == 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;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
const int xp = x + steps[visit[i]].x;
|
||||
const int yp = 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;
|
||||
m->cells[xi][yi] = true;
|
||||
|
||||
generate_step(m, xp, yp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void generate_maze(maze_t *m)
|
||||
{
|
||||
memset(m, 0, sizeof(maze_t));
|
||||
generate_step(m, 0, 0);
|
||||
}
|
||||
|
||||
static void draw_maze(Display *dpy, Window w, GC gc, const maze_t *m)
|
||||
{
|
||||
XClearWindow(dpy, w);
|
||||
|
||||
// Draw walls
|
||||
XFillRectangle(
|
||||
dpy, w, gc, PX(MARGIN), PX(MARGIN), PX(GRID_SIZE + 2),
|
||||
@ -104,6 +75,41 @@ static void draw_maze(Display *dpy, Window w, GC gc, const maze_t *m)
|
||||
XFlush(dpy);
|
||||
}
|
||||
|
||||
static void
|
||||
generate_maze(Display *dpy, Window w, GC gc, maze_t *m, int x, int y)
|
||||
{
|
||||
m->cells[x][y] = true;
|
||||
|
||||
draw_maze(dpy, w, gc, m);
|
||||
nanosleep(&pause, NULL);
|
||||
|
||||
if (GOAL == x && GOAL == 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;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
const int xp = x + steps[visit[i]].x;
|
||||
const int yp = 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;
|
||||
m->cells[xi][yi] = true;
|
||||
|
||||
generate_maze(dpy, w, gc, m, xp, yp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Seed random number generation from time
|
||||
@ -135,8 +141,8 @@ int main(void)
|
||||
|
||||
// Generate and draw maze
|
||||
maze_t m;
|
||||
generate_maze(&m);
|
||||
draw_maze(dpy, w, gc, &m);
|
||||
memset(&m, 0, sizeof(maze_t));
|
||||
generate_maze(dpy, w, gc, &m, 0, 0);
|
||||
|
||||
// Wait for window exit
|
||||
bool is_del = false;
|
||||
|
Loading…
x
Reference in New Issue
Block a user