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''' |
|||
⚫ | |||
⚫ | |||
⚫ | |||
neighbors(cell::Vector, bound::Vector, step::Int=2) = |
|||
⚫ | |||
⚫ | |||
⚫ | |||
function walk(maze::Matrix, nxtcell::Vector, visited::Vector=[]) |
|||
⚫ | |||
for neigh in shuffle(neighbors(nxtcell, size(maze))) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
end |
|||
end |
end |
||
maze |
|||
maze |
|||
end |
end |
||
function maze(w::Int, h::Int) |
|||
⚫ | |||
firstcell = 2 * [rand(1:w), rand(1:h)] |
|||
return walk(maze, firstcell) |
|||
end</lang> |
|||
'''Printing functions''' |
|||
⚫ | |||
⚫ | |||
function printmaze(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 |
|||
⚫ | |||
⚫ | |||
end |
|||
⚫ | |||
mazewalls = collect(wall(i, j) for i in 1:2:h, j in 1:w) |
|||
⚫ | |||
pprint( |
pprint(mazewalls) |
||
wall[Int(sum(c-> 2.0^.5(3c[1]+c[2]+3), |
|||
filter(x -> maze[x...] != 0, |
|||
⚫ | |||
⚫ | |||
end |
end |
||
printmaze(maze(10, 10)) |
|||
</lang> |
</lang> |
||
{{out}} |
{{out}} |
||
<pre>┏━━━━━┳━━━━━┳━━━━━━━┓ |
|||
<pre> |
|||
┃ ╻ ╻ ┃ ╺━┓ ╹ ┏━━━┓ ┃ |
|||
julia> mprint(maze(15,30)) |
|||
┣━┛ ┃ ┗━┓ ┗━━━┛ ╻ ┃ ┃ |
|||
┏━━━━━━━━━━━━━━━┳━━━━━━━┳━━━┳━━━┳━━━━━━━┳━━━┳━━━━━━━━━┳━━━━━┓ |
|||
┃ ╺━┻━┓ ┃ ╺━┳━━━┛ ┃ ┃ |
|||
┃ ╺━━━┳━━━┓ ╺━━━┛ ╻ ┏━╸ ┃ ╻ ┃ ╻ ┃ ╻ ╺━┳━┛ ╻ ╹ ╺━┓ ╺━┓ ┗━━━╸ ┃ |
|||
┃ ╺━┓ ┃ ┗━━━┛ ┏━━━┛ ┃ |
|||
┣━━━┓ ╹ ╻ ┣━━━━━━━┫ ┃ ┏━┛ ┃ ╹ ┃ ┃ ┣━┓ ╹ ┏━┻━━━┳━┻━┓ ┗━━━┳━━━┫ |
|||
┣━━━┛ ┣━━━━━┳━┛ ┏━━━┫ |
|||
┃ ╺━┻━━━┫ ┃ ╺━┳━╸ ┃ ┗━┛ ┏━┻━┓ ┃ ┃ ┃ ┗━━━┛ ╻ ┏━┛ ╻ ┗━┓ ╻ ╹ ╻ ┃ |
|||
┃ ┏━━━┛ ┏━╸ ┃ ╺━┛ ╻ ┃ |
|||
┃ ╺━━━┓ ┃ ┃ ┏━┛ ┏━┻━━━━━┫ ╻ ┗━┛ ┃ ┃ ╻ ┏━┳━┛ ┃ ╺━╋━╸ ┃ ┗━┳━┛ ┃ |
|||
┣━┛ ┏━━━┛ ╻ ┣━━━━━┛ ┃ |
|||
┣━━━╸ ┃ ╹ ┃ ┃ ╻ ┃ ╺━┓ ╻ ┃ ┗━┳━┓ ┃ ┗━┛ ┃ ╹ ┏━╋━╸ ┃ ╺━╋━━━┛ ┏━┫ |
|||
┃ ┏━┛ ┏━━━┻━┛ ┏━━━┳━┫ |
|||
┃ ┏━━━┻━━━┻━┛ ┣━┻━╸ ┃ ┗━┻━┓ ┃ ╹ ┗━━━┳━┛ ┏━┛ ┃ ╺━┻━━━┫ ╺━┳━┛ ┃ |
|||
┃ ┃ ╺━┛ ╺━━━━━┛ ╻ ╹ ┃ |
|||
┃ ┗━━━━━┓ ┏━╸ ┃ ╺━┳━┻━┓ ╺━┫ ┗━━━━━━━┛ ┏━┛ ┏━┻━━━━━┓ ┗━┓ ┃ ╺━┫ |
|||
┗━┻━━━━━━━━━━━━━┻━━━┛</pre> |
|||
┣━━━━━╸ ┃ ┗━━━┻━┓ ┃ ╻ ┗━┓ ┗━━━━━━━┳━━━┫ ╺━┛ ┏━┓ ╺━╋━╸ ┃ ┗━┓ ┃ |
|||
┃ ┏━━━┳━┻━┓ ╻ ╺━┛ ╹ ┣━┓ ┣━┓ ╺━━━┳━┛ ╻ ╹ ┏━━━┛ ┗━┓ ┃ ╺━┻━╸ ┃ ┃ |
|||
┃ ╹ ╻ ╹ ╻ ┗━┻━━━┳━━━┫ ╹ ╹ ┗━┳━┓ ╹ ┏━┻━━━┫ ╺━┳━╸ ┃ ┗━━━┳━━━┛ ┃ |
|||
┣━┳━┻━━━┻━━━━━┓ ╹ ╻ ┗━┳━━━╸ ╹ ┗━━━┛ ┏━╸ ┗━╸ ┃ ┏━┻━┓ ╻ ┗━━━╸ ┃ |
|||
┃ ╹ ╻ ╺━━━┳━╸ ┣━━━┻━┓ ┃ ┏━━━┓ ┏━━━━━┻━┳━━━━━┫ ┃ ╻ ╹ ┣━┳━━━╸ ┃ |
|||
┃ ╺━╋━━━┓ ┗━━━┫ ╻ ╺━┫ ┃ ┃ ╻ ┗━┛ ┏━━━┓ ╹ ┏━┓ ┗━┫ ┣━━━┫ ╹ ┏━━━┫ |
|||
┣━╸ ┗━╸ ┗━━━┓ ╹ ┗━┓ ╹ ┗━┛ ┣━━━━━┛ ╻ ┗━━━┛ ┗━╸ ╹ ┃ ╺━┛ ╺━┛ ╻ ┃ |
|||
┗━━━━━━━━━━━┻━━━━━┻━━━━━━━┻━━━━━━━┻━━━━━━━━━━━━━┻━━━━━━━━━┻━┛</pre> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |