class Robot(): """Simulate a robot moving on a 5x5 grid""" _position = None def place(self, coords): """Place robot at the specified position on the table""" try: self._position = Position(coords) except InvalidPosition: pass @has_position def move(self): """Move the robot one unit in the direction it is currently facing""" c = self._position.coords() try: f = c['f'] if f == 'NORTH': c['y'] += 1 elif f == 'EAST': c['x'] += 1 elif f == 'SOUTH': c['y'] -= 1 elif f == 'WEST': c['x'] -= 1 self._position = Position(c) except InvalidPosition: pass @has_position def left(self): """Rotate the robot 90 degrees to the left""" c = self._position.coords() try: f = c['f'] c['f'] = COMPASS[f]['left'] self._position = Position(c) except InvalidPosition: pass @has_position def right(self): """Rotate the robot 90 degrees to the right""" c = self._position.coords() try: f = c['f'] c['f'] = COMPASS[f]['right'] self._position = Position(c) except InvalidPosition: pass @has_position def report(self): """Print the current position of the robot""" print(self._position)
def right(self): """Rotate the robot 90 degrees to the right""" c = self._position.coords() try: f = c['f'] c['f'] = COMPASS[f]['right'] self._position = Position(c) except InvalidPosition: pass
def move(self): """Move the robot one unit in the direction it is currently facing""" c = self._position.coords() try: f = c['f'] if f == 'NORTH': c['y'] += 1 elif f == 'EAST': c['x'] += 1 elif f == 'SOUTH': c['y'] -= 1 elif f == 'WEST': c['x'] -= 1 self._position = Position(c) except InvalidPosition: pass
def right(self): """Rotate the robot 90 degrees to the right""" if not self._position: return (x, y, f) = self._position.tuple() if f == 'NORTH': new = Position(x, y, 'EAST') elif f == 'EAST': new = Position(x, y, 'SOUTH') elif f == 'SOUTH': new = Position(x, y, 'WEST') elif f == 'WEST': new = Position(x, y, 'NORTH') else: raise InvalidPosition self._position = new
def move(self): """Move the robot one unit in the direction it is currently facing""" if not self._position: print('ERROR') return (x, y, f) = self._position.tuple() try: if f == 'NORTH': new = Position(x, y + 1, f) elif f == 'EAST': new = Position(x + 1, y, f) elif f == 'SOUTH': new = Position(x, y - 1, f) elif f == 'WEST': new = Position(x - 1, y, f) self._position = new except InvalidPosition: pass
def place(self, coords): """Place robot at the specified position on the table""" try: self._position = Position(coords) except InvalidPosition: pass
class Robot(): _position = None def place(self, args): """Place robot at the specified position on the table""" try: self._position = Position(**args) except InvalidPosition: pass def move(self): """Move the robot one unit in the direction it is currently facing""" if not self._position: print('ERROR') return (x, y, f) = self._position.tuple() try: if f == 'NORTH': new = Position(x, y + 1, f) elif f == 'EAST': new = Position(x + 1, y, f) elif f == 'SOUTH': new = Position(x, y - 1, f) elif f == 'WEST': new = Position(x - 1, y, f) self._position = new except InvalidPosition: pass def left(self): """Rotate the robot 90 degrees to the left""" if not self._position: return (x, y, f) = self._position.tuple() if f == 'NORTH': new = Position(x, y, 'WEST') elif f == 'WEST': new = Position(x, y, 'SOUTH') elif f == 'SOUTH': new = Position(x, y, 'EAST') elif f == 'EAST': new = Position(x, y, 'NORTH') else: raise InvalidPosition self._position = new def right(self): """Rotate the robot 90 degrees to the right""" if not self._position: return (x, y, f) = self._position.tuple() if f == 'NORTH': new = Position(x, y, 'EAST') elif f == 'EAST': new = Position(x, y, 'SOUTH') elif f == 'SOUTH': new = Position(x, y, 'WEST') elif f == 'WEST': new = Position(x, y, 'NORTH') else: raise InvalidPosition self._position = new def report(self): """Print the current position of the robot""" if not self._position: return print(self._position)
def test_invalid_position(x, y, facing): with pytest.raises(InvalidCoordinates): Position(x, y, facing)
def test_valid_position(x, y, facing): position = Position(x, y, facing) assert position.x == x assert position.y == y assert position.facing == facing