-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobotSimulator.py
More file actions
63 lines (59 loc) · 1.44 KB
/
Copy pathrobotSimulator.py
File metadata and controls
63 lines (59 loc) · 1.44 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
class Robot :
""" Direction is use by his index in list : """
direction = ["north", "east", "south", "west"]
""" Getter and Setter for X, Y and direction(index)"""
def __init__(self, x = 0, y = 0, direction = "north"):
self._x = x
self._y = y
self._direction = direction
def get_x(self):
return self._x
def get_y(self):
return self._y
def get_direction(self):
return self.direction[self._direction]
def set_x(self, x):
self._x = x
def set_y(self, y):
self._y = y
def set_direction(self, current):
if self.direction.count(current) == 0:
raise ValueError("Incorrect direction")
self._direction = self.direction.index(current)
""" Functions of simulation """
def turn_right(self):
if self._direction == 3:
self._direction = 0
else:
self._direction += 1
def turn_left(self):
if self._direction == 0:
self._direction = 3
else:
self._direction -= 1
def advance(self):
if self._direction == 0:
self._y += 1
if self._direction == 2:
self._y -= 1
if self._direction == 1:
self._x += 1
if self._direction == 3:
self._x -= 1
""" Main simulation """
def robotSimulator(self, string):
for i in string:
if i == 'R':
self.turn_right()
if i == 'L':
self.turn_left()
if i == 'A':
self.advance()
return [self._x, self._y]
""" Tests zone """
robot = Robot()
robot.set_x(7)
robot.set_y(3)
robot.set_direction("north")
print (robot.robotSimulator("RAALAL"))
print(str(robot.get_direction()))