home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l076 / 1.ddi / MAZE.TRU < prev    next >
Encoding:
Text File  |  1988-08-27  |  5.0 KB  |  184 lines

  1. !  Maze Maker.
  2. !
  3. !  Makes a maze (recursively) and then solves it.
  4. !  Here's how we make a maze:
  5. !     Start at the first square.
  6. !
  7. !     1.  If a neighboring square hasn't been used yet,
  8. !         kick down the wall to that square, and call (1)
  9. !         recursively.
  10. !
  11. !  Since many squares have more than 1 neighboring unused
  12. !  square, we have to keep a record of a square's unused
  13. !  neighbors and try each in turn.
  14. !
  15. !
  16. RANDOMIZE
  17. SET MODE "ega"
  18. SET COLOR "green"                 !open window onto screen
  19. BOX AREA 0,1,0,1
  20. SET BACK "cyan"
  21. OPEN #1: screen .1,.9,.05,.95
  22.  
  23. LET bottom = 20                   !size of maze (# squares)
  24. LET right  = 28
  25.  
  26. ASK MODE m$
  27. IF m$="MEDRES" then LET linewidth = .11 else LET linewidth = .058
  28.  
  29. DIM sq_used(0,0), sq_rwall(0,0), sq_bwall(0,0)
  30. MAT sq_used = Zer(bottom,right)
  31. MAT sq_rwall = sq_used
  32. MAT sq_bwall = sq_used
  33.  
  34. SET COLOR "red"
  35. SET WINDOW 0,right,bottom,0       !draw maze walls (just a grid)
  36. BOX LINES 0,right,bottom,0
  37. FOR i=1 to right-1
  38.     PLOT i,0; i,bottom
  39. NEXT i
  40. FOR i=1 to bottom-1
  41.     PLOT 0,i; right,i
  42. NEXT i
  43.  
  44. DEF Randint(n) = Int(n*rnd) + 1   !random integer 1..n
  45.  
  46. LET r = 1                         !r,c = starting point
  47. LET c = Randint(right)
  48. SET COLOR 0                       !kick down entry into maze
  49. PLOT c-(1-linewidth),0; c-linewidth,0
  50.  
  51. CALL Kickdown((r),(c),"")         !now recursively kick down walls
  52.  
  53. MAT sq_used = 0                   !get ready to solve maze
  54. SET COLOR "yellow"
  55. PLOT c-.5,r-1; c-.5,r-.5          !entry into maze
  56. WHEN error in
  57.      CALL Solve((r),(c))          !recursively solve maze
  58. USE
  59. END WHEN
  60.  
  61. !
  62. !
  63. !  Recursively create a maze.
  64. !
  65. !  r,c give our current row/column position.  d$ contains the record
  66. !  of where we CAN go from here: for instance "ur" means that the
  67. !  square above us, and the square to the right, are currently unused.
  68. !
  69. !  We worry about hitting the edge of the maze, etc.  Then we pick
  70. !  one of the possible directions, kick down that wall, and call
  71. !  ourselves recursively to handle that new square.
  72. !
  73. !  When we come back, we run through the other possible ways to go.
  74. !  Of course, these ways may no longer be good.
  75. !
  76. !
  77. SUB Kickdown(r,c,d$)
  78.  
  79.     IF r=bottom and sq_used(r,c)=0 then LET hit_bottom=hit_bottom+1
  80.  
  81.     LET sq_used(r,c) = 1
  82.     SET COLOR "yellow"
  83.     BOX CIRCLE c-.6,c-.4,r-.6,r-.4
  84.     PAUSE .02
  85.  
  86.     DO
  87.        LET d$ = ""
  88.        IF r>1 and sq_used(r-1,c)=0 then LET d$ = d$ & "u"
  89.  
  90.        IF (r<bottom and sq_used(r+1,c)=0) or (r=bottom and hit_bottom=right) then
  91.           LET d$ = d$ & "d"
  92.        END IF
  93.  
  94.        IF c>1 and sq_used(r,c-1)=0 then LET d$ = d$ & "l"
  95.  
  96.        IF c<right and sq_used(r,c+1)=0 then LET d$ = d$ & "r"
  97.  
  98.        SET COLOR 0
  99.        BOX AREA c-.6,c-.4,r-.6,r-.4
  100.  
  101.        IF d$ = "" then EXIT SUB   !can't go anywhere from here
  102.  
  103.        LET x = Randint(Len(d$))   !pick a random direction & kick down that wall
  104.        SELECT CASE d$[x:x]
  105.        CASE "u"
  106.             LET sq_bwall(r-1,c) = 1
  107.             PLOT c-linewidth,r-1; c-(1-linewidth),r-1
  108.             LET r = r-1
  109.        CASE "d"
  110.             LET sq_bwall(r,c) = 1
  111.             PLOT c-(1-linewidth),r; c-linewidth,r
  112.             IF r <> bottom then LET r=r+1
  113.             IF hit_bottom = right then LET hit_bottom = hit_bottom+1
  114.        CASE "l"
  115.             LET sq_rwall(r,c-1) = 1
  116.             PLOT c-1,r-linewidth; c-1,r-(1-linewidth)
  117.             LET c = c-1
  118.        CASE "r"
  119.             LET sq_rwall(r,c) = 1
  120.             PLOT c,r-linewidth; c,r-(1-linewidth)
  121.             LET c = c+1
  122.        END SELECT
  123.        PAUSE .03
  124.        CALL Kickdown((r),(c),"")  !call ourselves recursively for new square
  125.     LOOP
  126. END SUB
  127.  
  128. !
  129. !
  130. !  Recursively solve a maze.
  131. !
  132. !  First, we remember that we've come into this square.  Then we search
  133. !  about trying to move into any unoccupied square neighboring this one.
  134. !  (That's the recursive call.)  If there are no unoccupied squares
  135. !  next to us, we forget that we've come to this square, and return.
  136. !
  137. !  If we hit the bottom exit, we cause an error to indicate that
  138. !  we're all done.
  139. !
  140. !
  141. SUB Solve(r,c)
  142.     LET sq_used(r,c) = 1
  143.  
  144.     IF sq_rwall(r,c)<>0 then      !try going right
  145.        CALL Try(r,c,r,c+1)
  146.     END IF
  147.  
  148.     IF sq_bwall(r,c)<>0 then      !try going down
  149.        IF r = bottom then
  150.           SET COLOR "yellow"
  151.           PLOT c-.5,r-.5; c-.5,r
  152.           CAUSE ERROR 1           !hurrah -- hit bottom exit!!
  153.        END IF
  154.        CALL Try(r,c,r+1,c)
  155.     END IF
  156.  
  157.     IF c>1 and sq_rwall(r,c-1)<>0 then      !try going left
  158.        CALL Try(r,c,r,c-1)
  159.     END IF
  160.  
  161.     IF r>1 and sq_bwall(r-1,c)<>0 then      !try going up
  162.        CALL Try(r,c,r-1,c)
  163.     END IF
  164.  
  165. END SUB
  166.  
  167. !
  168. !  Try
  169. !
  170. !  Subroutine for Solve.  Try moving into a square.  Quit if it's
  171. !  already used.  Otherwise extend the "solving" line, and call Solve
  172. !  for the new square.
  173. !
  174. SUB Try(r,c,nr,nc)
  175.     IF sq_used(nr,nc)=1 then EXIT SUB
  176.     SET COLOR "yellow"
  177.     PLOT c-.5,r-.5; nc-.5,nr-.5
  178.     CALL Solve((nr),(nc))
  179.     SET COLOR 0
  180.     PLOT c-.5,r-.5; nc-.5,nr-.5
  181. END SUB
  182.  
  183. END
  184.