rover: wrap when going negative

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

View file

@ -29,3 +29,8 @@ class Rover(BaseModel):
self.pos.x += 1
elif self.dir == Direction.WEST:
self.pos.x -= 1
if self.pos.x < 0:
self.pos.x += self.planet_size.x
if self.pos.y < 0:
self.pos.y += self.planet_size.y

View file

@ -60,3 +60,19 @@ def test_rover_can_go_forward_going_west():
)
rov.forward()
assert rov.pos == Vector(x=0, y=0)
def test_rover_can_wrap_under_north_south():
rov = Rover(
pos=Vector(x=0, y=0), planet_size=Vector(x=10, y=10), dir=Direction.SOUTH
)
rov.forward()
assert rov.pos == Vector(x=0, y=9)
def test_rover_can_wrap_under_east_west():
rov = Rover(
pos=Vector(x=0, y=0), planet_size=Vector(x=10, y=10), dir=Direction.WEST
)
rov.forward()
assert rov.pos == Vector(x=9, y=0)