Advertisement
Guest User

Labyrinth2

a guest
May 21st, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PyCon 1.72 KB | None | 0 0
  1. #python 3.5.2
  2. from random import randint
  3.  
  4. UP = 1
  5. RIGHT = 2
  6. DOWN = 3
  7. LEFT= 4
  8.  
  9. graph = {}
  10. # graph = {'0:0': {'1': True, '3': True}, '0:1': {...}}
  11.  
  12. SIZE = 10
  13.  
  14. def addEdge(graph, x, y, direction):
  15.     directions = {
  16.         UP:     [0, -1],
  17.         RIGHT:  [1,  0],
  18.         DOWN:   [0,  1],
  19.         LEFT:   [-1, 0]
  20.     }
  21.     x2 = x+directions[direction][0]
  22.     y2 = y+directions[direction][1]
  23.     if (x2 < 1) or (x2 > SIZE) or (y2 < 1) or (y2 > SIZE):
  24.         return False
  25.     node1 = str(x)+':'+str(y)
  26.     node2 = str(x2)+':'+str(y2)
  27.     if node2 not in graph:
  28.         graph[node2] = {}
  29.     graph[node1][direction] = True
  30.     graph[node2][(direction+1)%4+1] = True
  31.     return True
  32.  
  33. for x in range(1,SIZE+1):
  34.     for y in range(1,SIZE+1):
  35.         directions = []
  36.         node = str(x)+':'+str(y)
  37.         if node not in graph:
  38.             graph[node] = {}
  39.         d = randint(1, 4)
  40.         while not addEdge(graph, x, y, d):
  41.             d = randint(1, 4)
  42.            
  43.         d2 = randint(1, 4)
  44.         while (d2 != d) and (not addEdge(graph, x, y, d2)):
  45.             d2 = randint(1, 4)
  46.        
  47. row = "#"
  48. for x in range(1,SIZE+2):
  49.     row = row + "###"
  50. print(row)
  51.  
  52. for y in range(1,SIZE+1):
  53.     row = "#  "
  54.     for x in range(1,SIZE+1):
  55.         key = str(x)+':'+str(y)
  56.         c = "  "
  57.         if (RIGHT in graph[key]):
  58.             c = "--"
  59.         row = row + "+" + c
  60.     print(row+"#")
  61.  
  62.     if y >= SIZE-1:
  63.         break
  64.    
  65.     row = "#  "
  66.     for x in range(1,SIZE+1):
  67.         key = str(x)+':'+str(y)
  68.         c = "  "
  69.         if (DOWN in graph[key]):
  70.             c = "| "
  71.         row = row + c + " "
  72.     print(row+"#")
  73.  
  74. row = "#"
  75. for x in range(1,SIZE+2):
  76.     row = row + "###"
  77. print(row)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement