rover: add turn methods to Rover class

This commit is contained in:
Bruno BELANYI 2019-11-15 21:29:37 +01:00
parent af803b642a
commit 4a79fd4c7f
2 changed files with 25 additions and 0 deletions

View file

@ -15,6 +15,9 @@ class Direction(enum.Enum):
WEST = "W" WEST = "W"
DIRECTIONS = [Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST]
class Rover(BaseModel): class Rover(BaseModel):
pos: Vector = Vector(x=0, y=0) pos: Vector = Vector(x=0, y=0)
planet_size: Vector = Vector(x=100, y=100) planet_size: Vector = Vector(x=100, y=100)
@ -43,3 +46,13 @@ class Rover(BaseModel):
def backward(self): def backward(self):
self._translate(-1) 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)

View file

@ -156,3 +156,15 @@ def test_rover_can_go_backward_wrapping_over_east_west():
) )
rov.backward() rov.backward()
assert rov.pos == Vector(x=0, y=0) 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