Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_avoidance_walk_5.py
- import random
- from Tkinter import *
- from math import *
- ri = random.randint
- rs = random.shuffle
- rc = random.choice
- ww = 600
- hh = 600
- root = Tk()
- root.title("Tk_avoidance_walk")
- root.geometry("%dx%d+-6+-2"%(ww,hh))
- cv = Canvas(width=ww, height=hh, bg='white')
- cv.pack()
- steps = ["N","E","S","W"]
- r = 10
- grid = dict([((x,y), 0) for x in range(r,ww-r) for y in range(r,hh-r)])
- xy__ = x,y = ww/2, hh/2
- xy = [(x,y)]
- total = 1
- grid[x,y] = total
- path = [steps[:]]
- while 1:
- x,y = xy[-1]
- ttt = []
- vvv = ((x,y-r),(x+r,y),(x,y+r),(x-r,y))
- for x2,y2 in vvv:
- if (x2,y2) in grid:
- step = steps[vvv.index((x2,y2))]
- try:
- if not grid[x2,y2] and (step in path[-1]):
- ttt += [(x2, y2, step)]
- except:
- 0
- if ttt:
- x2,y2,ttt = rc(ttt)
- total = total+1
- grid[x2,y2] = total
- path[-1].remove(ttt)
- xy.append((x2,y2))
- path.append((steps[:]))
- else:
- path.pop()
- xy.pop()
- if step:
- try:
- cv.create_line(xy[-2:],fill='red')
- except:
- break
- cv.update()
- xy = [xy__]
- Lx, Ly = max(grid.keys())
- L = (Lx/5)*(Ly/5)
- paths = []
- block = {}
- LXY = 20
- while 1:
- p = []
- xy2 = [xy[-1]]
- for i in range(LXY+1):
- x,y = xy2[-1]
- ttt = []
- vvv = [(x,y-r),(x+r,y),(x,y+r),(x-r,y)]
- for x2,y2 in vvv:
- if (x2,y2) in grid:
- if (x2,y2) not in xy+xy2:
- ttt += [(grid[x2,y2],x2,y2)]
- grid[x2,y2] = 0
- if ttt:
- zzz,x2,y2 = min(ttt)
- xy2 += [(x2,y2)]
- grid[x2,y2] += len(xy)
- cv.delete('all')
- cv.create_line(xy+xy2,fill='blue')
- cv.update()
- else:
- xy2 = []
- break
- if xy2:
- xy.extend(xy2[:-1])
- else:
- t = LXY
- try:
- block[x,y] += t*2
- block = block[-2000:]
- except:
- block[x,y] = t
- t = block[x,y]
- if xy[:-t*2]:
- xy = xy[:-t*2]
- else:
- xy = [xy[0]]
- # print len(xy)
Add Comment
Please, Sign In to add comment