training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

maze2.c (5052B)


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