CAD Forum - tips, tricks, discussion and utilities for AutoCAD, Inventor, Revit and other Autodesk products [www.cadforum.cz]
CZ | EN | DE
Login or
registration
  Visitors: 20410

CAD tip CAD tip # 11914:

   
Question CAD 
 %  platform  category 
Q - question

Maze generator for AutoCAD.

A - answer MAZE is a generalized maze generator written in VisualLISP. This utility was ported to AutoCAD from the original CLISP algorithm published on Github.

You can use MAZE to draw any maze structure - a predetermined arrangement of cells (a rectangular grid) with wall sites between them. Each new maze is unique, random. All generated mazes have a free walkthrough path (maze solution).

Download the free MAZE.LSP file from Download, load it into AutoCAD by mouse dragging or with the APPLOAD command and type the MAZE command to draw a randomly generated maze grid. The maze is drawn as LINE entities near the coordinate origin point (0,0).

You can change the values of the *width* and *height* LISP variables (in the LSP file) to draw mazes of any size (complexity). Always use odd numbers for these sizes.

Sample DWG drawings generated by MAZE are here - MAZE-139x77.DWG and MAZE-47x29.DWG (3D maze, by assigning width and thickness to polylines made from Maze lines).

MAZE source code - VisualLISP:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; CLISP Maze 20030311 by Joe Wingbermuehle - maze generator
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Mod from CLISP to VisualLISP by CAD Studio, 2018 - www.cadstudio.cz  www.cadforum.cz
; - like us on Facebook - fb.com/CADstudio
;
; The width and height of the maze. Both must be odd!
(setq *width* 39) ; 139
(setq *height* 21) ; 77

(setq maze nil)
(vl-load-com)

(defun C:MAZE ( / x y)

 (defun random (rng / x) ; integer version
  (fix (* (/ (setq x 4294967296.0 seed (rem (1+ (* 1664525.0 (cond (seed) ((getvar 'DATE))))) x)) x) rng))
 )

(defun setf (arr x y new) ; put array
 (vlax-safearray-put-element arr x y new)
)
(defun aref (arr x y) ; get array
 (vlax-safearray-get-element arr x y)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Start carving the maze at a specific location.
(defun carve-maze (x y / d c cd dv x1 y1 x2 y2)
	(setq d (random 4))
		(setq c 0)
		(while (< c 4)
         (setq  cd (rem (+ c d) 4) ; mod
                dv (cond
                        ((= cd 0) (list 1 0))
                        ((= cd 1) (list 0 1))
                        ((= cd 2) (list -1 0))
                        (t        (list 0 -1)))
                x1 (+ x (car dv))
                y1 (+ y (cadr dv))
                x2 (+ x1 (car dv))
                y2 (+ y1 (cadr dv))
               )
            (if (and (and (> x2 0) (< x2 *width*))
                     (and (> y2 0) (< y2 *height*)))
               (if (and (= (aref maze x1 y1) 1)
                        (= (aref maze x2 y2) 1))
				  (progn
                     (setf maze x1 y1 0)
                     (setf maze x2 y2 0)
                     (carve-maze x2 y2)
                  )
               )
            )
		(setq c (1+ c))
      ); loop
   ;)
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Generate a maze
(defun generate-maze ()
   (random 13)
   (setf maze 1 1 0) ; 1 1 0
   (carve-maze 1 1)
   (setf maze 1 0 0) ; maze entry
   (setf maze (- *width* 1) (- *height* 2) 0) ; maze exit
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Display the maze - textual
(defun display-maze ( / y x)
   (setq y 0)
   (while (< y *height*)
      (setq x 0)
      (while (< x *width*)
         (if (= (aref maze x y) 1)
            (princ "[]") ; \U+25AE
            (princ "  ")
         )
		 (setq x (1+ x))
      ) ; wh x
      (terpri)
	  (setq y (1+ y))
   ) ; wh y
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Draw the maze - graphics
(defun draw-maze ( / y x elem e oldc oldo)
   (setq oldc (getvar "CMDECHO")  oldo (getvar "OSMODE"))
   (setvar "CMDECHO" 0)(setvar "OSMODE" 0)
   (setq y 0)
   (while (< y *height*); HORIZONTALS ----
      (setq x 0  elem nil)
      (while (< x *width*)
         (if (= (aref maze x y) 1)
			(if elem (setq elem (cons (list x (- *height* y 1)) elem)) ; still horizontals
					 (setq elem (list (list x (- *height* y 1))))
			) ; else 0
			(if (and elem (> (length elem) 1)) ; any segments to draw?
				(progn (command "_LINE" (car (reverse elem)) (car elem) "") (setq elem nil)) ; start->end
				(setq elem nil)
			)
         ) ; maze draw
		 (setq x (1+ x))
		 (if (and (= x *width*) elem (> (length elem) 1))(progn (command "_LINE" (car (reverse elem)) (car elem) ""))) ;finish row
      ) ; wh x
	  (setq y (1+ y))
   ) ; wh y
   (setq x 0)
   (while (< x *width*); VERTICALS -------
      (setq y 0  elem nil)
      (while (< y *height*)
         (if (= (aref maze x y) 1)
			(if elem (setq elem (cons (list x (- *height* y 1)) elem)) ; still verticals
					 (setq elem (list (list x (- *height* y 1))))
			) ; else 0
			(if (and elem (> (length elem) 1)) ; any segments to draw?
				(progn (command "_LINE" (car (reverse elem)) (car elem) "") (setq elem nil)) ; start->end
				(setq elem nil)
			)
         ) ; maze
		 (setq y (1+ y))
		 (if (and (= y *height*) elem (> (length elem) 1))(progn (command "_LINE" (car (reverse elem)) (car elem) ""))) ;finish column
      ) ; wh y
	  (setq x (1+ x))
   ) ; wh x
  (setvar "OSMODE" oldo)
  (setvar "CMDECHO" oldc)
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Create and display the maze.

(setq maze (vlax-make-safearray vlax-vbInteger (cons 0 (1- *width*)) (cons 0 (1- *height*)))) ; create array
(setq y 0)
(while (< y *height*)
   (setq x 0)
   (while (< x *width*)
      (setf maze x y 1) ; prefill
      (setq x (1+ x))
   ) ; wh x
   (setq y (1+ y))
) ; wh y

(generate-maze) ; create
(display-maze)  ; list out
(draw-maze)     ; draw out

(princ)
)

(princ "\nMAZE command loaded.")
(princ)
ACAD
100% *  CAD 
9.6.2018    22309×  
Prices - CAD eShop:
applies to: AutoCAD ·

See also:
Tip 14061:Animated drawing of geometry in AutoCAD.
Tip 14027:3D heart from the heart.
Tip 14017:Voronoi diagrams in AutoCAD.
Tip 14007:Workaround for the missing (vlax-create-object) in AutoCAD LT (voice, clipboard, etc.)
Tip 13960:Fermat's spiral in AutoCAD.


Back   All CAD Tips



Have we helped you? If you want to support the CAD Forum web service, consider buying one of our CAD applications, or our custom software development offerings, or donating via PayPal (see above). You may also add a link to your web - like this "fan" link: CAD Forum - tips, utilities, blocks for Autodesk products
CAD:    OS:    Categ: 
Text:  FAQ glossary   



Featuring:
Generate hatch patterns automatically from your DWG drawings with
HGEN 2006 More info


Please use these tips at your own risk.
Arkance Systems is not responsible for possible problems that may occur as a result of using any of these tips.
TOPlist