diff --git a/rover/rover.py b/rover/rover.py index 0f2e4bd..d7bf29d 100644 --- a/rover/rover.py +++ b/rover/rover.py @@ -15,6 +15,9 @@ class Direction(enum.Enum): WEST = "W" +DIRECTIONS = [Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST] + + class Rover(BaseModel): pos: Vector = Vector(x=0, y=0) planet_size: Vector = Vector(x=100, y=100) @@ -43,3 +46,13 @@ class Rover(BaseModel): def backward(self): self._translate(-1) + + def _turn(self, value): + index: int = DIRECTIONS.index(self.dir) + self.dir = DIRECTIONS[(index + value) % len(DIRECTIONS)] + + def turn_left(self): + self._turn(-1) + + def turn_right(self): + self._turn(1) diff --git a/rover/test_rover.py b/rover/test_rover.py index ec82639..49d605d 100644 --- a/rover/test_rover.py +++ b/rover/test_rover.py @@ -156,3 +156,15 @@ def test_rover_can_go_backward_wrapping_over_east_west(): ) rov.backward() assert rov.pos == Vector(x=0, y=0) + + +def test_rover_can_turn_left(): + rov = Rover(dir=Direction.NORTH) + rov.turn_left() + assert rov.dir == Direction.WEST + + +def test_rover_can_turn_right(): + rov = Rover(dir=Direction.NORTH) + rov.turn_right() + assert rov.dir == Direction.EAST