rover.py 2.05 KB
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]