Maze generation: Difference between revisions

→‎{{header|Python}}: replacing both method (they were pretty similar)
(→‎{{header|Python}}: replacing both method (they were pretty similar))
Line 1,448:
 
=={{header|Python}}==
<lang python>from random import choiceshuffle, randrange
 
def make_maze(heightw = 16, widthh = 8):
vis = [[0] * connw =+ [ [[1] for j_ in range(widthh)] for+ i[[1] in* range(height)w + 1)]
visited ver = [[False"| "] * widthw + ['|'] for i_ in range(heighth)] + [[]]
seen hor = [[False"+--"] * Hw + ['+'] for _ in xrangerange(Wh + 1)]
 
def neighborswalk(x, y):
vis[y][x] = 1
return [ (i, j) for (i, j) in ((y+1, x), (y-1, x), (y, x+1), (y, x-1))
if j >= 0 and j < width and i >= 0 and i < height
if not visited[i][j] ]
 
d = [(x - def1, y), walk(x, y + 1), (x + 1, avail=width*heighty), (x, y - 1):]
shuffle(d)
visited[y][x] = True
for (xx, yy) in d:
if vis[yy][xx]: continue
if xx == x: hor[max(y, yy)][x] = "+ "
if yy == y: ver[y][max(x, xx)] = " "
walk(0xx, 0yy)
 
visit walk(randrange(Ww), randrange(Hh))
while avail:
for (a, b) in zip(hor, ver):
n = neighbors(x, y)
print(""''.join(s1a) + ["*'\n"]' + s2 + ["|"]''.join(b))
if not len(n): return avail
 
show_maze(make_maze(8, 11))</lang>output<lang>+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
(i,j) = choice(n)
| | conn[y][x].append((i,j)) | | | |
+ + + + + + conn[i][j].append((y,x))+ + +--+--+--+--+--+ +--+ +
| | | | | | | | | |
 
+--+ + --+--+ + +--+--+--+ + +--+ +--+--+ +
avail = walk(i, j, avail - 1)
| | | | | | if| not visited[i][j] ] | | |
 
+ +--+ +--+ +-- + + + + +--+ + + +--+--+
walk(0, 0)
| | | | | | | | | |
return conn
+ +--+ +--+--+ + +--+ +--+--+ +--+--+ + +
 
| | | | | | | |
def show_maze(cells):
+--+ + + +--+--+--+ +--+--+ --+--+--+--+--+ +
w, h = len(cells[0]), len(cells)
| | | | | | | |
for i in range(h):
+ +--+--+ +--+--+ s1 = ["+--+--+ " if (i+-1,-+ j) in cells[i][j] else "+--"+ for j+ in range(w)]+ +
| s2| = [" " if (i, j-1)| in cells[i][j] else "| " for j in range(w)] | | | |
+ +--+ +--+--+--+ + +--+--+--+--+ +--+ + +
print("".join(s1 + ["*\n"] + s2 + ["|"]))
| | | if j >= 0 and j < width and i >= 0 and i < height | |
 
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+</lang>
print("*--" * w + "*")
 
show_maze(make_maze(8, 11))</lang>output<lang>+--+--+--+--+--+--+--+--+--+--+--+
| | | | |
+--+ + +--+ + + +--+ +--+ +
| | | | | | | |
+ + + +--+--+ +--+ +--+ + +
| | | | | | | |
+ +--+ + + +--+ +--+--+ + +
| | | | | | |
+--+ +--+ +--+ +--+ + +--+--+
| | | | | | |
+ + + +--+ +--+--+ +--+--+ +
| | | | | | |
+ + +--+ +--+--+--+--+ + + +
| | | | | | |
+ +--+--+--+ +--+--+ + + + +
| | | |
+--+--+--+--+--+--+--+--+--+--+--+</lang>
===Alternative version===
{{trans|D}}
<lang python>from random import randrange, choice
from collections import namedtuple
 
W, H = 11, 8
hWalls = [[True] * H for _ in xrange(W)]
vWalls = [[True] * H for _ in xrange(W)]
seen = [[False] * H for _ in xrange(W)]
P = namedtuple("P", "x y")
 
def visit(x, y):
seen[x][y] = True
D = [P(x-1,y), P(x+1,y), P(x,y-1), P(x,y+1)]
ns = [p for p in D if p.x>=0 and p.x<W and p.y>=0 and p.y<H]
while ns:
n = choice(ns)
if not seen[n.x][n.y]:
if x != n.x: hWalls[min(x, n.x)][y] = False
else: vWalls[x][min(y, n.y)] = False
visit(*n)
ns = [x for x in ns if x != n]
 
visit(randrange(W), randrange(H))
 
print "+" + ("--+" * W)
for y in xrange(H):
s = "|"
for x in xrange(W):
s += " |" if hWalls[x][y] else " "
s += "\n+"
for x in xrange(W):
s += "--+" if vWalls[x][y] else " +"
print s</lang>
 
=={{header|Tcl}}==
Anonymous user