Maze generation: Difference between revisions

J draft
m (moved Maze to Maze generation)
(J draft)
Line 2:
 
Generate and show a maze, using using the simple [http://en.wikipedia.org/wiki/Maze_generation_algorithm#Depth-first_search Depth-first search] algorithm.
 
=={{header|J}}==
 
From the picolisp implementation, except without any grid support:
 
<lang j>maze=:4 :0
horiz=. 0$~x,y-1
verti=. 0$~(x-1),y
unvisited=. 0,0,~|:0,0,~1$~y,x
path=.,:here=. ?x,y
while.#path do.
unvisited=.0 (<1+here)} unvisited
neighbors=. here+"1 (,-)0 1 ,:1 0
neighbors=. neighbors #~ (<"1 neighbors+1) {unvisited
if.#neighbors do.
next=. ({~ ?@#) neighbors
if.{.next=here
do. horiz=.1 (<-:here+next-0 3)} horiz
else.verti=. 1 (<-:here+next-3 0)} verti end.
path=.path,here=.next
else.
here=.{:path
path=.}:path
end.
end.
(horiz,.1{.~-x);1 0,verti
)
 
display=:3 :0
size=. 2+$>{.y
text=. (}:1 3$~2*1+{:size)#"1":size$<' '
'hdoor vdoor'=. 2 4 *L:0 (#&,{@;&i./@$)&.> y
2 4}._2 _4}.' ' (3 8+L:0 hdoor)} ' ' (vdoor+L:0"0/2 5;2 6;2 7)} text
)</lang>
 
Example use (with ascii box drawing enabled):
 
<lang j> display 12 maze 16
+ +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| | | | |
+ + + +---+ +---+---+ +---+ +---+ +---+---+---+ +
| | | | | | | | | |
+ + +---+ + + +---+ + + + +---+ + +---+ +
| | | | | | | | | | |
+ +---+---+---+ +---+---+---+---+---+ + + +---+ + +
| | | | | | | | | |
+---+ + +---+---+ +---+---+---+ + + +---+ +---+---+
| | | | | | | |
+ + +---+---+ +---+---+ + +---+ +---+ +---+---+ +
| | | | | | | | |
+ +---+---+ +---+ + +---+ + +---+---+---+---+ + +
| | | | | | | | |
+---+---+ + +---+ +---+ +---+---+---+ + +---+---+ +
| | | | | | | | |
+ +---+ + + +---+ +---+---+---+ + + + + + +
| | | | | | | | | |
+ + + + + + + + + +---+---+---+---+---+---+ +
| | | | | | | | | |
+ + + +---+ + +---+---+ + + +---+---+ + + +
| | | | | | | | | | | |
+ + +---+ +---+ +---+---+ +---+ +---+---+---+ + +
| | | | | |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+</lang>
 
 
 
 
=={{header|PicoLisp}}==
6,962

edits