Skip to content

Instantly share code, notes, and snippets.

@subhashdasyam
Last active January 9, 2017 04:33
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 subhashdasyam/56534c63ef0b5f0f714a5c3a162eddc4 to your computer and use it in GitHub Desktop.
Save subhashdasyam/56534c63ef0b5f0f714a5c3a162eddc4 to your computer and use it in GitHub Desktop.
Maze Recursion 100x100 n 200x200
import random
import sys
sys.setrecursionlimit(5000)
def make_maze(columns=20, rows=20):
columns = columns / 2
rows = rows / 2
visited = [[0] * columns + [1] for _ in range(rows)] + [[1] * (columns + 1)]
ver = [["* "] * columns + ['*'] for _ in range(rows)] + [[]]
hor = [["**"] * columns + ['*'] for _ in range(rows + 1)]
def walk(x, y):
visited[y][x] = 1
d = [
(x - 1, y),
(x, y + 1),
(x + 1, y),
(x, y - 1)
]
random.shuffle(d)
for (xx, yy) in d:
if visited[yy][xx]:
continue
if xx == x:
hor[max(y, yy)][x] = "* "
if yy == y:
ver[y][max(x, xx)] = " "
walk(xx, yy)
walk(
random.randrange(columns),
random.randrange(rows)
)
s = ""
for (a, b) in zip(hor, ver):
s += ''.join(a + ['\n'] + b + ['\n'])
return s
if __name__ == '__main__':
print(make_maze(columns=200, rows=200))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment