Anonymous user
Maze generation: Difference between revisions
Refactored code
(Refactored code) |
|||
Line 3,789:
=={{header|Julia}}==
{{works with|Julia|>0.6}}
'''Generating functions'''
<lang julia>function walk(maze, cell, visited = Any[])▼
push!(visited, cell)▼
if !(neigh in visited)▼
maze[round.(Int,(cell+neigh)/2)...] = 0▼
for neigh in shuffle(neighbors(nxtcell, size(maze)))
end
end
end
function maze(w::Int, h::Int)
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...])
h, w = size(maze)▼
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
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(
▲ 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>
=={{header|Kotlin}}==
|