Skip to content

Instantly share code, notes, and snippets.

@regehr
Created April 3, 2017 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save regehr/899ccb541315f8dc6d7cad84a43b4353 to your computer and use it in GitHub Desktop.
Save regehr/899ccb541315f8dc6d7cad84a43b4353 to your computer and use it in GitHub Desktop.
#!/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