/* Maze Generator Copyright (x) 1995 DJ Delorie Permission granted to use this source for any purpose, provided this copyright notice remains present and intact. */ #include #include #include #include #include #include #include int print_mime = 1; int solve = 0; int html = 0; #define GIF_GREY 0xcc void print_form() { char *accept = getenv("HTTP_ACCEPT"); printf("Content-type: text/html\n\n"); system("header Maze Generator"); printf("
\n"); printf("Number of cells across (1..N):\n"); printf("
\n"); printf("Number of cells up/down (1..N):\n"); printf("
\n"); printf("Type of maze:
\n"); printf("Width of each cell (2..N):\n"); printf("
\n"); printf("Height of each cell (2..N):\n"); printf("
\n"); printf("Random Number Seed (optional):\n"); printf("
\n"); printf("\"[X]\"

\n"); printf("

\n"); system("trailer"); } #define GO_LEFT 1 #define GO_RIGHT 2 #define GO_UP 4 #define GO_DOWN 8 #define USED 16 #define SOLUTION 32 #define M(x,y) maze_data[(x)+1+((y)+1)*(c+2)] #define dprintf if(0)printf void generate_maze(int gif, int c, int r, int w, int h, int s) { char *maze_data; int x, y; int *xt, *yt; if (c < 1 || r < 1 || w < 1 || h < 1) return; maze_data = (char *)calloc(r+2, c+2); xt = (int *)malloc(c*r*sizeof(int)); yt = (int *)malloc(c*r*sizeof(int)); for (x=0; x0; x--) { y = random() % (x+1); if (y!=x) { int t = xt[x]; xt[x] = xt[y]; xt[y] = t; t = yt[x]; yt[x] = yt[y]; yt[y] = t; } } M(-1,0) |= USED | GO_RIGHT; M(0,0) |= USED | GO_LEFT; int num_left = r*c-1; while (num_left) for (x=0; x\n"); for (y=0; y<=r; y++) { putchar('+'); for (x=0; x\n"); } else { // 0 = lines GIF_GREY = blank 255 = solution int i; FILE *trans = popen("/usr/bin/ppmtogif 2>/dev/null", "w"); fprintf(trans, "P5\n%d %d %d\n", (c+2)*w+1, (r+2)*h+1, 255); for (i=0; i<((c+2)*w+1) * h; i++) fputc(GIF_GREY, trans); for (y=0; y<=r; y++) { for (i=0; i 2) c = atoi(argv[2]); if (argc > 3) r = atoi(argv[3]); if (argc > 4) w = atoi(argv[4]); if (argc > 5) h = atoi(argv[5]); if (argc > 6) s = atoi(argv[6]); if (s == 0) s = (int)time(0); if (html) { printf("Content-type: text/html\n\n"); if (solve) system("header Maze Solution"); else system("header Generated Maze"); if (strcmp(argv[1], "text") == 0) { print_mime = 0; printf("
"); generate_maze(0, c, r, w, h, s); printf("
"); } else printf("

\n", solve?"solve+":"", c, r, w, h, s); if (!solve) printf("

Solution\"[X]\"

\n", gif?"gif":"text", c, r, w, h, s); system("trailer -p genmaze.cgi"); } else { if (gif) printf("Content-type: image/gif\n\n"); else printf("Content-type: text/html\n\n"); generate_maze(gif, c, r, w, h, s); } } int main(int argc, char **argv) { setbuf(stdout, 0); if (setpriority(PRIO_PROCESS, 0, 20)) exit(1); if (strcasecmp(getenv("REQUEST_METHOD")?:"", "post")) { if (argc == 1) { print_form(); return 0; } else { while (1) { if (strcmp(argv[1], "solve") == 0) solve = 1; else if (strcmp(argv[1], "html") == 0) html = 1; else break; argc--; argv++; } generate_maze(argc, argv); return 0; } } printf("Content-type: text/html\n\n"); int cols=10, rows=10, cwidth=0, cheight=0, seed=(int)time(0); char *type = "text"; char name[100], val[100], *cp=name, *end=name+sizeof(name)-1; int cl = atoi(getenv("CONTENT_LENGTH")?:"0"), i, ch; for (i=0; ; i++) { if (i < cl) ch = getc(stdin); else ch = 0; if (ch == '=') { *cp = 0; cp = val; end = val+sizeof(val)-1; } else if (ch == '&' || ch == 0) { *cp = 0; cp = name; end = name+sizeof(name)-1; if (strcmp(name, "cols") == 0) cols = atoi(val); else if (strcmp(name, "rows") == 0 && val[0]) rows = atoi(val); else if (strcmp(name, "type") == 0 && val[0]) type = strdup(val); else if (strcmp(name, "width") == 0 && val[0]) cwidth = atoi(val); else if (strcmp(name, "height") == 0 && val[0]) cheight = atoi(val); else if (strcmp(name, "seed") == 0 && val[0]) seed = atoi(val); if (ch == 0) break; } else { *cp++ = ch; if (cp >= end) cp--; } } if (cwidth == 0) if (strcmp(type, "text") == 0) cwidth = 3; else cwidth = 10; if (cheight == 0) if (strcmp(type, "text") == 0) cheight = 2; else cheight = 10; if (cols < 1 || cols > 100 || rows < 1 || rows > 100 || cwidth < 2 || cwidth > 30 || cheight < 2 || cheight > 30) { printf("Invalid parameters. cells 1..100, cell size 2..30\n"); return 0; } if (cols*cwidth > 600 || rows * cheight > 600) { printf("Invalid parameters. Image cannot exceed 600x600\n"); return 0; } if (seed == 0) seed = (int)time(0); system("header Generated Maze"); if (strcmp(type, "text") == 0) { print_mime = 0; printf("
"); generate_maze(0, cols, rows, cwidth, cheight, seed); printf("
"); } else printf("

\n", cols, rows, cwidth, cheight, seed); printf("

Solution\"[X]\"

\n", type, cols, rows, cwidth, cheight, seed); system("trailer -p genmaze.cgi"); return 0; }