-
Notifications
You must be signed in to change notification settings - Fork 0
/
BotTrust.py
67 lines (52 loc) · 1.58 KB
/
BotTrust.py
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
from collections import deque
class Robot:
directions = deque ([])
location = 1
destination = 1
name = ""
def action(self):
if self.canPushButton():
self.directions.popleft()
self.findNextDestination()
else:
self.move()
def canPushButton(self):
if self.directions and self.directions[0][0]==self.name and self.location == self.destination:
return True
else:
return False
def findNextDestination(self):
for co in self.directions:
if co[0] == self.name:
self.destination = co[1]
return
def move(self):
if self.location<self.destination:
self.location += 1
elif self.location>self.destination:
self.location -= 1
class World:
time = 0
robots = []
directions= deque ([["o",2],["b",1],["b",2],["o",4]])
# directions= deque ([["o",5],["o",8],["b",100]])
# directions= deque ([["b",2],["b",1]])
def __init__(self):
names =[]
for dir in self.directions:
names.append(dir[0])
names = set(names)
for name in names:
robot = Robot()
robot.name = name
robot.directions = self.directions
robot.findNextDestination()
self.robots.append(robot)
def run(self):
while self.directions:
self.time+=1
for r in self.robots:
r.action()
w = World()
w.run()
print 'Time:',w.time