Maze generation: Difference between revisions

Content added Content deleted
Line 2,594: Line 2,594:
<a id='solve' style='display:none' href='javascript:solve(); void(0)'>Solve</a>
<a id='solve' style='display:none' href='javascript:solve(); void(0)'>Solve</a>
</fieldset></form><table id='maze'/></body></html></lang>
</fieldset></form><table id='maze'/></body></html></lang>

=={{header|Julia}}==
<lang julia>function neighbors (cell, bound, d = 2)
filter(cell -> all([1,1] .<= cell .<= [bound...]),
map(mov -> cell+d*mov, {[0,1],[1,0],[-1,0],[0,-1]}))
end

function remwall (maze, cell, visited = {})
push!(visited, cell)
for neigh in shuffle(neighbors(cell, size(maze)))
if !(neigh in visited)
maze[neigh...] = maze[int((cell+neigh)/2)...] = ' '
remwall(maze, neigh, visited)
end
end
end

function randmaze (w, h, block = '\u2588')
let maze = [block for i = 1:2w+1, j = 1:2h+1],
rcell = 2+2ifloor([w,h].*rand(2))
maze[rcell...] = ' '
remwall(maze, rcell)
maze
end
end

mprint(maze) = print(replace((@sprintf "%s" maze),'\t', ""))

function pprint(maze)
let walls = CharString(" ╺╹┗╸━┛┻╻┏┃┣┓┳┫╋"...),
w = size(maze,1), h = size(maze,2),
hash = x -> {[0,1] => 1, [-1,0] => 2, [0,-1] => 4, [1,0] => 8}[x]
mprint([maze[i,j] == ' ' ? ' ' :
walls[1+sum(hash,filter(x->maze[x...] != ' ',
neighbors([i,j],[w,h],1)) .- {[i,j]})]
for i = 1:2:w, j = 1:h])
end
end

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


=={{header|Mathematica}}==
=={{header|Mathematica}}==