/* Alan Wood 5th July 1993 ** Ported to Coherent 6th Jan 1995 - Alan Wood ** Program displays output using IBM graphics blocks instead of ** true graphics. ** Output can be piped to a printer capable of producing IBM character ** set eg maze | lpr -B. ** ** To compile: cc maze.c -o maze and don't forget chmod +x maze. ** ** Program to generate mazes using my algorithm. ** A path is started from a random location and continues until the path ** becomes blocked in. Subsequent paths are started from locations that ** have already been visited and have unvisited locations adjacent. This ** procedure continues until all locations have been visited. ** Start and end points are added last. ** ** Note that in this text version, both x and y dimensions must be odd. ** If even numbers are entered, they are decremented. */ #include #include #define WALL 1 #define PATH 0 #define TRUE 1 #define FALSE 0 #define maze(x,y) mazep[(y) * width + x] int width; int height; int *mazep; int path[4]; int num_of_paths; int way_to_go; char ch; char buf[10]; main() { do{ fprintf(stderr, "X(5->):"); width = atoi(gets(buf)); if(!(width & 1)) width--; }while (width < 4); do{ fprintf(stderr, "Y(5->):"); height = atoi(gets(buf)); if(!(height & 1)) height--; } while (height < 4); mazep = (int *) malloc(width * height * sizeof(int)); if (mazep == NULL) { fprintf(stderr, "Unable to create maze\n"); exit(1); } make_maze(); printthemaze(); return; } make_maze() { int x,y; int oldx, oldy; int path_flag; int scan_flag; for (x=0; x 0) { way_to_go = path[rand() % (num_of_paths)]; switch (way_to_go) { case 0 : maze(x,y) = PATH;y--; maze(x,y) = PATH;y--; maze(x,y) = PATH; break; case 1 : maze(x,y) = PATH;x++; maze(x,y) = PATH;x++; maze(x,y) = PATH; break; case 2 : maze(x,y) = PATH;y++; maze(x,y) = PATH;y++; maze(x,y) = PATH; break; case 3 : maze(x,y) = PATH;x--; maze(x,y) = PATH;x--; maze(x,y) = PATH; break; default : break; } } else { path_flag = TRUE; scan_flag = TRUE; oldx=x; oldy=y; do{ x+=2; if (x>width-1) { x=1; y+=2; if (y>height-1) y=1; } if((x==oldx) && (y==oldy)) scan_flag = FALSE; if(maze(x,y)==PATH) find_path(x,y); if ((maze(x,y) == PATH) && (num_of_paths > 0)) path_flag = FALSE; }while(path_flag && scan_flag); } } while(scan_flag); /* add entrance and exit points */ maze(0,1) = PATH; maze((width-1),(height-2)) = PATH; return; } find_path(x,y) int x; int y; { num_of_paths = 0; if ((y-2) > 0) if ((maze(x,y-2) & maze(x,y-1)) == 1) { path[num_of_paths] = 0; num_of_paths++; } if ((x+2) < (width-1)) if ((maze(x+2,y) & maze(x+1,y)) == 1) { path[num_of_paths] = 1; num_of_paths++; } if ((y+2) < (height-1)) if ((maze(x,y+2) & maze(x,y+1)) == 1) { path[num_of_paths] = 2; num_of_paths++; } if ((x-2) > 0) if ((maze(x-2,y) & maze(x-1,y)) == 1) { path[num_of_paths] = 3; num_of_paths++; } return; } printthemaze() { int x,y; for(y=0;y