-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraffic.py
More file actions
73 lines (63 loc) · 1.83 KB
/
Copy pathtraffic.py
File metadata and controls
73 lines (63 loc) · 1.83 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sys, time
class TrafficLight:
def __init__(self, color):
self.color = color
def getColor(self):
return self.color
def change1(self):
if self.color =='G':
self.color = 'Y'
def change2(self):
if self.color == 'Y':
self.color = 'R'
elif self.color == 'R':
self.color = 'G'
class Timer:
event1_callback = []
event2_callback = []
def __init__(self):
self.t = 0
def add(self):
self.t += 1
tt = self.t % 8
if tt == 5:
self.event1()
elif tt == 0:
self.event2()
def event1(self):
for e in self.event1_callback:
e()
def event2(self):
for e in self.event2_callback:
e()
def subscribe1(self, observer):
self.event1_callback.append(observer)
def subscribe2(self, observer):
self.event2_callback.append(observer)
def draw(l1,l2,l3,l4,t):
print ' | | %d'%t
print ' | %c |'%l1
print '-------------------- ---------------------'
print ' %c %c'%(l2,l3)
print '-------------------- ---------------------'
print ' | %c |'%(l4)
print ' | |'
timer = Timer()
l1 = TrafficLight('R')
l2 = TrafficLight('G')
l3 = TrafficLight('G')
l4 = TrafficLight('R')
timer.subscribe1(l1.change1)
timer.subscribe1(l2.change1)
timer.subscribe1(l3.change1)
timer.subscribe1(l4.change1)
timer.subscribe2(l1.change2)
timer.subscribe2(l2.change2)
timer.subscribe2(l3.change2)
timer.subscribe2(l4.change2)
for t in xrange(1,100):
draw(l1.getColor(),l2.getColor(),l3.getColor(),l4.getColor(),t)
time.sleep(1)
timer.add()
sys.stderr.write("\x1b[2J\x1b[H")
sys.stdout.flush()