Maze generation: Difference between revisions

mNo edit summary
Line 4,182:
│ │ │ │ │ │
└───────────┴───────────────────────────────────────────┴───────────────────────────────────┴───────────────────┴ │ ┘</pre></small>
 
=={{header|Phix}}==
Adapted a couple of techniques from the excellent D submission<br>
(however this holds the grid as an array of complete lines)
<lang Phix>--
-- grid is eg for w=3,h=2: {"+---+---+---+", -- ("wall")
-- "| ? | ? | ? |", -- ("cell")
-- "+---+---+---+", -- ("wall")
-- "| ? | ? | ? |", -- ("cell")
-- "+---+---+---+", -- ("wall")
-- ""} -- (for a trailing \n)
--
-- note those ?(x,y) are at [y*2][x*4-1], and we navigate
-- using y=2..2*h (+/-2), x=3..w*4-1 (+/-4) directly.
--
constant w = 11, h = 8
 
sequence wall = join(repeat("+",w+1),"---")&"\n",
cell = join(repeat("|",w+1)," ? ")&"\n",
grid = split(join(repeat(wall,h+1),cell),'\n')
 
procedure amaze(integer x, integer y)
grid[y][x] = ' ' -- mark cell visited
sequence p = shuffle({{x-4,y},{x,y+2},{x+4,y},{x,y-2}})
for i=1 to length(p) do
integer {nx,ny} = p[i]
if nx>1 and nx<w*4 and ny>1 and ny<=2*h and grid[ny][nx]='?' then
integer mx = (x+nx)/2
grid[(y+ny)/2][mx-1..mx+1] = ' ' -- knock down wall
amaze(nx,ny)
end if
end for
end procedure
 
function heads()
return rand(2)=1 -- 50:50 true(1)/false(0)
end function
 
integer {x,y} = {(rand(w)*4)-1,rand(h)*2}
amaze(x,y)
-- mark start pos
grid[y][x] = '*'
-- add a random door (heads=rhs/lhs, tails=top/btm)
if heads() then
grid[rand(h)*2][heads()*w*4-1] = ' '
else
x = rand(w)*4-1
grid[heads()*h*2+1][x-1..x+1] = ' '
end if
puts(1,join(grid,'\n'))</lang>
{{out}}
<pre>
+---+---+---+---+---+---+---+---+---+---+---+
| | | |
+ + +---+---+---+---+---+---+ + + +
| | | | * | | |
+ +---+ + +---+---+---+ + +---+---+
| | | | | |
+ + +---+ +---+ +---+ +---+---+ +
| | | | | | | |
+ +---+ + + +---+ + + +---+---+
| | | | | | |
+ + +---+---+---+ +---+---+---+---+ +
| | | | |
+ +---+---+---+ +---+ + + +---+ +
| | | | | |
+ +---+ + +---+---+---+---+---+ +---+
| |
+---+---+---+---+---+---+---+---+---+---+---+
</pre>
 
=={{header|PicoLisp}}==
7,820

edits