diff --git a/src/main/java/cat/hack3/codingtests/marsrover/MarsMap.java b/src/main/java/cat/hack3/codingtests/marsrover/MarsMap.java index 617ac5b..3650405 100644 --- a/src/main/java/cat/hack3/codingtests/marsrover/MarsMap.java +++ b/src/main/java/cat/hack3/codingtests/marsrover/MarsMap.java @@ -65,11 +65,16 @@ public class MarsMap { setNewLongitude(newLongitude); } - private void setNewLongitude(int newLongitude) { - currentPosition = currentPosition.ofUpdatedLongitude(newLongitude); + private void decrementLongitude() { + int longitude = currentPosition.getLongitude(); + int newLongitude = longitude - 1 < 1 + ? width + : --longitude; + setNewLongitude(newLongitude); } - private void decrementLongitude() { + private void setNewLongitude(int newLongitude) { + currentPosition = currentPosition.ofUpdatedLongitude(newLongitude); } public Coordinates getCurrentPosition() { diff --git a/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverTest.java b/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverTest.java index 66fcaee..3edd633 100644 --- a/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverTest.java +++ b/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverTest.java @@ -95,6 +95,17 @@ public class MarsRoverTest { assertEquals(rover.getCurrentDirection(), EAST); } + @Test + public void loopTheWorldInLongitudeMovingBackwards() { + Coordinates originalCoordinates = rover.getCurrentCoordinates(); + + rover.turnLeft(); + repeatAction(10, i -> rover.moveBackwards()); + + assertEquals(rover.getCurrentCoordinates(), originalCoordinates); + assertEquals(rover.getCurrentDirection(), EAST); + } + private void repeatAction(int times, IntConsumer actionToRepeat) { IntStream.rangeClosed(1, times) .forEach(actionToRepeat);