home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / R_LA4_02.ZIP / MAZE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-21  |  6.4 KB  |  214 lines

  1. /* MAZE.C - From page 588 of "Microsoft C Programming for       */
  2. /* the IBM" by Robert Lafore. This program used direct mem-     */
  3. /* ory access to draw a maze on the screen.                     */
  4. /****************************************************************/
  5.  
  6. #include <stdio.h>
  7. #include <time.h>
  8. #define MAXROWS 23
  9. #define MAXCOLS 78
  10. #define FALSE 0
  11. #define TRUE 1
  12. #define RIGHT 0
  13. #define LEFT 1
  14. #define UP 2
  15. #define DOWN 3
  16. #define BLANK '\x20'    /* space */
  17. #define HORZ '\xCD'     /* vertical line */
  18. #define VERT '\xBA'     /* vertical pipe */
  19. #define NONE '\x07'     /* impossible combination */
  20. #define L_UP '\xBC'     /* L bend left to up */
  21. #define L_DN '\xBB'     /* L bend left to down */ 
  22. #define R_UP '\xC8'     /* L bend right to up */
  23. #define R_DN '\xC9'     /* L bend right to down */
  24. #define T_DN '\xCB'     /* T bend going down */
  25. #define T_UP '\xCA'     /* T bend going up */
  26. #define T_RT '\xCC'     /* T bend going right*/
  27. #define T_LF '\xB9'     /* T bend going left*/
  28. #define BORDER '\xB2'   /* grey square */
  29. int lastd;
  30.       /* going to: rite  left  up    down      coming from */
  31. char table[4][4]={{HORZ, NONE, L_UP, L_DN},     /* left */
  32.                   {NONE, HORZ, R_UP, R_DN},     /* right */
  33.                   {R_DN, L_DN, VERT, NONE},     /* up */
  34.                   {R_UP, L_UP, NONE, VERT} };   /* down */
  35. main()
  36. {
  37. int col, row, j;
  38. char getscr(), spot;
  39.  
  40.    cprintf("\x1B[2J");              /*clear screen*/
  41.    border();
  42.    col = 0; row = 12; lastd = RIGHT;   /*start at middle left*/
  43.    randline(col, row);                 /*draw first line*/
  44.    for(j = 1; !kbhit(); j++)  {        /*draw more lines*/
  45.       do  {                            /*until key struck*/
  46.          col = random(MAXCOLS - 1);    /*choose random spot*/
  47.          row = random(MAXROWS - 1);
  48.          spot = getscr(col, row);      /*find spot with line*/
  49.       } while(spot != HORZ && spot != VERT); /*on it already*/
  50.       tee(spot, col, row);       /*insert T in line, draw new line*/
  51.       if(j == 200)               /*after 200 lines*/
  52.          drawexit();
  53.    }  /* end of for */
  54.    goexit();                  /* all over */
  55. }  /*end of main*/
  56.  
  57. /* randline() */  /* draws random line starting at x, y */
  58. randline(x, y)
  59. int x, y;
  60. {
  61. char trapped = FALSE;
  62.  
  63.    while (trapped == FALSE)  {
  64.       switch (random(7))  {
  65.          case 1:  /*draw line to right*/
  66.          case 2:
  67.          case 3:
  68.          case 4:
  69.             if(getscr(x + 1, y) == BLANK)
  70.                turn(RIGHT, x++, y);
  71.             break;
  72.          case 5:     /*draw line to the left*/
  73.             if(getscr(x - 1, y) == BLANK)
  74.                turn(LEFT, x--, y);
  75.             break;
  76.          case 6:     /*draw line up (decrease y)*/
  77.             if(getscr(x, y - 1) == BLANK)
  78.                turn(UP, x, y--);
  79.             break;
  80.          case 7:     /*draw line down (increase y)*/
  81.             if(getscr(x, y + 1) == BLANK)
  82.                turn(DOWN, x, y++);
  83.             break;
  84.       }  /* end switch */
  85.       /* see if this line trapped in cul-de-sac */
  86.       if((getscr(x+1, y)!=BLANK) && (getscr(x-1, y) !=BLANK) &&
  87.             (getscr(x, y+1)!=BLANK) && (getscr(x, y-1) !=BLANK))
  88.          trapped = TRUE;         /*set flag*/
  89.       if(kbhit() != 0)           /*if key struck*/
  90.          goexit();
  91.    }  /* end while */
  92. }  /* end randline */
  93.  
  94. /* random() */    /* generates random number between 1 and rmax */
  95. int random(rmax)
  96. int rmax;
  97. {
  98. long seconds;
  99. static int rand;
  100.  
  101.    time(&seconds);
  102.    rand = 0x7fff & ((int)seconds + rand*273) >> 4;
  103.    return((rand % rmax) + 1);
  104. }
  105.  
  106. /* turn() */   /* function to print correct character to make turn */
  107.                /* direction is where we're going */
  108.                /* lastd is where we're coming from */
  109.                /* turn characters are in array table */
  110. turn(direction, x, y)
  111. int direction, x, y;
  112. {
  113.  
  114.    putscr( table[lastd][direction], x, y);
  115.    lastd = direction;
  116. }
  117.  
  118. /* border() */    /* draws border around screen */
  119. border()
  120. {
  121. int j;
  122.  
  123.    for( j = 0; j < MAXCOLS + 2; j++)  {
  124.       putscr(BORDER, j, 0);               /*draw  top*/
  125.       putscr(BORDER, j, MAXROWS + 1);     /*draw bottom*/
  126.    }
  127.    for( j = 0; j < MAXROWS + 2; j++)  {
  128.       putscr(BORDER, 0, j);               /*draw  right*/
  129.       putscr(BORDER, MAXCOLS + 1, j);     /*draw left*/
  130.    }
  131. }
  132.  
  133. /* tee() */    /* inserts T in existing horiz or vert line at col, row */
  134. tee(spot, col, row)
  135. int col, row;
  136. char spot;
  137. {
  138.  
  139.    if (spot == HORZ)  {                   /* HORIZ LINE */
  140.       if (getscr(col, row + 1) == BLANK)  {        /*go down*/
  141.          putscr(T_DN, col, row);
  142.          lastd = DOWN;
  143.          randline(col, ++row);
  144.       }
  145.       else
  146.          if (getscr(col, row - 1) == BLANK)  {        /*go up*/
  147.             putscr(T_UP, col, row);
  148.             lastd = UP;
  149.             randline(col, --row);
  150.          }
  151.    }
  152.    else  {                                      /* VERT LINE */
  153.       if (getscr(col + 1, row) == BLANK)  {        /*go right*/
  154.          putscr(T_RT, col, row);
  155.          lastd = RIGHT;
  156.          randline(++col, row);
  157.       }
  158.       else
  159.          if (getscr(col - 1, row) == BLANK)  {        /*go left*/
  160.             putscr(T_LF, col, row);
  161.             lastd = LEFT;
  162.             randline(--col, row);
  163.          }
  164.    }  /* end else */
  165. }  /* end tee */
  166.  
  167. /* drawexit() */  /* draws exit line on right edge of maze */
  168.                   /* looks for vertical line to start on */
  169. drawexit()
  170. {
  171. int j;
  172.  
  173.                /* look for vert line */
  174.    for(j = 6; getscr(MAXCOLS, j) != VERT; j++)
  175.       ;
  176.    putscr(T_RT, MAXCOLS, j);           /* insert right tee */
  177.    putscr(HORZ, MAXCOLS + 1, j);       /* add horiz line */
  178. }
  179.  
  180. /* goexit() */    /* puts cursor in lower right corner & exits */
  181. goexit()
  182. {
  183.  
  184.    printf("\x1B[%d;%dH", 24, 1);       /* move cursor */
  185.    exit();
  186. }
  187.  
  188. /* putscr() */    /* puts char on screen at location col, row */
  189.                   /* (cols 0 to 79, rows 0 to 24) */
  190. putscr( ch, col, row)
  191. char ch;
  192. int col, row;
  193. {
  194. int far *farptr;
  195. int attr = 0x0700;         /* attribute for normal text */
  196.  
  197.    farptr = (int far *) 0xB0000000;    /* start of screen mem */
  198.    *(farptr + row*80 + col) = ((int)ch & 0x00ff) | attr;
  199. }
  200.  
  201. /* getscr */   /* returns char stored in screen mem */
  202.                /* at location col, row */
  203. char getscr(col, row)
  204. int col, row;
  205. {
  206. int far *farptr;
  207. char ch;
  208.  
  209.    farptr = (int far *) 0xB0000000;
  210.    ch = *(farptr + row*80 + col);
  211.    return(ch);
  212. }
  213.  
  214.