here2share

# Tk_avoidance_walk_5.py

Mar 13th, 2022 (edited)
981
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. # Tk_avoidance_walk_5.py
  2.  
  3. import random
  4. from Tkinter import *
  5. from math import *
  6.  
  7. ri = random.randint
  8. rs = random.shuffle
  9. rc = random.choice
  10.  
  11. ww = 600
  12. hh = 600
  13.  
  14. root = Tk()
  15. root.title("Tk_avoidance_walk")
  16. root.geometry("%dx%d+-6+-2"%(ww,hh))
  17. cv = Canvas(width=ww, height=hh, bg='white')
  18. cv.pack()
  19.  
  20. steps = ["N","E","S","W"]
  21.    
  22. r = 10
  23. grid = dict([((x,y), 0) for x in range(r,ww-r) for y in range(r,hh-r)])
  24. xy__ = x,y = ww/2, hh/2
  25. xy = [(x,y)]
  26. total = 1
  27. grid[x,y] = total
  28. path = [steps[:]]
  29.  
  30. while 1:
  31.     x,y = xy[-1]
  32.     ttt = []
  33.     vvv = ((x,y-r),(x+r,y),(x,y+r),(x-r,y))
  34.     for x2,y2 in vvv:
  35.         if (x2,y2) in grid:
  36.             step = steps[vvv.index((x2,y2))]
  37.             try:
  38.                 if not grid[x2,y2] and (step in path[-1]):
  39.                     ttt += [(x2, y2, step)]
  40.             except:
  41.                 0
  42.     if ttt:
  43.         x2,y2,ttt = rc(ttt)
  44.         total = total+1
  45.         grid[x2,y2] = total
  46.         path[-1].remove(ttt)
  47.         xy.append((x2,y2))
  48.         path.append((steps[:]))
  49.     else:
  50.         path.pop()
  51.         xy.pop()
  52.    
  53.     if step:
  54.         try:
  55.             cv.create_line(xy[-2:],fill='red')
  56.         except:
  57.             break
  58.         cv.update()
  59.  
  60. xy = [xy__]
  61. Lx, Ly = max(grid.keys())
  62. L = (Lx/5)*(Ly/5)
  63.  
  64. paths = []
  65. block = {}
  66. LXY = 20
  67. while 1:
  68.     p = []
  69.     xy2 = [xy[-1]]
  70.     for i in range(LXY+1):
  71.         x,y = xy2[-1]
  72.         ttt = []
  73.         vvv = [(x,y-r),(x+r,y),(x,y+r),(x-r,y)]
  74.         for x2,y2 in vvv:
  75.             if (x2,y2) in grid:
  76.                 if (x2,y2) not in xy+xy2:
  77.                     ttt += [(grid[x2,y2],x2,y2)]
  78.                     grid[x2,y2] = 0
  79.         if ttt:
  80.             zzz,x2,y2 = min(ttt)
  81.                    
  82.             xy2 += [(x2,y2)]
  83.             grid[x2,y2] += len(xy)
  84.            
  85.             cv.delete('all')
  86.             cv.create_line(xy+xy2,fill='blue')
  87.             cv.update()
  88.         else:
  89.             xy2 = []
  90.             break
  91.    
  92.     if xy2:
  93.         xy.extend(xy2[:-1])
  94.     else:
  95.         t = LXY
  96.         try:
  97.             block[x,y] += t*2
  98.             block = block[-2000:]
  99.         except:
  100.             block[x,y] = t
  101.         t = block[x,y]
  102.            
  103.         if xy[:-t*2]:
  104.             xy = xy[:-t*2]
  105.         else:
  106.             xy = [xy[0]]
  107.    
  108.     # print len(xy)
  109.  
Add Comment
Please, Sign In to add comment