Maze generation: Difference between revisions

Refactored code
(Refactored code)
Line 3,789:
 
=={{header|Julia}}==
{{works with|Julia|>0.6}}
The following code is for julia version >= 0.6.
 
'''Generating functions'''
<lang julia>function walk(maze, cell, visited = Any[])
<lang julia>check(bound::Vector) = cell -> all([1, 1] .<= cell .<= [bound...])
push!(visited, cell)
for neigh in shuffle(neighbors(cell::Vector, size(maze))bound::Vector, step::Int=2) =
neighbors(c,b,d=2)= filter(check(bbound), map(mdir ->c cell +d step *m dir, Any[[0, 1], [-1, 0], [0, -1], [1, 0]]))
if !(neigh in visited)
 
maze[round.(Int,(cell+neigh)/2)...] = 0
function walk(maze::Matrix, neighnxtcell::Vector, visited::Vector=[])
push!(visited, cellnxtcell)
for neigh in shuffle(neighbors(nxtcell, size(maze)))
if !(neigh in visited)
maze[round.(Int, (cellnxtcell + neigh) / 2)...] = 0
<lang julia>function walk(maze, cellneigh, visited = Any[])
end
end
end maze
maze
end
function maze(w::Int, h::Int)
maze = collect(i % 2 | j % 2 for i =in 1:2:h2w+1, j =in 1:w]2h+1)
firstcell = 2 * [rand(1:w), rand(1:h)]
return walk(maze, firstcell)
end</lang>
 
'''Printing functions'''
neighbors(c,b,d=2)=filter(check(b),map(m->c+d*m, Any[[0,1],[-1,0],[0,-1],[1,0]]))
<lang julia>pprint(matrix) = for i = 1:size(matrix, 1) println(join(matrix[i, :])) end
 
function printmaze(maze)
check(bound) = cell -> all([1,1] .<= cell .<= [bound...])
function printmaze(maze, wall walls = split("╹ ╸ ┛ ╺ ┗ ━ ┻ ╻ ┃ ┓ ┫ ┏ ┣ ┳ ╋"))
 
h, w = size(maze)
maze(w, h) = walk([i%2|j%2 for i=1:2w+1,j=1:2h+1], 2*[rand(1:w),rand(1:h)])
f = cell -> 2 ^ ((3cell[1] + cell[2] + 3) / 2)
 
wall(i, j) = if maze[i,j] == 0 " " else
pprint(matrix) = for i = 1:size(matrix,1) println(join(matrix[i,:])) end
walls[Int(sum(f, filter(x -> maze[x...] != 0, neighbors([i, j], [h, w], 1)) .- Any[[i, j]]))]
 
end
function printmaze(maze, wall = split("╹ ╸ ┛ ╺ ┗ ━ ┻ ╻ ┃ ┓ ┫ ┏ ┣ ┳ ╋"))
mazewalls = collect(wall(i, j) for i in 1:2:h, j in 1:w)
h,w = size(maze)
pprint([ maze[i,j] == 0 ? ' ' :mazewalls)
wall[Int(sum(c-> 2.0^.5(3c[1]+c[2]+3),
filter(x -> maze[x...] != 0,
neighbors([i,j],[h,w],1)) .- Any[[i,j]]))]
for i = 1:2:h, j = 1:w])
end
 
printmaze(maze(10, 10))
</lang>
 
{{out}}
<pre>┏━━━━━┳━━━━━┳━━━━━━━┓
<pre>
┃ ╻ ╻ ┃ ╺━┓ ╹ ┏━━━┓ ┃
julia> mprint(maze(15,30))
┣━┛ ┃ ┗━┓ ┗━━━┛ ╻ ┃ ┃
┏━━━━━━━━━━━━━━━┳━━━━━━━┳━━━┳━━━┳━━━━━━━┳━━━┳━━━━━━━━━┳━━━━━┓
┃ ╺━┻━┓ ┃ ╺━┳━━━┛ ┃ ┃
┃ ╺━━━┳━━━┓ ╺━━━┛ ╻ ┏━╸ ┃ ╻ ┃ ╻ ┃ ╻ ╺━┳━┛ ╻ ╹ ╺━┓ ╺━┓ ┗━━━╸ ┃
┃ ╺━┓ ┃ ┗━━━┛ ┏━━━┛ ┃
┣━━━┓ ╹ ╻ ┣━━━━━━━┫ ┃ ┏━┛ ┃ ╹ ┃ ┃ ┣━┓ ╹ ┏━┻━━━┳━┻━┓ ┗━━━┳━━━┫
┣━━━┛ ┣━━━━━┳━┛ ┏━━━┫
┃ ╺━┻━━━┫ ┃ ╺━┳━╸ ┃ ┗━┛ ┏━┻━┓ ┃ ┃ ┃ ┗━━━┛ ╻ ┏━┛ ╻ ┗━┓ ╻ ╹ ╻ ┃
┃ ┏━━━┛ ┏━╸ ┃ ╺━┛ ╻ ┃
┃ ╺━━━┓ ┃ ┃ ┏━┛ ┏━┻━━━━━┫ ╻ ┗━┛ ┃ ┃ ╻ ┏━┳━┛ ┃ ╺━╋━╸ ┃ ┗━┳━┛ ┃
┣━┛ ┏━━━┛ ╻ ┣━━━━━┛ ┃
┣━━━╸ ┃ ╹ ┃ ┃ ╻ ┃ ╺━┓ ╻ ┃ ┗━┳━┓ ┃ ┗━┛ ┃ ╹ ┏━╋━╸ ┃ ╺━╋━━━┛ ┏━┫
┃ ┏━┛ ┏━━━┻━┛ ┏━━━┳━┫
┃ ┏━━━┻━━━┻━┛ ┣━┻━╸ ┃ ┗━┻━┓ ┃ ╹ ┗━━━┳━┛ ┏━┛ ┃ ╺━┻━━━┫ ╺━┳━┛ ┃
┃ ┃ ╺━┛ ╺━━━━━┛ ╻ ╹ ┃
┃ ┗━━━━━┓ ┏━╸ ┃ ╺━┳━┻━┓ ╺━┫ ┗━━━━━━━┛ ┏━┛ ┏━┻━━━━━┓ ┗━┓ ┃ ╺━┫
┗━┻━━━━━━━━━━━━━┻━━━┛</pre>
┣━━━━━╸ ┃ ┗━━━┻━┓ ┃ ╻ ┗━┓ ┗━━━━━━━┳━━━┫ ╺━┛ ┏━┓ ╺━╋━╸ ┃ ┗━┓ ┃
┃ ┏━━━┳━┻━┓ ╻ ╺━┛ ╹ ┣━┓ ┣━┓ ╺━━━┳━┛ ╻ ╹ ┏━━━┛ ┗━┓ ┃ ╺━┻━╸ ┃ ┃
┃ ╹ ╻ ╹ ╻ ┗━┻━━━┳━━━┫ ╹ ╹ ┗━┳━┓ ╹ ┏━┻━━━┫ ╺━┳━╸ ┃ ┗━━━┳━━━┛ ┃
┣━┳━┻━━━┻━━━━━┓ ╹ ╻ ┗━┳━━━╸ ╹ ┗━━━┛ ┏━╸ ┗━╸ ┃ ┏━┻━┓ ╻ ┗━━━╸ ┃
┃ ╹ ╻ ╺━━━┳━╸ ┣━━━┻━┓ ┃ ┏━━━┓ ┏━━━━━┻━┳━━━━━┫ ┃ ╻ ╹ ┣━┳━━━╸ ┃
┃ ╺━╋━━━┓ ┗━━━┫ ╻ ╺━┫ ┃ ┃ ╻ ┗━┛ ┏━━━┓ ╹ ┏━┓ ┗━┫ ┣━━━┫ ╹ ┏━━━┫
┣━╸ ┗━╸ ┗━━━┓ ╹ ┗━┓ ╹ ┗━┛ ┣━━━━━┛ ╻ ┗━━━┛ ┗━╸ ╹ ┃ ╺━┛ ╺━┛ ╻ ┃
┗━━━━━━━━━━━┻━━━━━┻━━━━━━━┻━━━━━━━┻━━━━━━━━━━━━━┻━━━━━━━━━┻━┛</pre>
 
=={{header|Kotlin}}==
Anonymous user