rover: use direction when going forward

This commit is contained in:
Bruno BELANYI 2019-11-15 21:12:07 +01:00
parent add6940dce
commit d9bf239c39
2 changed files with 51 additions and 0 deletions

View file

@ -19,3 +19,13 @@ 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)
dir: Direction = Direction.NORTH dir: Direction = Direction.NORTH
def forward(self):
if self.dir == Direction.NORTH:
self.pos.y += 1
elif self.dir == Direction.SOUTH:
self.pos.y -= 1
elif self.dir == Direction.EAST:
self.pos.x += 1
elif self.dir == Direction.WEST:
self.pos.x -= 1

View file

@ -19,3 +19,44 @@ def test_rover_has_direction():
def test_rover_default_direction_is_north(): def test_rover_default_direction_is_north():
rov = Rover() rov = Rover()
assert rov.dir == Direction.NORTH assert rov.dir == Direction.NORTH
def test_rover_can_go_forward_once_going_north():
rov = Rover(
pos=Vector(x=0, y=0), planet_size=Vector(x=10, y=10), dir=Direction.NORTH
)
rov.forward()
assert rov.pos == Vector(x=0, y=1)
def test_rover_can_go_forward_twice_going_north():
rov = Rover(
pos=Vector(x=0, y=0), planet_size=Vector(x=10, y=10), dir=Direction.NORTH
)
rov.forward()
rov.forward()
assert rov.pos == Vector(x=0, y=2)
def test_rover_can_go_forward_going_east():
rov = Rover(
pos=Vector(x=0, y=0), planet_size=Vector(x=10, y=10), dir=Direction.EAST
)
rov.forward()
assert rov.pos == Vector(x=1, y=0)
def test_rover_can_go_forward_going_south():
rov = Rover(
pos=Vector(x=0, y=1), planet_size=Vector(x=10, y=10), dir=Direction.SOUTH
)
rov.forward()
assert rov.pos == Vector(x=0, y=0)
def test_rover_can_go_forward_going_west():
rov = Rover(
pos=Vector(x=1, y=0), planet_size=Vector(x=10, y=10), dir=Direction.WEST
)
rov.forward()
assert rov.pos == Vector(x=0, y=0)