Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
27
Task/Maze-generation/Python/maze-generation.py
Normal file
27
Task/Maze-generation/Python/maze-generation.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
from random import shuffle, randrange
|
||||
|
||||
def make_maze(w = 16, h = 8):
|
||||
vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
|
||||
ver = [["| "] * w + ['|'] for _ in range(h)] + [[]]
|
||||
hor = [["+--"] * w + ['+'] for _ in range(h + 1)]
|
||||
|
||||
def walk(x, y):
|
||||
vis[y][x] = 1
|
||||
|
||||
d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
|
||||
shuffle(d)
|
||||
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(xx, yy)
|
||||
|
||||
walk(randrange(w), randrange(h))
|
||||
|
||||
s = ""
|
||||
for (a, b) in zip(hor, ver):
|
||||
s += ''.join(a + ['\n'] + b + ['\n'])
|
||||
return s
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(make_maze())
|
||||
Loading…
Add table
Add a link
Reference in a new issue