rover.py
2.05 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
74
class OutOfBoundaryException(Exception):
"""
Raised when a Rover gets starting coordinates that are not on the planet
"""
class InvalidHeading(Exception):
"""
Raised when a heading that does not exist is provided
"""
class Rover(object):
"""
This Mars Rover should keep track of its current position, its heading, and
the size of the grid
"""
def __init__(self, start, route, boundaries):
self.boundaries = [[0, 0], [0, 0]]
self.coordinates = [0, 0]
self.heading = ''
self.headings = ['N', 'E', 'S', 'W']
self.set_boundaries(boundaries)
s = start.split()
self.set_coordinates(s)
self.set_heading(s[2])
print route
def set_boundaries(self, b):
"""Define the boundaries of the planet"""
b = b.split()
# providing a non-integer string will raise a value error, which should
# be fine for now
self.boundaries[1][0] = int(b[0])
self.boundaries[1][1] = int(b[1])
def set_coordinates(self, coords):
"""Set the current coordinates for the rover"""
# providing a non-integer string will raise a value error, which should
# be fine for now
x = int(coords[0])
y = int(coords[1])
# check if x and y can actually exist within the planetary boundaries
if x < self.min_x or x > self.max_x or y < self.min_y or y > self.max_y:
raise OutOfBoundaryException
self.coordinates = [x, y]
def set_heading(self, h):
"""Set the current heading of the rover"""
if h not in self.headings:
raise InvalidHeading('Heading "{}" is not allowed'.format(h))
self.heading = h
# some convenient properties to access the boundaries lists
@property
def min_x(self):
return self.boundaries[0][0]
@property
def min_y(self):
return self.boundaries[0][1]
@property
def max_x(self):
return self.boundaries[1][0]
@property
def max_y(self):
return self.boundaries[1][1]