diff --git a/main.c b/main.c index 6c4ea4f..57feaa0 100644 --- a/main.c +++ b/main.c @@ -14,19 +14,23 @@ #include #include -#define MAZE_SIZE 100 +#define MAZE_WIDTH 100 +#define MAZE_HEIGHT 100 -#define GRID_SIZE (2 * MAZE_SIZE - 1) -#define MARGIN 2 +#define MARGIN_X 2 +#define MARGIN_Y 2 #define WALL_THICKNESS 1 -#define WINDOW_SIZE (GRID_SIZE + 2 * (WALL_THICKNESS + MARGIN)) - #define PX(x) ((x) << 2) -#define GOAL (GRID_SIZE - 1) -#define MAX_PATH_LENGTH (MAZE_SIZE * MAZE_SIZE) +#define GRID_WIDTH (2 * MAZE_WIDTH - 1) +#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; @@ -55,14 +59,15 @@ static const vec2_t steps[] = { static Display *dpy; static Window window; 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 void clear_path(vec2_t p) { - const int margin_px = PX(MARGIN + WALL_THICKNESS); - const int left = margin_px + PX(p.x); - const int top = margin_px + PX(p.y); + const int margin_x_px = PX(MARGIN_X + WALL_THICKNESS); + const int margin_y_px = PX(MARGIN_Y + WALL_THICKNESS); + 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); @@ -70,17 +75,18 @@ static void clear_path(vec2_t p) static void draw_visited(vec2_t p) { - const int margin_px = PX(MARGIN + WALL_THICKNESS); - const int left = margin_px + PX(p.x); - const int top = margin_px + PX(p.y); + const int margin_x_px = PX(MARGIN_X + WALL_THICKNESS); + const int margin_y_px = PX(MARGIN_Y + WALL_THICKNESS); + 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) { - const bool valid_x = p.x >= 0 && p.x < GRID_SIZE; - const bool valid_y = p.y >= 0 && p.y < GRID_SIZE; + const bool valid_x = p.x >= 0 && p.x < GRID_WIDTH; + const bool valid_y = p.y >= 0 && p.y < GRID_HEIGHT; return valid_x && valid_y; } @@ -131,7 +137,7 @@ static void solve(vec2_t p, vec2_t *sp, vec2_t **end) { *sp++ = p; while (1) { - if (GOAL == p.x && GOAL == p.y) { + if (GOAL_X == p.x && GOAL_Y == p.y) { *sp++ = p; *end = sp; return; @@ -180,8 +186,8 @@ int main(void) wall_col = BlackPixel(dpy, DefaultScreen(dpy)); bg_col = WhitePixel(dpy, DefaultScreen(dpy)); window = XCreateSimpleWindow( - dpy, DefaultRootWindow(dpy), 0, 0, PX(WINDOW_SIZE), PX(WINDOW_SIZE), - 0, bg_col, bg_col); + dpy, DefaultRootWindow(dpy), 0, 0, PX(WINDOW_WIDTH), + PX(WINDOW_HEIGHT), 0, bg_col, bg_col); Atom del = XInternAtom(dpy, "WM_DELETE_WINDOW", false); XSetWMProtocols(dpy, window, &del, 1); ctx = DefaultGC(dpy, DefaultScreen(dpy)); @@ -205,16 +211,16 @@ int main(void) XClearWindow(dpy, window); XSetForeground(dpy, ctx, wall_col); XFillRectangle( - dpy, window, ctx, PX(MARGIN), PX(MARGIN), PX(GRID_SIZE + 2), - PX(GRID_SIZE + 2)); - const vec2_t exit = { GOAL + 1, GOAL }; + 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); // Generate memset(&maze, 0, sizeof(maze)); - const vec2_t gen_start = { GOAL, GOAL }; - maze[GOAL][GOAL].is_path = true; + const vec2_t gen_start = { GOAL_X, GOAL_Y }; + maze[GOAL_X][GOAL_Y].is_path = true; clear_path(gen_start); generate(gen_start); @@ -224,7 +230,7 @@ int main(void) maze[0][0].visited = true; solve(solve_start, path, &path_end); - sleep(1); + // sleep(1); // Draw solution path XSetForeground(dpy, ctx, visited_col);