1
0
Fork 0

also in backwards

This commit is contained in:
Xavier Fontanet 2024-06-20 23:06:19 +02:00
parent 20b785ef29
commit ac72870894
2 changed files with 19 additions and 3 deletions

View File

@ -65,11 +65,16 @@ public class MarsMap {
setNewLongitude(newLongitude); setNewLongitude(newLongitude);
} }
private void setNewLongitude(int newLongitude) { private void decrementLongitude() {
currentPosition = currentPosition.ofUpdatedLongitude(newLongitude); 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() { public Coordinates getCurrentPosition() {

View File

@ -95,6 +95,17 @@ public class MarsRoverTest {
assertEquals(rover.getCurrentDirection(), EAST); 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) { private void repeatAction(int times, IntConsumer actionToRepeat) {
IntStream.rangeClosed(1, times) IntStream.rangeClosed(1, times)
.forEach(actionToRepeat); .forEach(actionToRepeat);