/* The function init_maze has been improved and should return a maze with a solution */ /* The current algorithm uses a self-avoiding random walk. A better option may be */ /* to give random weights to the dual graph, and finite a maximal spanning tree */ /* Change constant RAND_SHIFT to change the maze */ #define MAZE_TYPE_SQUARE 0 /* maze with square cells */ #define MAZE_TYPE_CIRCLE 1 /* circular maze */ #define MAZE_TYPE_HEX 2 /* honeycomb maze */ #define MAZE_TYPE_OCT 3 /* maze with octagonal and square cells */ typedef struct { short int nneighb; /* number of neighbours */ int neighb[MAZE_MAX_NGBH]; /* neighbour cells */ short int directions[MAZE_MAX_NGBH]; /* direction of neighbours */ short int north, east, south, west; /* closed walls */ short int northeast, northwest, southeast, southwest; /* closed walls */ short int active; /* takes value 1 if currently active in RW path */ short int tested; /* takes value 1 if tested */ short int connected; /* takes value 1 if connected to exit */ short int closed; /* takes value 1 if no untested neighbours */ } t_maze; int nmaze(int i, int j) { return(NXMAZE*j + i); } void init_maze_graph(t_maze maze[NXMAZE*NYMAZE]) { int i, j, k, n; printf("Initializing maze\n"); /* initialize neighbours */ /* in the bulk */ for (i=1; i 0) printf("Cell (%i, %i)\n", i, j); // for (k = 0; k 0) printf("Cell (%i, %i)\n", i, j); // for (k = 0; k 0)&&(!maze[n].tested)) while ((npaths > 0)) { maze[n].active = 1; printf("Cell (%i, %i) ", n%NXMAZE, n/NXMAZE); nnext = 0; for (i=0; i pathlength) j = 0; printf("j = %i\n", j); // while (deadend) if (!maze[path[j]].connected) deadend = find_maze_path(maze, path[j], newpath, &newpathlength, type); if (!deadend) for (n=0; n 0) // { // printf("Cell (%i, %i)\t", i, j); // if (maze[n].north) printf("North "); // if (maze[n].northeast) printf("N-E "); // if (maze[n].east) printf("East "); // if (maze[n].south) printf("South "); // if (maze[n].west) printf("West "); // printf("\n"); // } // } // sleep(5); } void init_hex_maze(t_maze maze[NXMAZE*NYMAZE]) /* init a maze with hexagonal cells */ { init_maze_oftype(maze, MAZE_TYPE_HEX); } void init_oct_maze(t_maze maze[NXMAZE*NYMAZE]) /* init a maze with hexagonal cells */ { init_maze_oftype(maze, MAZE_TYPE_OCT); } void init_maze_exit(int nx, int ny, t_maze maze[NXMAZE*NYMAZE]) /* init a maze with exit at (nx, ny) */ { int i, j, n, deadend, pathlength, newpathlength; int *path, *newpath; path = (int *)malloc(2*NXMAZE*NYMAZE*sizeof(short int)); newpath = (int *)malloc(2*NXMAZE*NYMAZE*sizeof(short int)); init_maze_graph(maze); for (i=0; i pathlength) j = 0; printf("j = %i\n", j); // while (deadend) if (!maze[path[j]].connected) deadend = find_maze_path(maze, path[j], newpath, &newpathlength, 0); if (!deadend) for (n=0; n