maze-maker


Information

Created with NetLogo version NetLogo 3.0
Running with NetLogoLite.jar version 302.



Procedures

NetLogo Version: NetLogo 3.0

;; SUMMARY
;;;;  The amazing maze maker!
;; COPYRIGHT & LICENSE
;;;; Copyright (C) 2005 James P. Steiner
;;;; Some Rights Reserved.
;;;; Creative Commons Attribution-NonCommercial-ShareAlike License v. 2.0.
;;;; Visit http://creativecommons.org/licenses/by-nc-sa/2.0/ for more information.
;; APPLET DIMENSIONS (width x height)
;;;; 610 x 450
;;;;

globals [ depth         ;; measures the number of recursive calls
          max-depth     ;; holds the peak depth
          halt          ;; if set to 1, maze-building stops
          spacing       ;; the patch distance bewteen centers of maze corridors
          z-spacing     ;; 0 - spacing
          edge-vs-spacing-x
          edge-vs-spacing-y
          maze-edge-x   ;; like screen-edge for the maze
          maze-edge-y   ;; 
          blocks-x       ;; the number of maze-blocks across
          blocks-y       ;; the number of maze-blocks down
          first-x       ;; marks the lower left corner.
          first-y      ;; "" 
          room-count   ;; to total maze-blocks in the grid
          radius        ;; number of patches from the center patch.
          mark-color    ;; complements the gap-color. marks unused maze blocks
          tiles         ;; patches that are stepping points on the grid
        ]

turtles-own [ stack ]
to push ; turt;e pushes current coords onto stack, before turning
    set stack fput ( list xcor ycor ) stack
end

to pop ; turtle pops previous turn point from stack
  locals [ xy ]
  set xy item 0 stack
  set stack but-first stack
  setxy (item 0 xy) (item 1 xy)
end

to setup
  clear-all
  ;; path-width is width of drawn maze path, in patches
  ;; radius should always work out to integer, but just in case...
  set radius int( ( path-width - 1 ) / 2 )
  set mark-color white
  
  ; spacing is distance 'tween centers of paths
  set spacing gap + path-width
  ; zero minus spacing
  
  set tiles patches with
  [    pxcor mod spacing = 0
   and pycor mod spacing = 0
   and abs pxcor + radius + 1 < screen-edge-x
   and abs pycor + radius + 1 < screen-edge-y
  ]
 
  ; the total number of blocks in the maze
  set room-count count tiles
    
  mark-tiles

  set halt 0
  create-walkers  

end

to create-walkers
  if walkers >= 1 + room-count / 10
  [ set walkers 1 + room-count / 10 ]
  create-custom-turtles walkers
  [ pick-spot
    while [ any? other-turtles-here ]
    [ pick-spot ]
    set heading 0
    set color pen-color
    if random-orientation? [ set heading 90 * random 4 ]
    if not one-color? 
    [ while [ color = pen-color or color = mark-color ]
      [ set color 3 + 10 * random 13 + random-float 5.0
      ]
    ]
    ask patches in-radius radius [ set pcolor color-of myself ]
    set stack []
    push
  ]

end
 
to pick-spot
    locals [ spot ]
    set spot tiles with [ not any? turtles-here ]
    ifelse any? spot
    [ set spot random-one-of spot
      set xcor pxcor-of spot
      set ycor pycor-of spot
    ]
    [ die ]
end

to mark-tiles
   ask tiles [ set pcolor mark-color ]
end


to build-maze ;; turtle procedure 
  locals [ paths target left-right straight curve? running my-color]

  if halt = 1 [ stop ]
  set depth depth + 1
  if depth > max-depth [ set max-depth depth ]
  
   ; make list of open directions to travel
   set paths find-open-paths
   ifelse not any? paths
   [ ifelse length stack > 0
     ; if there's no where to go, pop back to the location
     ; of the previous turn, and start again from there.
     [ pop ]
     [ stop ]
   ]
   [ ; there is somewhere to go, so...
     ; first, identify the patch that is straight ahead
     set straight patch-ahead spacing
     set left-right paths with [ self != straight ]
     ; check if I should go straight, or maybe curve?
     set curve? random-float 1.0 < curvyness
     ifelse ( curve? and any? left-right )
            or not is-open straight
     [ ; I should curve, or I can't go straight.
       ; so pick a direction, avoiding picking straight.
       set target random-one-of left-right
       ; mark the destination, to help prevent another
       ; turtle from trying to go there.
       set pcolor-of target color
       ; add my current location to the list of
       ; places where I made a turn. I come back
       ; here, later.
       push
       ; turn to the new location 
       set heading towards target
       ; go there!
       draw-move
     ]
     [ ; I'm supposed to go straight.
       ; so first I'll leave myself a note, so I
       ; remember that I'm going straight for a while.
       set running true
       ; While I'm running...
       while [ running ]
       [ ; mark my target patch
         set pcolor-of straight color
         ; turn to face it
         set heading towards straight
         ; go there!
         draw-move
         ; identify the next patch
         set straight patch-at ( dx * spacing) ( dy * spacing )
         ; Am I still running?
         ; if I pick a number more than the curvyness factor,
         ; and the patch ahead is clear, I'm still running!
         set running ( random-int-or-float 1.0 >= curvyness and is-open straight )
       ]
     ]
   ]
   if length stack = 0 [ stop ]
end


to-report find-open-paths
  locals [ paths path-list ]
  set paths (patches
              at-points
              (map [ (list (?1 * spacing ) (?2 * spacing) )] [ 0 0 1 -1 ] [ 1 -1 0 0 ])
             ) with [ pcolor = mark-color ]
  report paths
end


to draw-move
    locals [ my-color start-spot]
    set my-color color
    set start-spot patch-here
    ifelse radius > 0
    [ repeat spacing
      [ ask patches in-radius radius [ set pcolor my-color ] ; with [ pcolor != my-color ][ set pcolor my-color ]
        jump 1 ]
      ask patches in-radius radius [ set pcolor my-color ]
    ]
    [ repeat spacing 
      [ jump 1
        set pcolor color
      ]
    ]
    ask start-spot [ ask patches in-radius radius [ set pcolor my-color - 2 ] ]
 end


to-report is-open [ a-patch ]
    report (pcolor-of a-patch = mark-color)
end

    
to setup-and-go
    setup
    display
    go
end

to go
    ask turtles
    [ go-turtle
    ]
end

to go-turtle
    push 
    while [ length stack > 0 ]
    [ build-maze ]
end

to randomize
    set walkers 1 + random 5
    set gap random 5
    set path-width 1 + 2 * random 5
    set curvyness random-float 1.0
    setup
    go
    print max-depth
    wait 1
    if halt = 1 [ stop ]
end


                    


Download Link

View or download the complete model file (to download: right-click, save-link-as):
-- Download maze-maker --