Maze generation: Difference between revisions

Content added Content deleted
(Refactored code)
Line 3,789: Line 3,789:


=={{header|Julia}}==
=={{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, size(maze)))
neighbors(cell::Vector, bound::Vector, step::Int=2) =
filter(check(bound), map(dir -> cell + step * dir, [[0, 1], [-1, 0], [0, -1], [1, 0]]))
if !(neigh in visited)

maze[round.(Int,(cell+neigh)/2)...] = 0
walk(maze, neigh, visited)
function walk(maze::Matrix, nxtcell::Vector, visited::Vector=[])
push!(visited, nxtcell)
for neigh in shuffle(neighbors(nxtcell, size(maze)))
if neigh visited
maze[round.(Int, (nxtcell + neigh) / 2)...] = 0
walk(maze, neigh, visited)
end
end
end
end
maze
maze
end
end
function maze(w::Int, h::Int)
maze = collect(i % 2 | j % 2 for i in 1:2w+1, j in 1: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...])
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)) .- [[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 ? ' ' :
pprint(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
end

printmaze(maze(10, 10))
</lang>
</lang>

{{out}}
{{out}}
<pre>┏━━━━━┳━━━━━┳━━━━━━━┓
<pre>
┃ ╻ ╻ ┃ ╺━┓ ╹ ┏━━━┓ ┃
julia> mprint(maze(15,30))
┣━┛ ┃ ┗━┓ ┗━━━┛ ╻ ┃ ┃
┏━━━━━━━━━━━━━━━┳━━━━━━━┳━━━┳━━━┳━━━━━━━┳━━━┳━━━━━━━━━┳━━━━━┓
┃ ╺━┻━┓ ┃ ╺━┳━━━┛ ┃ ┃
┃ ╺━━━┳━━━┓ ╺━━━┛ ╻ ┏━╸ ┃ ╻ ┃ ╻ ┃ ╻ ╺━┳━┛ ╻ ╹ ╺━┓ ╺━┓ ┗━━━╸ ┃
┃ ╺━┓ ┃ ┗━━━┛ ┏━━━┛ ┃
┣━━━┓ ╹ ╻ ┣━━━━━━━┫ ┃ ┏━┛ ┃ ╹ ┃ ┃ ┣━┓ ╹ ┏━┻━━━┳━┻━┓ ┗━━━┳━━━┫
┣━━━┛ ┣━━━━━┳━┛ ┏━━━┫
┃ ╺━┻━━━┫ ┃ ╺━┳━╸ ┃ ┗━┛ ┏━┻━┓ ┃ ┃ ┃ ┗━━━┛ ╻ ┏━┛ ╻ ┗━┓ ╻ ╹ ╻ ┃
┃ ┏━━━┛ ┏━╸ ┃ ╺━┛ ╻ ┃
┃ ╺━━━┓ ┃ ┃ ┏━┛ ┏━┻━━━━━┫ ╻ ┗━┛ ┃ ┃ ╻ ┏━┳━┛ ┃ ╺━╋━╸ ┃ ┗━┳━┛ ┃
┣━┛ ┏━━━┛ ╻ ┣━━━━━┛ ┃
┣━━━╸ ┃ ╹ ┃ ┃ ╻ ┃ ╺━┓ ╻ ┃ ┗━┳━┓ ┃ ┗━┛ ┃ ╹ ┏━╋━╸ ┃ ╺━╋━━━┛ ┏━┫
┃ ┏━┛ ┏━━━┻━┛ ┏━━━┳━┫
┃ ┏━━━┻━━━┻━┛ ┣━┻━╸ ┃ ┗━┻━┓ ┃ ╹ ┗━━━┳━┛ ┏━┛ ┃ ╺━┻━━━┫ ╺━┳━┛ ┃
┃ ┃ ╺━┛ ╺━━━━━┛ ╻ ╹ ┃
┃ ┗━━━━━┓ ┏━╸ ┃ ╺━┳━┻━┓ ╺━┫ ┗━━━━━━━┛ ┏━┛ ┏━┻━━━━━┓ ┗━┓ ┃ ╺━┫
┗━┻━━━━━━━━━━━━━┻━━━┛</pre>
┣━━━━━╸ ┃ ┗━━━┻━┓ ┃ ╻ ┗━┓ ┗━━━━━━━┳━━━┫ ╺━┛ ┏━┓ ╺━╋━╸ ┃ ┗━┓ ┃
┃ ┏━━━┳━┻━┓ ╻ ╺━┛ ╹ ┣━┓ ┣━┓ ╺━━━┳━┛ ╻ ╹ ┏━━━┛ ┗━┓ ┃ ╺━┻━╸ ┃ ┃
┃ ╹ ╻ ╹ ╻ ┗━┻━━━┳━━━┫ ╹ ╹ ┗━┳━┓ ╹ ┏━┻━━━┫ ╺━┳━╸ ┃ ┗━━━┳━━━┛ ┃
┣━┳━┻━━━┻━━━━━┓ ╹ ╻ ┗━┳━━━╸ ╹ ┗━━━┛ ┏━╸ ┗━╸ ┃ ┏━┻━┓ ╻ ┗━━━╸ ┃
┃ ╹ ╻ ╺━━━┳━╸ ┣━━━┻━┓ ┃ ┏━━━┓ ┏━━━━━┻━┳━━━━━┫ ┃ ╻ ╹ ┣━┳━━━╸ ┃
┃ ╺━╋━━━┓ ┗━━━┫ ╻ ╺━┫ ┃ ┃ ╻ ┗━┛ ┏━━━┓ ╹ ┏━┓ ┗━┫ ┣━━━┫ ╹ ┏━━━┫
┣━╸ ┗━╸ ┗━━━┓ ╹ ┗━┓ ╹ ┗━┛ ┣━━━━━┛ ╻ ┗━━━┛ ┗━╸ ╹ ┃ ╺━┛ ╺━┛ ╻ ┃
┗━━━━━━━━━━━┻━━━━━┻━━━━━━━┻━━━━━━━┻━━━━━━━━━━━━━┻━━━━━━━━━┻━┛</pre>


=={{header|Kotlin}}==
=={{header|Kotlin}}==