maze3.c (5288B)
1 /* Exercise 7.27 */ 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <time.h> 6 7 #define ROWS 19 8 #define COLS 19 9 10 #define LOOPS ROWS * COLS 11 12 #define RIGHT 1 13 #define DOWN 2 14 #define LEFT 3 15 #define UP 4 16 17 void mazeTraverse(char maze[][COLS], int r, int c, int ret[], int route); 18 void mazeGenerator(char maze[][COLS], int ret[]); 19 20 void pmaze(char maze[][COLS]); 21 22 void go(char maze[][COLS], int *r, int *c, int route); 23 24 int ahead(char maze[][COLS], int *r, int *c, int route, int check); 25 int toright(char maze[][COLS], int r, int c, int route); 26 int chroute(int route, int nroute); 27 28 int main(void) 29 { 30 char maze[ROWS][COLS]; 31 int ret[4] = { 0 }, route; 32 33 srand( time(NULL) ); 34 35 mazeGenerator(maze, ret); 36 37 /* Set the route */ 38 if(!ret[0]) route = DOWN; 39 else if(ret[0] == ROWS - 1) route = UP; 40 else if(!ret[1]) route = RIGHT; 41 else if(ret[1] == COLS - 1) route = LEFT; 42 43 mazeTraverse(maze, ret[0], ret[1], ret, route); 44 45 return 0; 46 } /* E0F main */ 47 48 /* Generate a maze and put it to the "maze" matrix */ 49 void mazeGenerator(char maze[][COLS], int ret[]) 50 { 51 int row, col, i = 0; 52 53 /* Inizialize the maze */ 54 for(row = 0; row < ROWS; row++) 55 for(col = 0; col < COLS; col++) 56 maze[row][col] = '#'; 57 58 row = rand() % ROWS; 59 col = !row || row == ROWS-1 ? 1 + rand()%(COLS-2) : rand()%2 ? COLS-1 : 0; 60 61 ret[0] = row; 62 ret[1] = col; 63 64 if(!ret[1] || ret[1] == COLS - 1) { 65 ret[2] = rand() % 2 ? 0 : ROWS - 1; 66 ret[3] = 1 + rand() % (COLS - 2); 67 } 68 else { 69 ret[2] = 1 + rand() % (ROWS - 2); 70 ret[3] = rand() % 2 ? 0 : COLS - 1; 71 } 72 73 maze[ret[0]][ret[1]] = '.'; /* Start point */ 74 maze[ret[2]][ret[3]] = 'E'; /* End point */ 75 76 /* Create the path */ 77 while(i++ < LOOPS) { 78 row = 1 + rand() % (ROWS - 2); 79 col = 1 + rand() % (COLS - 2); 80 81 maze[row][col] = '.'; 82 } 83 84 } /* eof mazeGenerator() */ 85 86 /* Traverse a maze founding the exit, if exists */ 87 void mazeTraverse(char maze[][COLS], int r, int c, int ret[], int route) 88 { 89 static int print = 1; 90 91 maze[r][c] = (maze[r][c] != 'x' && maze[r][c] != 'X') ? 'x' : 'X'; 92 93 if(print) { /* Don't show the route changes */ 94 printf("\n"); 95 pmaze(maze); 96 } 97 98 if(ahead(maze, &r, &c, route, 1)) { 99 if(toright(maze, r, c, route)) { 100 101 /* A little fix */ 102 maze[r][c] = maze[r][c] == 'x' ? '.' : 'X'; 103 104 route = chroute(route, LEFT); 105 print = 0; 106 } 107 else print = 1; 108 } 109 else { 110 if(!toright(maze, r, c, route)) { 111 route = chroute(route, RIGHT); 112 } 113 else print = 1; 114 } 115 116 if(r == ret[0] && c == ret[1]) { 117 printf("Closed maze!\n"); 118 return; 119 } 120 else if(r == ret[2] && c == ret[3]) { 121 printf("Exit found: row %d, col %d.\n", ret[2], ret[3]); 122 return; 123 } 124 else 125 mazeTraverse(maze, r, c, ret, route); 126 127 } /* eof mazeTraverse() */ 128 129 /* Print a maze */ 130 void pmaze(char maze[][COLS]) 131 { 132 int i, j; 133 134 for(i = 0; i < ROWS; i++) { 135 for(j = 0; j < COLS; j++) { 136 printf("%c ", maze[i][j]); 137 } 138 printf("\n"); 139 } 140 141 } /* eof pmaze() */ 142 143 /* Ehm.. go ahead :-) */ 144 int ahead(char maze[][COLS], int *r, int *c, int route, int check) 145 { 146 switch(route) 147 { 148 case RIGHT: 149 if(maze[*r][*c + 1] == '#' && check) 150 return 1; 151 else 152 ++*c; 153 break; 154 case DOWN: 155 if(maze[*r + 1][*c] == '#' && check) 156 return 1; 157 else 158 ++*r; 159 break; 160 case LEFT: 161 if(maze[*r][*c - 1] == '#' && check) 162 return 1; 163 else 164 --*c; 165 break; 166 case UP: 167 if(maze[*r - 1][*c] == '#' && check) 168 return 1; 169 else 170 --*r; 171 break; 172 default: 173 printf("Where are you going?\n"); 174 exit(-1); 175 } 176 177 return 0; 178 } /* eof ahead() */ 179 180 /* Check if the wall is to the right side */ 181 int toright(char maze[][COLS], int r, int c, int route) 182 { 183 switch(route) 184 { 185 case RIGHT: 186 if(maze[r + 1][c] == '#') 187 return 1; 188 break; 189 case DOWN: 190 if(maze[r][c - 1] == '#') 191 return 1; 192 break; 193 case LEFT: 194 if(maze[r - 1][c] == '#') 195 return 1; 196 break; 197 case UP: 198 if(maze[r][c + 1] == '#') 199 return 1; 200 break; 201 default: 202 printf("What's your position?\n"); 203 exit(-1); 204 205 } 206 207 return 0; 208 } /* eof toright() */ 209 210 /* Turn to left */ 211 int chroute(int route, int nroute) 212 { 213 switch(route) { 214 case RIGHT: 215 if(nroute == RIGHT) 216 route = DOWN; 217 else if(nroute == DOWN) 218 route = LEFT; 219 else if(nroute == LEFT) 220 route = UP; 221 else if(nroute == UP) 222 ; /* don't change */ 223 else 224 route = 0; 225 break; 226 case DOWN: 227 if(nroute == RIGHT) 228 route = LEFT; 229 else if(nroute == DOWN) 230 route = UP; 231 else if(nroute == LEFT) 232 route = RIGHT; 233 else if(nroute == UP) 234 ; /* don't change */ 235 else 236 route = 0; 237 break; 238 case LEFT: 239 if(nroute == RIGHT) 240 route = UP; 241 else if(nroute == DOWN) 242 route = RIGHT; 243 else if(nroute == LEFT) 244 route = DOWN; 245 else if(nroute == UP) 246 ; /* don't change */ 247 else 248 route = 0; 249 break; 250 case UP: 251 if(nroute == RIGHT || nroute == UP || 252 nroute == LEFT || nroute == DOWN) { 253 route = nroute; 254 } 255 else 256 route = 0; 257 break; 258 default: 259 printf("Where do you want to go?\n"); 260 exit(-1); 261 } 262 263 264 if(!route) { 265 printf("ERROR: invalid route: %d\n", nroute); 266 return -1; 267 } 268 269 return route; 270 } /* eof chroute() */ 271