Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_avoidance_walk_4.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"]
- tries = 0
- def direction():
- if not tries:
- t = steps[:]
- rs(t)
- return [t]
- return [rc([steps[:], steps[::-1]])]
- r = 10
- grid = dict([((x,y), 0) for x in range(r,ww-r) for y in range(r,hh-r)])
- x,y = ww/2, hh/2
- xy = [(x,y)]
- zz = []
- most = 0
- while 1:
- while 1:
- try:
- x,y = xy[-1]
- step = zz[-1].pop()
- break
- except:
- try:
- xy.pop()
- zz.pop()
- except:
- xy = [(x,y)]
- zz += direction()
- if step == "E":
- x += r
- elif step == "W":
- x -= r
- elif step == "N":
- y += r
- else:
- y -= r
- try:
- grid[x,y]
- if (x,y) not in xy:
- zz += direction()
- xy += [(x,y)]
- if len(xy) > most:
- most = len(xy)
- print most
- tries = max(0,tries-3)
- cv.delete('all')
- cv.create_line(xy,fill='blue')
- cv.update()
- else:
- tries += 1
- if tries > 12:
- xy = xy[:-50]
- zz = zz[:-50]
- except:
- 0
Add Comment
Please, Sign In to add comment