Skip to content

Instantly share code, notes, and snippets.

@regehr
Created April 3, 2017 02:39
Show Gist options
  • Save regehr/aaf5e3954bf84796d4100e5f9f53c15d to your computer and use it in GitHub Desktop.
Save regehr/aaf5e3954bf84796d4100e5f9f53c15d to your computer and use it in GitHub Desktop.
mst-based random maze generator
#!/usr/bin/python3
import random
# dimensions should be odd and >= 5
W = 25
H = 25
q = []
m = []
for y in range(H):
l = []
for x in range(W):
l.append(' ')
m.append(l)
q.append((1, 1, 1, 3))
q.append((1, 1, 3, 1))
while len(q) > 0:
(x1, y1, x2, y2) = q.pop(random.randrange(len(q)))
if m[y2][x2] != ' ':
continue
m[y1][x1] = '#'
m[int((y1 + y2) / 2)][int((x1 + x2) / 2)] = '#'
m[y2][x2] = '#'
if x2 > 1:
q.append((x2, y2, x2 - 2, y2))
if y2 > 1:
q.append((x2, y2, x2, y2 - 2))
if x2 < W - 2:
q.append((x2, y2, x2 + 2, y2))
if y2 < H - 2:
q.append((x2, y2, x2, y2 + 2))
for y in range(H):
for x in range(W):
print(m[x][y],end='')
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment