From af803b642a5d111b8b8fde086694481a1c193308 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 15 Nov 2019 21:20:42 +0100 Subject: [PATCH] rover: add backward method --- rover/rover.py | 16 ++++++++---- rover/test_rover.py | 64 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/rover/rover.py b/rover/rover.py index c02091d..0f2e4bd 100644 --- a/rover/rover.py +++ b/rover/rover.py @@ -20,15 +20,15 @@ class Rover(BaseModel): planet_size: Vector = Vector(x=100, y=100) dir: Direction = Direction.NORTH - def forward(self): + def _translate(self, value): if self.dir == Direction.NORTH: - self.pos.y += 1 + self.pos.y += value elif self.dir == Direction.SOUTH: - self.pos.y -= 1 + self.pos.y -= value elif self.dir == Direction.EAST: - self.pos.x += 1 + self.pos.x += value elif self.dir == Direction.WEST: - self.pos.x -= 1 + self.pos.x -= value if self.pos.x < 0: self.pos.x += self.planet_size.x @@ -37,3 +37,9 @@ class Rover(BaseModel): self.pos.x %= self.planet_size.x self.pos.y %= self.planet_size.y + + def forward(self): + self._translate(1) + + def backward(self): + self._translate(-1) diff --git a/rover/test_rover.py b/rover/test_rover.py index 29fc698..ec82639 100644 --- a/rover/test_rover.py +++ b/rover/test_rover.py @@ -92,3 +92,67 @@ def test_rover_can_wrap_over_east_west(): ) rov.forward() assert rov.pos == Vector(x=0, y=0) + + +def test_rover_can_go_backward_north(): + rov = Rover( + pos=Vector(x=0, y=1), planet_size=Vector(x=10, y=10), dir=Direction.NORTH + ) + rov.backward() + assert rov.pos == Vector(x=0, y=0) + + +def test_rover_can_go_backward_south(): + rov = Rover( + pos=Vector(x=0, y=0), planet_size=Vector(x=10, y=10), dir=Direction.SOUTH + ) + rov.backward() + assert rov.pos == Vector(x=0, y=1) + + +def test_rover_can_go_backward_west(): + rov = Rover( + pos=Vector(x=0, y=0), planet_size=Vector(x=10, y=10), dir=Direction.WEST + ) + rov.backward() + assert rov.pos == Vector(x=1, y=0) + + +def test_rover_can_go_backward_east(): + rov = Rover( + pos=Vector(x=1, y=0), planet_size=Vector(x=10, y=10), dir=Direction.EAST + ) + rov.backward() + assert rov.pos == Vector(x=0, y=0) + + +def test_rover_can_go_backward_wrapping_under_north_south(): + rov = Rover( + pos=Vector(x=0, y=0), planet_size=Vector(x=10, y=10), dir=Direction.NORTH + ) + rov.backward() + assert rov.pos == Vector(x=0, y=9) + + +def test_rover_can_go_backward_wrapping_over_north_south(): + rov = Rover( + pos=Vector(x=0, y=9), planet_size=Vector(x=10, y=10), dir=Direction.SOUTH + ) + rov.backward() + assert rov.pos == Vector(x=0, y=0) + + +def test_rover_can_go_backward_wrapping_under_east_west(): + rov = Rover( + pos=Vector(x=0, y=0), planet_size=Vector(x=10, y=10), dir=Direction.EAST + ) + rov.backward() + assert rov.pos == Vector(x=9, y=0) + + +def test_rover_can_go_backward_wrapping_over_east_west(): + rov = Rover( + pos=Vector(x=9, y=0), planet_size=Vector(x=10, y=10), dir=Direction.WEST + ) + rov.backward() + assert rov.pos == Vector(x=0, y=0)