loop the world longitudinally succesful
This commit is contained in:
parent
be56b4478a
commit
20b785ef29
|
@ -12,6 +12,10 @@ public class Coordinates {
|
||||||
return new Coordinates(newLatitude, longitude);
|
return new Coordinates(newLatitude, longitude);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Coordinates ofUpdatedLongitude(int newLongitude) {
|
||||||
|
return new Coordinates(latitude, newLongitude);
|
||||||
|
}
|
||||||
|
|
||||||
private Coordinates(int latitude, int longitude) {
|
private Coordinates(int latitude, int longitude) {
|
||||||
this.latitude = latitude;
|
this.latitude = latitude;
|
||||||
this.longitude = longitude;
|
this.longitude = longitude;
|
||||||
|
|
|
@ -28,8 +28,8 @@ public class MarsMap {
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case NORTH -> decrementLatitude();
|
case NORTH -> decrementLatitude();
|
||||||
case SOUTH -> incrementLatitude();
|
case SOUTH -> incrementLatitude();
|
||||||
case WEST -> incrementLongitude();
|
case WEST -> decrementLongitude();
|
||||||
case EAST -> decrementLongitude();
|
case EAST -> incrementLongitude();
|
||||||
}
|
}
|
||||||
logger.info(String.format("Updated coordinates towards %s, now is %d-%d",
|
logger.info(String.format("Updated coordinates towards %s, now is %d-%d",
|
||||||
direction, currentPosition.getLatitude(), currentPosition.getLongitude()));
|
direction, currentPosition.getLatitude(), currentPosition.getLongitude()));
|
||||||
|
@ -58,7 +58,15 @@ public class MarsMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void incrementLongitude() {
|
private void incrementLongitude() {
|
||||||
|
int longitude = currentPosition.getLongitude();
|
||||||
|
int newLongitude = longitude + 1 > width
|
||||||
|
? 1
|
||||||
|
: ++longitude;
|
||||||
|
setNewLongitude(newLongitude);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setNewLongitude(int newLongitude) {
|
||||||
|
currentPosition = currentPosition.ofUpdatedLongitude(newLongitude);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void decrementLongitude() {
|
private void decrementLongitude() {
|
||||||
|
|
|
@ -34,17 +34,6 @@ public class MarsRoverTest {
|
||||||
assertEquals(rover.getCurrentDirection(), originalDirection);
|
assertEquals(rover.getCurrentDirection(), originalDirection);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void loopTheWorldInLatitudeMovingForward() {
|
|
||||||
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
|
||||||
Direction originalDirection = rover.getCurrentDirection();
|
|
||||||
|
|
||||||
repeatAction(10, i -> rover.moveForward());
|
|
||||||
|
|
||||||
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
|
|
||||||
assertEquals(rover.getCurrentDirection(), originalDirection);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void stepBackwards() {
|
public void stepBackwards() {
|
||||||
Direction originalDirection = rover.getCurrentDirection();
|
Direction originalDirection = rover.getCurrentDirection();
|
||||||
|
@ -55,17 +44,6 @@ public class MarsRoverTest {
|
||||||
assertEquals(rover.getCurrentDirection(), originalDirection);
|
assertEquals(rover.getCurrentDirection(), originalDirection);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void loopTheWorldInLatitudeMovingBackwards() {
|
|
||||||
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
|
||||||
Direction originalDirection = rover.getCurrentDirection();
|
|
||||||
|
|
||||||
repeatAction(10, i -> rover.moveBackwards());
|
|
||||||
|
|
||||||
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
|
|
||||||
assertEquals(rover.getCurrentDirection(), originalDirection);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void turnLef() {
|
public void turnLef() {
|
||||||
assertEquals(rover.turnLeft(), EAST);
|
assertEquals(rover.turnLeft(), EAST);
|
||||||
|
@ -84,6 +62,39 @@ public class MarsRoverTest {
|
||||||
assertEquals(rover.turnRight(), WEST);
|
assertEquals(rover.turnRight(), WEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void loopTheWorldInLatitudeMovingForward() {
|
||||||
|
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
||||||
|
Direction originalDirection = rover.getCurrentDirection();
|
||||||
|
|
||||||
|
repeatAction(10, i -> rover.moveForward());
|
||||||
|
|
||||||
|
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
|
||||||
|
assertEquals(rover.getCurrentDirection(), originalDirection);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void loopTheWorldInLatitudeMovingBackwards() {
|
||||||
|
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
||||||
|
Direction originalDirection = rover.getCurrentDirection();
|
||||||
|
|
||||||
|
repeatAction(10, i -> rover.moveBackwards());
|
||||||
|
|
||||||
|
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
|
||||||
|
assertEquals(rover.getCurrentDirection(), originalDirection);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void loopTheWorldInLongitudeMovingForward() {
|
||||||
|
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
||||||
|
|
||||||
|
rover.turnLeft();
|
||||||
|
repeatAction(10, i -> rover.moveForward());
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
Loading…
Reference in New Issue