```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ``` ```/** * Simple maze by randomized DFS. * * Wall is "1", corridor is "0". Code relies heavily on various things no one * should ever rely on. It's more fun to write it this way. * * (c) L. Diener 2012 */ #include #include #include #define MAZE_HEIGHT 50 #define MAZE_WIDTH 50 int maze[MAZE_WIDTH][MAZE_HEIGHT]; // "Can I place a corridor tile at (x,y) when coming from (fromx, fromy)?" int canPlaceCorridor(int x, int y, int fromx, int fromy) { // The edge of the maze is all walls (helps avoid special cases, too) if(x <= 0 || x >= MAZE_WIDTH-1 || y <= 0 || y >= MAZE_HEIGHT-1) { return 0; } // Of all tiles around (x,y) that are not in the direction of (fromx, // fromy), none can be a corridor. Also, (x,y) itself should be a wall. int corridorCount = 0; for(int xd = -(fromx >= x); xd <= (fromx <= x); xd++) { for(int yd = -(fromy >= y); yd <= (fromy <= y); yd++) { corridorCount += !maze[x+xd][y+yd]; } } return (corridorCount == 0); } // Build maze by recursion. void buildMazeRecursive(int x, int y) { int triedDir = 0; while(triedDir != 15) { int dir = rand() % 4; int xd = (dir < 2) ? 0 : dir == 2 ? -1 : 1; int yd = (dir >= 2) ? 0 : dir == 0 ? -1 : 1; if((triedDir & (1 << dir)) == 0) { triedDir = triedDir | (1 << dir); if(canPlaceCorridor(x+xd,y+yd,x,y)) { maze[x+xd][y+yd] = 0; buildMazeRecursive(x+xd,y+yd); } } } return; } // Set the whole maze to walls. void clearMaze() { for(int x = 0; x < MAZE_WIDTH; x++ ) { for(int y = 0; y < MAZE_HEIGHT; y++ ) { maze[x][y] = 1; } } } // Build maze, create exit and entry points. void buildMaze() { // Start fresh. clearMaze(); // Randomize. srand(time(0)); // Actually build maze, starting from tile (0,1) buildMazeRecursive(0,1); // Make tile (0, 1) te entry point. maze[0][1] = 0; // Make the first tile that fits in the right wall the exit point. for(int y = MAZE_HEIGHT - 1; y > 0; y--) { if(maze[MAZE_WIDTH-2][y] == 0) { maze[MAZE_WIDTH-1][y] = 0; break; } } } // Print out the maze, # is wall, space is free void printMaze() { for(int y = 0; y < MAZE_HEIGHT; y++ ) { for(int x = 0; x < MAZE_WIDTH; x++ ) { printf("%c", " #"[maze[x][y]]); } printf("\n"); } } int main(int argc, char** argv) { buildMaze(); printMaze(); } ```
 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 ``` ```################################################## # # # ## ## ## # # # # # ## ## ### # ## # # ## ## # # # # ## # # ###### ## #### # ## ##### # ### # ### ### # # # ### ## # # ## # # # ##### ## ### # # # ## # # ## ### ###### ## ### # # # # ###### ## ## # #### #### # # ### ## # # ## # ### # # ### # # ###### ## ## # ### # # ### #### ## ###### # # # # ## # ## # # ## ### ### ## # # # # ## ## # # # # # # ## ### # ###### # ### # # # ## # ### ## ## # # ## ## # # ## # # # # ### # ## # ## #### ######## ## ## # ##### ######## # ##### ## ## # # ### ##### # ### # # ## ## # # ## # ## # ##### # # # # # # ## # #### ## ## ### ###### ## # # ## ## # # # # # # # ### ## # # ### #### #### # ## #### # ### ### # # ## # ## ## ## # # ## ## # # #### # ## ### #### ## # # ## ## # ##### ### # # # ### #### # #### # ####### # ## ### ## # ### # ###### ## # # # # ### # # # # ### # #### ### # # ## ## # ## ### # ####### # ### # # ### # # # ### ### # ## # # # ## # # ## ## ####### ## #### # ##### # ###### ######## # # ## #### # ## # ##### # ## ## # ### ### ## ## ## ## # # ## # ##### # # # #### ## #### # ### # # ### #### ## ### # # # ## # # # ## # ##### ### #### # # # # #### ## ## # # # # # ## ### ####### # # # ## #### # # ## # ### # ## # ## ## # # # #### ### # ### # ### # # # ## # # ## # # # # ## ### ### ### ## # ## # # # # # # ## # # ## # # # # # ### ### ###### # ## ## #### ## ##### ### # # ## ## # ## # ## # # # # # # # ## # # ### # ## # #### # ##### ## ## ## #### ## ## ###### ## ## # # # ## ### ## ## ## ## # # ## # ## # # ##### ## ## ### ### ######### ### ## # # ## # # ### ## # ## ## ## ##### ## # # ### ## # ##### ## # #### ## ## # # # ###### # # # #### ###### ## # ### # # # # ### ### ## ## #### ###### ## ## # # # ## # ## # #### # # ## # # ### ### # #### ##### ######### ###### # ### # # ## # # ## ## # # # ###### ### # # ####### ## ### # ### # ## # ### ### # ## # # ## ## ## ###### # ## # # # # ## # ## # # # ## ## ## # ################################################## ```