Advertisement
Guest User

Generate linear road

a guest
Sep 25th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. import random
  2.  
  3. arr = [[n for n in range(1, 11)]]
  4. for i in range (0, 10):
  5.     arr.append([n for n in range(1, 11)])
  6.  
  7. def path(i, j):
  8.     arr[i][j] = 0
  9.     ni = 5
  10.     nj = 4
  11.     while ni > len(arr) - 1:
  12.         ni -= 1
  13.     dirs = [1, 2] #right and left
  14.     #Remove right
  15.     if j == len(arr[i]) - 1:
  16.         dirs.remove(1)
  17.     #Remove left
  18.     if j == 0:
  19.         dirs.remove(2)
  20.     #Choose random direction  
  21.     direction = random.choice(dirs)
  22.    
  23.     while nj > len(arr[i]) - 1 and direction == 1:
  24.         nj -= 1
  25.     while nj < 1 and direction == 2:
  26.         nj += 1
  27.     #Go down
  28.     for i in range(i, i + random.randrange(2, ni)):
  29.         arr[i][j] = 0
  30.     #Then go right
  31.     if direction == 1:
  32.         for j in range(j, j + random.randrange(1, nj)):
  33.             arr[i][j] = 0
  34.     #Or go left
  35.     elif direction == 2:
  36.         for j in range(j, j - random.randrange(1, nj), -1):
  37.             arr[i][j] = 0
  38.     try:
  39.         return path(i + 1, j)
  40.     except:
  41.         return None
  42.    
  43. path(0, random.randrange(0, 10))
  44. for i in range(0, len(arr)):
  45.     print(arr[i])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement