From 24b3f63d3c80da224df8e1e637b8b3d36aa9c593 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 15 Nov 2019 21:12:51 +0100 Subject: [PATCH] rover: wrap when going negative --- rover/rover.py | 5 +++++ rover/test_rover.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/rover/rover.py b/rover/rover.py index 2190364..6fc531b 100644 --- a/rover/rover.py +++ b/rover/rover.py @@ -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 diff --git a/rover/test_rover.py b/rover/test_rover.py index f5a00ca..8c6e0a1 100644 --- a/rover/test_rover.py +++ b/rover/test_rover.py @@ -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)