-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot_simulator.py
More file actions
58 lines (48 loc) · 1.01 KB
/
robot_simulator.py
File metadata and controls
58 lines (48 loc) · 1.01 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
# Globals for the bearings
# Change the values as you see fit
EAST = 0
NORTH = 1
WEST = 2
SOUTH = 3
class Robot(object):
def __init__(self, bearing=NORTH, x=0, y=0):
self.x=x
self.y=y
self.coordinates=(self.x,self.y)
self.bearing=bearing
def turn_right(self):
if self.bearing==0:
self.bearing=3
elif self.bearing==1:
self.bearing=0
elif self.bearing==2:
self.bearing=1
elif self.bearing==3:
self.bearing=2
def turn_left(self):
if self.bearing==0:
self.bearing=1
elif self.bearing==1:
self.bearing=2
elif self.bearing==2:
self.bearing=3
elif self.bearing==3:
self.bearing=0
def advance(self):
if self.bearing==0:
self.x+=1
elif self.bearing==1:
self.y+=1
elif self.bearing==2:
self.x-=1
elif self.bearing==3:
self.y-=1
self.coordinates=(self.x,self.y)
def simulate(self,command):
for i in range(len(command)):
if command[i]=='A':
self.advance()
elif command[i]=='L':
self.turn_left()
elif command[i]=='R':
self.turn_right()