Support non-square mazes
This commit is contained in:
parent
15fd1e3405
commit
e57b83d97a
58
main.c
58
main.c
@ -14,19 +14,23 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#define MAZE_SIZE 100
|
#define MAZE_WIDTH 100
|
||||||
|
#define MAZE_HEIGHT 100
|
||||||
|
|
||||||
#define GRID_SIZE (2 * MAZE_SIZE - 1)
|
#define MARGIN_X 2
|
||||||
#define MARGIN 2
|
#define MARGIN_Y 2
|
||||||
#define WALL_THICKNESS 1
|
#define WALL_THICKNESS 1
|
||||||
#define WINDOW_SIZE (GRID_SIZE + 2 * (WALL_THICKNESS + MARGIN))
|
|
||||||
|
|
||||||
#define PX(x) ((x) << 2)
|
#define PX(x) ((x) << 2)
|
||||||
|
|
||||||
#define GOAL (GRID_SIZE - 1)
|
#define GRID_WIDTH (2 * MAZE_WIDTH - 1)
|
||||||
#define MAX_PATH_LENGTH (MAZE_SIZE * MAZE_SIZE)
|
#define GRID_HEIGHT (2 * MAZE_HEIGHT - 1)
|
||||||
|
#define WINDOW_WIDTH (GRID_WIDTH + 2 * (WALL_THICKNESS + MARGIN_X))
|
||||||
|
#define WINDOW_HEIGHT (GRID_HEIGHT + 2 * (WALL_THICKNESS + MARGIN_Y))
|
||||||
|
#define GOAL_X (GRID_WIDTH - 1)
|
||||||
|
#define GOAL_Y (GRID_HEIGHT - 1)
|
||||||
|
|
||||||
#define STACK_SIZE (MAZE_SIZE * MAZE_SIZE)
|
#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;
|
||||||
|
|
||||||
@ -55,14 +59,15 @@ static const vec2_t steps[] = {
|
|||||||
static Display *dpy;
|
static Display *dpy;
|
||||||
static Window window;
|
static Window window;
|
||||||
static GC ctx;
|
static GC ctx;
|
||||||
static cell_t maze[GRID_SIZE][GRID_SIZE];
|
static cell_t maze[GRID_WIDTH][GRID_HEIGHT];
|
||||||
static int bg_col, wall_col, visited_col;
|
static int bg_col, wall_col, visited_col;
|
||||||
|
|
||||||
static void clear_path(vec2_t p)
|
static void clear_path(vec2_t p)
|
||||||
{
|
{
|
||||||
const int margin_px = PX(MARGIN + WALL_THICKNESS);
|
const int margin_x_px = PX(MARGIN_X + WALL_THICKNESS);
|
||||||
const int left = margin_px + PX(p.x);
|
const int margin_y_px = PX(MARGIN_Y + WALL_THICKNESS);
|
||||||
const int top = margin_px + PX(p.y);
|
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));
|
XFillRectangle(dpy, window, ctx, left, top, PX(1), PX(1));
|
||||||
XClearArea(dpy, window, left, top, PX(1), PX(1), false);
|
XClearArea(dpy, window, left, top, PX(1), PX(1), false);
|
||||||
XFlush(dpy);
|
XFlush(dpy);
|
||||||
@ -70,17 +75,18 @@ static void clear_path(vec2_t p)
|
|||||||
|
|
||||||
static void draw_visited(vec2_t p)
|
static void draw_visited(vec2_t p)
|
||||||
{
|
{
|
||||||
const int margin_px = PX(MARGIN + WALL_THICKNESS);
|
const int margin_x_px = PX(MARGIN_X + WALL_THICKNESS);
|
||||||
const int left = margin_px + PX(p.x);
|
const int margin_y_px = PX(MARGIN_Y + WALL_THICKNESS);
|
||||||
const int top = margin_px + PX(p.y);
|
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));
|
XFillRectangle(dpy, window, ctx, left, top, PX(1), PX(1));
|
||||||
XFlush(dpy);
|
XFlush(dpy);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool in_bounds(vec2_t p)
|
static bool in_bounds(vec2_t p)
|
||||||
{
|
{
|
||||||
const bool valid_x = p.x >= 0 && p.x < GRID_SIZE;
|
const bool valid_x = p.x >= 0 && p.x < GRID_WIDTH;
|
||||||
const bool valid_y = p.y >= 0 && p.y < GRID_SIZE;
|
const bool valid_y = p.y >= 0 && p.y < GRID_HEIGHT;
|
||||||
return valid_x && valid_y;
|
return valid_x && valid_y;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,7 +137,7 @@ static void solve(vec2_t p, vec2_t *sp, vec2_t **end)
|
|||||||
{
|
{
|
||||||
*sp++ = p;
|
*sp++ = p;
|
||||||
while (1) {
|
while (1) {
|
||||||
if (GOAL == p.x && GOAL == p.y) {
|
if (GOAL_X == p.x && GOAL_Y == p.y) {
|
||||||
*sp++ = p;
|
*sp++ = p;
|
||||||
*end = sp;
|
*end = sp;
|
||||||
return;
|
return;
|
||||||
@ -180,8 +186,8 @@ int main(void)
|
|||||||
wall_col = BlackPixel(dpy, DefaultScreen(dpy));
|
wall_col = BlackPixel(dpy, DefaultScreen(dpy));
|
||||||
bg_col = WhitePixel(dpy, DefaultScreen(dpy));
|
bg_col = 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, bg_col, bg_col);
|
PX(WINDOW_HEIGHT), 0, bg_col, bg_col);
|
||||||
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));
|
||||||
@ -205,16 +211,16 @@ int main(void)
|
|||||||
XClearWindow(dpy, window);
|
XClearWindow(dpy, window);
|
||||||
XSetForeground(dpy, ctx, wall_col);
|
XSetForeground(dpy, ctx, wall_col);
|
||||||
XFillRectangle(
|
XFillRectangle(
|
||||||
dpy, window, ctx, PX(MARGIN), PX(MARGIN), PX(GRID_SIZE + 2),
|
dpy, window, ctx, PX(MARGIN_X), PX(MARGIN_Y), PX(GRID_WIDTH + 2),
|
||||||
PX(GRID_SIZE + 2));
|
PX(GRID_HEIGHT + 2));
|
||||||
const vec2_t exit = { GOAL + 1, GOAL };
|
const vec2_t exit = { GOAL_X + 1, GOAL_Y };
|
||||||
clear_path(exit);
|
clear_path(exit);
|
||||||
XFlush(dpy);
|
XFlush(dpy);
|
||||||
|
|
||||||
// Generate
|
// Generate
|
||||||
memset(&maze, 0, sizeof(maze));
|
memset(&maze, 0, sizeof(maze));
|
||||||
const vec2_t gen_start = { GOAL, GOAL };
|
const vec2_t gen_start = { GOAL_X, GOAL_Y };
|
||||||
maze[GOAL][GOAL].is_path = true;
|
maze[GOAL_X][GOAL_Y].is_path = true;
|
||||||
clear_path(gen_start);
|
clear_path(gen_start);
|
||||||
generate(gen_start);
|
generate(gen_start);
|
||||||
|
|
||||||
@ -224,7 +230,7 @@ int main(void)
|
|||||||
maze[0][0].visited = true;
|
maze[0][0].visited = true;
|
||||||
solve(solve_start, path, &path_end);
|
solve(solve_start, path, &path_end);
|
||||||
|
|
||||||
sleep(1);
|
// sleep(1);
|
||||||
|
|
||||||
// Draw solution path
|
// Draw solution path
|
||||||
XSetForeground(dpy, ctx, visited_col);
|
XSetForeground(dpy, ctx, visited_col);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user