home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / MAZE_1.C < prev    next >
C/C++ Source or Header  |  1991-10-01  |  4KB  |  184 lines

  1. /*
  2.    This program makes 10x10 mazes and prints them on the screen.  No
  3.    promise of portability is made, but it does seem to work on NS GNX
  4.    C.
  5.  
  6.    Public Domain by Jonathan Guthrie.
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <time.h>
  12.  
  13. #define UP  1
  14. #define DN  2
  15. #define LT  4
  16. #define RT  8
  17.  
  18. int  addelem(int, int [12][12], int *, int, int);
  19. void openwall(int [12][12], int, int);
  20. void writemaze(int [12][12]);
  21.  
  22. void main(void)
  23. {
  24.       int i, j, base;
  25.       int search[150], array[12][12];
  26.  
  27.       for(i=1 ; i<11 ; ++i)
  28.       {
  29.             array[i][0] = -1;
  30.             array[i][11] = -1;
  31.             array[0][i] = -1;
  32.             array[11][i] = -1;
  33.             for(j=1 ; j<11 ; ++j)
  34.                   array[i][j] = 0;
  35.       }
  36.  
  37.       srand((int)time(NULL));
  38.       i = rand() % 10 + 1;
  39.       j = rand() % 10 + 1;
  40.       base = addelem(0, array, search, i, j);
  41.       array[i][j] = RT + RT;      /* Not a valid value */
  42.       while(0 < base)
  43.       {
  44.             i = rand() % base;
  45.             j = search[i];
  46.             search[i] = search[--base];
  47.             i = j % 100;
  48.             j /= 100;
  49.             openwall(array, i, j);
  50.             base = addelem(base, array, search, i, j);
  51.       }
  52.  
  53.       writemaze(array);
  54. }
  55.  
  56.  
  57. int addelem(int base, int maze[12][12], int *search, int row, int col)
  58. {
  59.       if(0 == maze[row-1][col])
  60.       {
  61.             search[base++] = row + col * 100 - 1;
  62.             maze[row-1][col] = -DN;
  63.       }
  64.       else if(0 > maze[row-1][col])
  65.             maze[row-1][col] -= DN;
  66.  
  67.       if(0 == maze[row+1][col])
  68.       {
  69.             search[base++] = row + col * 100 + 1;
  70.             maze[row+1][col] = -UP;
  71.       }
  72.       else if(0 > maze[row+1][col])
  73.             maze[row+1][col] -= UP;
  74.  
  75.       if(0 == maze[row][col-1])
  76.       {
  77.             search[base++] = row + col * 100 - 100;
  78.             maze[row][col-1] = -RT;
  79.       }
  80.       else if(0 > maze[row][col-1])
  81.             maze[row][col-1] -= RT;
  82.  
  83.       if(0 == maze[row][col+1])
  84.       {
  85.             search[base++] = row + col * 100 + 100;
  86.             maze[row][col+1] = -LT;
  87.       }
  88.       else if(0 > maze[row][col+1])
  89.             maze[row][col+1] -= LT;
  90.  
  91.       return base;
  92. }
  93.  
  94.  
  95. void openwall(int maze[12][12], int row, int col)
  96. {
  97.       int directions, max, direction, temprow, tempcol, temp, back;
  98.  
  99.       directions = -maze[row][col];
  100.  
  101.       max = 0;
  102.       if(directions & UP)
  103.       {
  104.             temp = rand();
  105.             if(temp > max)
  106.             {
  107.                   max = temp;
  108.                   direction = UP;
  109.                   back = DN;
  110.                   temprow = row - 1;
  111.                   tempcol = col;
  112.             }           
  113.       }
  114.  
  115.       if(directions & DN)
  116.       {
  117.             temp = rand();
  118.             if(temp > max)
  119.             {
  120.                   max = temp;
  121.                   direction = DN;
  122.                   back = UP;
  123.                   temprow = row + 1;
  124.                   tempcol = col;
  125.             }
  126.       }
  127.  
  128.       if(directions & LT)
  129.       {
  130.             temp = rand();
  131.             if(temp > max)
  132.             {
  133.                   max = temp;
  134.                   direction = LT;
  135.                   back = RT;
  136.                   temprow = row;
  137.                   tempcol = col - 1;
  138.             }
  139.       }
  140.  
  141.       if(directions & RT)
  142.       {
  143.             temp = rand();
  144.             if(temp > max)
  145.             {
  146.                   max = temp;
  147.                   direction = RT;
  148.                   back = LT;
  149.                   temprow = row;
  150.                   tempcol = col + 1;
  151.             }
  152.       }
  153.     
  154.       maze[row][col] = direction;
  155.       maze[temprow][tempcol] += back;
  156. }
  157.  
  158. void writemaze(int maze[12][12])
  159. {
  160.       int i, j;
  161.  
  162.       puts("*********************");
  163.       for(i=1 ; i<11 ; ++i)
  164.       {
  165.             putchar('*');
  166.             for(j=1 ; j<11 ; ++j)
  167.             {
  168.                   putchar(' ');
  169.                   if(maze[i][j] & RT)
  170.                         putchar(' ');
  171.                   else  putchar('*');
  172.             }
  173.             putchar('\n');
  174.             for(j=1 ; j<11 ; ++j)
  175.             {
  176.                   putchar('*');
  177.                   if(maze[i][j] & DN)
  178.                         putchar(' ');
  179.                   else  putchar('*');
  180.             }
  181.             puts("*");
  182.       }
  183. }
  184.