training

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

maze.c (4398B)


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