Maze generation: Difference between revisions

(Added scala example)
Line 4,669:
+---+---+---+---+---+---+---+---+---+---+---+
</pre>
 
=={{header|TXR}}==
 
<lang txr>@(bind (width height) (15 15))
@(do
(defvar *r* (make-random-state nil))
 
(defun scramble (list)
(let ((out ()))
(each ((item list))
(let ((r (random *r* (+ 1 (length out)))))
(set [out r..r] (list item))))
out))
 
(defun make-maze-rec (w h vi pa cu)
(set [vi cu] t)
(let* ((x (car cu)) (y (cdr cu))
(adj (list (- x 1)..y (+ x 1)..y
x..(- y 1) x..(+ y 1))))
(each ((ne (scramble adj)))
(cond ((not [vi ne])
(push ne [pa cu])
(push cu [pa ne])
(make-maze-rec w h vi pa ne))))))
 
(defun make-maze (w h)
(let ((vi (hash :equal-based))
(pa (hash :equal-based)))
(each ((x (range -1 w)))
(set [vi x..-1] t)
(set [vi x..h] t))
(each ((y (range* 0 h)))
(set [vi -1..y] t)
(set [vi w..y] t))
(make-maze-rec w h vi pa 0..0)
pa))
 
(defun print-tops (pa w j)
(each ((i (range* 0 w)))
(if (memqual i..(- j 1) [pa i..j])
(format t "+ ")
(format t "+----")))
(format t "+\n"))
 
(defun print-sides (pa w j)
(let ((str ""))
(each ((i (range* 0 w)))
(if (memqual (- i 1)..j [pa i..j])
(set str `@str `)
(set str `@str| `)))
(format t "~a|\n~a|\n" str str)))
 
(defun print-maze (pa w h)
(each ((j (range* 0 h)))
(print-tops pa w j)
(print-sides pa w j))
(print-tops pa w h)))
@;;
@(bind m @(make-maze width height))
@(do (print-maze m width height))</lang>
 
Output:
 
<pre>+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| | | | | |
| | | | | |
+----+ + + + + +----+----+----+----+ +----+----+ + +
| | | | | | | |
| | | | | | | |
+ +----+----+----+ +----+----+ + + +----+ +----+----+ +
| | | | | | |
| | | | | | |
+----+----+----+ + + +----+----+----+----+ +----+ +----+ +
| | | | | | | |
| | | | | | | |
+ +----+ +----+ +----+ + +----+----+----+ +----+ + +
| | | | | | | |
| | | | | | | |
+ +----+----+ +----+----+ +----+ +----+ +----+----+ + +
| | | | | | | | | |
| | | | | | | | | |
+ + + +----+ +----+----+ +----+ +----+ + +----+ +
| | | | | | | | |
| | | | | | | | |
+ +----+----+----+ + + +----+ +----+----+ + + +----+
| | | | | | | | | |
| | | | | | | | | |
+ + +----+----+----+ +----+ +----+----+ + + + + +
| | | | | | | |
| | | | | | | |
+----+----+ +----+----+----+ +----+----+----+----+ + +----+ +
| | | | | | |
| | | | | | |
+ +----+----+ +----+----+----+ +----+ +----+ + + +----+
| | | | | | |
| | | | | | |
+ +----+----+----+----+----+----+----+ +----+ +----+ +----+ +
| | | | | | | | |
| | | | | | | | |
+----+ +----+----+ + + + +----+ + + +----+ + +
| | | | | | | | |
| | | | | | | | |
+ +----+----+ + +----+----+----+ +----+----+ + +----+----+
| | | | | | | | |
| | | | | | | | |
+----+----+----+----+----+ + + +----+----+ + + +----+ +
| | | |
| | | |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+</pre>
 
=={{header|XPL0}}==
543

edits