diff --git a/mars-rover/src/rover.py b/mars-rover/src/rover.py index e4b60a2..dfb3c31 100644 --- a/mars-rover/src/rover.py +++ b/mars-rover/src/rover.py @@ -20,12 +20,12 @@ def moveBy(self, commandKey): command(self, delta) def move_forward(self, delta): - self.x += delta[0] - self.y += delta[1] + self.x = self.planet.wrap_x(self.x + delta[0]) + self.y = self.planet.wrap_y(self.y + delta[1]) def move_backward(self, delta): - self.x -= delta[0] - self.y -= delta[1] + self.x = self.planet.wrap_x(self.x - delta[0]) + self.y = self.planet.wrap_y(self.y - delta[1]) def move_right(self, delta): self.orientation = delta[3] diff --git a/mars-rover/test/test_rover.py b/mars-rover/test/test_rover.py index 9740174..9a46519 100644 --- a/mars-rover/test/test_rover.py +++ b/mars-rover/test/test_rover.py @@ -130,3 +130,49 @@ def test_simple_path(self): assert_equal('E', r.orientation) assert_equal(12, r.x) assert_equal(23, r.y) + +class TestWrapping: + + def setup(self): + global wp + wp = planet.Planet(3, 3) + + def test_wrap_east_forward(self): + r = wp.createRover(2, 0, 'E') + r.move(list("f")) + assert_equal(0, r.x) + + def test_wrap_west_forward(self): + r = wp.createRover(0, 0, 'W') + r.move(list("f")) + assert_equal(2, r.x) + + def test_wrap_north_forward(self): + r = wp.createRover(0, 2, 'N') + r.move(list("f")) + assert_equal(0, r.y) + + def test_wrap_south_forward(self): + r = wp.createRover(0, 0, 'S') + r.move(list("f")) + assert_equal(2, r.y) + + def test_wrap_east_backward(self): + r = wp.createRover(0, 0, 'E') + r.move(list('b')) + assert_equal(2, r.x) + + def test_wrap_west_backward(self): + r = wp.createRover(2, 0, 'W') + r.move(list('b')) + assert_equal(0, r.x) + + def test_wrap_north_backward(self): + r = wp.createRover(0, 0, 'N') + r.move(list('b')) + assert_equal(2, r.y) + + def test_wrap_south_backward(self): + r = wp.createRover(0, 2, 'S') + r.move(list('b')) + assert_equal(0, r.y)