| Changes to /trunk/Python/Langtons_ant.py |
r0 vs. r30
Edit
|
r30
|
| /trunk/Python/Langtons_ant.py | /trunk/Python/Langtons_ant.py r30 | ||
| 1 | from Tkinter import * | ||
|---|---|---|---|
| 2 | |||
| 3 | rs = raw_input("Enter rule set (e.g.: L/R) => ") | ||
| 4 | str0 = rs.split('/')[0] | ||
| 5 | str1 = rs.split('/')[1] | ||
| 6 | i0, i1 = -1, -1 | ||
| 7 | w,h = 500, 500 | ||
| 8 | lx,ly = w/2, h/2 | ||
| 9 | dirx,diry = 0,-1 | ||
| 10 | |||
| 11 | # defining absolute directions | ||
| 12 | vec = { | ||
| 13 | (0,1,'L'):(1,0), | ||
| 14 | (1,0,'L'):(0,-1), | ||
| 15 | (0,-1,'L'):(-1,0), | ||
| 16 | (-1,0,'L'):(0,1), | ||
| 17 | (0,1,'R'):(-1,0), | ||
| 18 | (1,0,'R'):(0,1), | ||
| 19 | (0,-1,'R'):(1,0), | ||
| 20 | (-1,0,'R'):(0,-1) | ||
| 21 | } | ||
| 22 | mod = 2 | ||
| 23 | grid = dict([((x,y),0) for y in range(0,h,mod) for x in range(0,w,mod)]) | ||
| 24 | |||
| 25 | # Initialize Tk window | ||
| 26 | root = Tk() | ||
| 27 | ant = Canvas(root,width=w, height=h, bg='white') | ||
| 28 | ant.pack(fill=BOTH) | ||
| 29 | |||
| 30 | while 1: | ||
| 31 | if lx < w and ly < h and lx > 0 and ly > 0: | ||
| 32 | if grid[(lx,ly)] == 0: | ||
| 33 | i0 = (i0+1)%len(str0) | ||
| 34 | rdir = str0[i0] | ||
| 35 | elif grid[(lx,ly)] == 1: | ||
| 36 | i1 = (i1+1)%len(str1) | ||
| 37 | rdir = str1[i1] | ||
| 38 | dirx, diry = vec[(dirx,diry,rdir)] | ||
| 39 | grid[(lx,ly)] = grid[(lx,ly)]^1 | ||
| 40 | col = "white" if grid[(lx,ly)] == 0 else "darkorange" | ||
| 41 | ant.delete("current") | ||
| 42 | ant.create_rectangle(lx, ly, lx+mod-1, ly+mod-1, fill="black",outline="black",tags="current") | ||
| 43 | ant.update() | ||
| 44 | ant.delete((lx,ly)) | ||
| 45 | ant.create_rectangle(lx, ly, lx+mod-1, ly+mod-1, fill=col,outline=col,tags=(lx,ly)) | ||
| 46 | lx,ly = lx+dirx*mod, ly+diry*mod | ||