Jump to content

Maze generation: Difference between revisions

Line 732:
=={{header|Common Lisp}}==
Uses unicode line drawing chars. Assumes they are single width on console. Code pretty horribly unreadable.
<lang lisp>(defmacro !and= (a b) `(setf ,a (logand ,a*random-state* (lognotmake-random-state ,b))t))
(defmacro wipe (y x v) `(!and= (aref arr ,y ,x) ,v))
 
(defun make2d-mazearray (w h)
(make-array (list h w) :initial-element 0)))
(setf *random-state* (make-random-state t))
(let ((arr (make-array (list (+ h h 2) (+ w w 2))
:initial-element 0)))
 
(loopdefmacro for i belowor-and (+v ha hb 1c) do
`(loopif for(or j below,a (+and w,b w(= 1 ,c))) do0 ,v))
(setf (aref arr i j) 15)))
 
(defun showmake-maze (mw h)
;; initial clean up
(let ((vis (loop2d-array for i tow h do))
; (wipever (+2d-array iw ih) 0 4)
; (wipehor (+ i i) (+2d-array w wh)) 8)
(labels
(loop for j to w do
(setf (aref arr (+ i i 1)((walk (+ j j 1))y 0x)
;(wipe 0 (+setf (aref vis jy jx) 1)
;(wipe (+ h h) (+ j j)loop 2)
(wipe (+ i i) (+ j j 1)(let (x2 3y2)
(wipeloop for (+dx idy) iin '((-1 0) (+ j1 j0) 12(0 -1) (0 1))
with cnt = 0 do
 
(labelslet ((walkxx (y+ x dx))
(setfyy (aref arr+ y xdy)) 16)
(loopif (letand (xxarray-in-bounds-p vis yy xx)
(setfzerop (aref arrvis yy x) 0)xx))
(loop with cnt = 0 with x1 with y1
(zerop (random (incf cnt))))
;; find a random neighbor
for (dxsetf dy)x2 inxx '((-2y2 0yy) (0 2) (2 0) (0 -2))
do (letif ((x1not (+ dx x)x2) (y1return-from (+ dy y)walk))
(whenif (and (<= 0 y1 (+ hx h)x2)
(setf (<=aref 0 x1hor (+min wy wy2) x) 1)
(setf (aref ver (zeropy (logandmin (arefx arr y1 x1x2)) 161))
(zeropwalk (random (incfy2 cntx2))))
(setf xx x1 yy y1))))
 
(if (not (and xx yy)) (return-from walk))
 
;; break walls along the walk
(if (= x xx)
(let ((yy (/ (+ y yy) 2)))
(wipe yy (1+ x) 4)
(wipe yy (1- x) 8)
(setf (aref arr yy x) 0)))
 
(if (= y yy)
(let ((xx (/ (+ x xx) 2)))
(wipe (1+ y) xx 1)
(wipe (1- y) xx 2)
(setf (aref arr y xx) 0)))
 
(walk yy xx)))))
 
(show ()
(walk (1+ (* 2 (random h))) (1+ (* 2 (random w)))))
(let ((g " │││─┘┐┤─└┌├─┴┬┼"))
arr))
(loop for i from 0 to h do
(loop for j from 0 to w do
(format t "~c~a"
(char g
(+ (or-and 1 (= i 0) (> j 0) (aref ver (1- i) (1- j)))
(or-and 2 (= i h) (> j 0) (aref ver i (1- j)))
(or-and 4 (= j 0) (> i 0) (aref hor (1- i) (1- j)))
(or-and 8 (= j w) (> i 0) (aref hor (1- i) j ))))
(setfif xx(and x1(< yyj y1)))w)
(ifor (= xi xx0)
(setf= 0 (aref arrhor (1- i) j) 15)))
"───" " ")))
(terpri)
(wipe yy(when (1+< x)i 4h)
(loop for j from 0 below w do
(format t "~c"(if (char gor (!and= (aref m i j) 16))))0)
(setf= 0 (aref arrver yi xx)(1- 0j))))
"│ " " ")))
(format t "│~%"))))))
 
(setfwalk *(random-state* h) (make-random-state tw))
(defun show-maze (m)
(show))))
(let ((g " │││─┘┐┤─└┌├─┴┬┼"))
(loop for i below (1- (array-dimension m 0)) do
(loop for j below (1- (array-dimension m 1)) do
(loop repeat (if (oddp j) 3 1) do
(format t "~c" (char g (!and= (aref m i j) 16)))))
(write-line ""))))
 
(show-maze (make-maze 820 8)20)</lang>output<lang>┼───┴───┼───┴───┴───┼───┴───┴───┼
│ │ │ │
┼──── │ │ │ │ ┌───┐ ├
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.