My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from Tkinter import *

rs = raw_input("Enter rule set (e.g.: L/R) => ")
str0 = rs.split('/')[0]
str1 = rs.split('/')[1]
i0, i1 = -1, -1
w,h = 500, 500
lx,ly = w/2, h/2
dirx,diry = 0,-1

# defining absolute directions
vec = {
(0,1,'L'):(1,0),
(1,0,'L'):(0,-1),
(0,-1,'L'):(-1,0),
(-1,0,'L'):(0,1),
(0,1,'R'):(-1,0),
(1,0,'R'):(0,1),
(0,-1,'R'):(1,0),
(-1,0,'R'):(0,-1)
}
mod = 2
grid = dict([((x,y),0) for y in range(0,h,mod) for x in range(0,w,mod)])

# Initialize Tk window
root = Tk()
ant = Canvas(root,width=w, height=h, bg='white')
ant.pack(fill=BOTH)

while 1:
if lx < w and ly < h and lx > 0 and ly > 0:
if grid[(lx,ly)] == 0:
i0 = (i0+1)%len(str0)
rdir = str0[i0]
elif grid[(lx,ly)] == 1:
i1 = (i1+1)%len(str1)
rdir = str1[i1]
dirx, diry = vec[(dirx,diry,rdir)]
grid[(lx,ly)] = grid[(lx,ly)]^1
col = "white" if grid[(lx,ly)] == 0 else "darkorange"
ant.delete("current")
ant.create_rectangle(lx, ly, lx+mod-1, ly+mod-1, fill="black",outline="black",tags="current")
ant.update()
ant.delete((lx,ly))
ant.create_rectangle(lx, ly, lx+mod-1, ly+mod-1, fill=col,outline=col,tags=(lx,ly))
lx,ly = lx+dirx*mod, ly+diry*mod
Show details Hide details

Change log

r30 by vasiliauskas.agnius on Jun 21, 2009   Diff
langton ant
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 1296 bytes, 46 lines
Hosted by Google Code