tiny-maze


Information

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


INFORMATION


-------
A tiny, self-starting maze generator demo that uses a non-recursive bread-crumb technique to draw single-path mazes, where each call to GO adds (or retraces) one step to the path, and a left-handed traversal routine.

COPYRIGHT


-----
Tiny-maze Copyright (C) 2005 James P. Steiner

Procedures

NetLogo Version: NetLogo 3.0

;; SUMMARY
;;;;  A tiny non-recursive maze generator. Click to pause.
;; 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)
;;;; 152 x 174
;;
globals
[ click?
  running?
  mode
]


patches-own
[ my-source
  value
  new-value
]

to startup
   setup
   loop
   [ go ]
end

to setup
   ca
   set running? true
   set click? false
   set mode 0
end

to go
   no-display
   if running? = true
   [ go-maze ]
   go-mouse-effects
   display
   wait (1 / 60)
end

to go-maze
   ifelse mode < 2
   [ maze-draw ]
   [ maze-walk wait ( 1 / 20) ]
   every 30
   [ set mode 0
   ]
end

to maze-walk
     ifelse mode < 4
     [ cct 4
       [ set xcor (screen-edge-x - 2) - (screen-size-x - 4) * (who mod 2)
         set ycor (screen-edge-y - 2) - (screen-size-y - 4) * (int (who / 2))
         set color turquoise + 20 * who
         set heading 90 * who
         set shape "square" set size 1.99
       ] 
       set mode 4
     ]
     [ ask turtles
       [ ;; right-hand traversal--extend right-limb and drag on maze wall to traverse maze
         ;; 1. if opening on right, turn right
         ;; 2. else: if opening ahead, go forward
         ;; 3. else: if opening on left, go left
         ;; 4. else: dead-end, about-face
         ;; 5. go forward.
         ;; optimzed, looks like this:
         ;; 1. if opening on right, turn right
         ;; 2. else: if NO opening ahead, 
         ;;    2a: if opening on left, turn left
         ;;    2b: else: dead-end, about face
         ;; 3. Go forward
            
         
         ifelse pcolor-of patch-right-and-ahead 90 1      = black [ rt  90 ]
         [ if pcolor-of patch-ahead 1                    != black
           [ ifelse pcolor-of patch-left-and-ahead  90 1  = black [ lt  90 ]
                                                                  [ rt 180 ]
         ]]
         jump 1
         
         ;; another method:
         ;; build a list of the OPEN patches, in counter-clockwise order,
         ;; starting to the right, the agentset will be in the order
         ;; specified, one-of will select the *first* open direction.
         ;; turn to that direction, go forward
         ;;;;set heading towards one-of patches at-points
         ;;;;     (list (list dy dx) (list dx dy) (list (- dy) dx) (list (- dy) (- dx)))
         ;;;;jump 1
       ]
       ;; check for collisions with other walker turtles
       ;; if I am older (lower who number), I grow, otherwise, I die.
       ask turtles
       [ ;; use radius 1.5 so that turtles can't skip over each other
         let others turtles in-radius 1.5 with [ self != myself ]
         if any? others 
           [ let impact 1
             if any? others with [ who < who-of myself ] [ set impact 2 ]
             ifelse impact = 1 [ set size size + .25 ] [ die ]
           
           ]
         ]  
       ]
     
end

to go-mouse-effects
   ifelse mouse-down?
   [ if click? != true [ set click? true ] ]
   [ if click?  = true [ set running? not running? set click? false ] ]
end

to-report smart-towardsxy [ a b ]
   ifelse a != 0 or b != 0
   [ report towardsxy a b ]
   [ report 0 ]
end   

to maze-draw
   let new gray
   let crumb blue
   let final black
   let border white
   ifelse mode = 0
   [ ask patches [ set pcolor new ]
     ask patches with [ abs pxcor = screen-edge-x or abs pycor = screen-edge-y ] [ set pcolor border ]
     ask turtles [ die ]
     cct 1
     [ set color yellow
       set size 1.99
       set pcolor crumb
     ]
     set mode 1
   ]
   [ ask turtles
     [ 
       let paths patches at-points [ [-2 0] [2 0] [0 2] [0 -2] ] with [ abs(pxcor - pxcor-of myself) <= 2 and abs(pycor - pycor-of myself) <= 2 and pcolor = new]
       if not any? paths
       [ set paths patches at-points [ [-1 0] [1 0] [0 1] [0 -1] ] with [ abs(pxcor - pxcor-of myself) <= 1 and abs(pycor - pycor-of myself) <= 1 and pcolor = crumb]
       ]
       ifelse any? paths
       [ let path random-one-of paths
         set heading towards path
         ifelse pcolor-of path = new
         [ set pcolor crumb
           jump 1 set pcolor crumb
           jump 1 set pcolor crumb
         ]
         [ set pcolor final
           jump 1 set pcolor final
           jump 1 set pcolor final
         ]
       ]
       [ set mode mode + 1 
         die
       ]
     ]
   ]
end
     

                    


Download Link

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