Advertisement
here2share

# b_ascii_maze.py

Mar 31st, 2021
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. # b_ascii_maze.py
  2.  
  3. from random import shuffle, randrange
  4.  
  5. def make_maze(w = 38, h = 14):
  6.     vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
  7.     ver = [["|  "] * w + ['|'] for _ in range(h)] + [[]]
  8.     hor = [["+--"] * w + ['+'] for _ in range(h + 1)]
  9.  
  10.     def walk(x, y):
  11.         vis[y][x] = 1
  12.  
  13.         d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
  14.         shuffle(d)
  15.         for (xx, yy) in d:
  16.             if vis[yy][xx]: continue
  17.             if xx == x: hor[max(y, yy)][x] = "+  "
  18.             if yy == y: ver[y][max(x, xx)] = "   "
  19.             walk(xx, yy)
  20.  
  21.     walk(randrange(w), randrange(h))
  22.  
  23.     s = ""
  24.     for (a, b) in zip(hor, ver):
  25.         s += ''.join(a + ['\n'] + b + ['\n'])
  26.     return s
  27.  
  28. if __name__ == '__main__':
  29.     print(make_maze())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement