1
0
Fork 0

first obstacle feature test OK

This commit is contained in:
Xavier Fontanet 2024-06-21 19:13:53 +02:00
parent 88e6db14c0
commit afe21ad223
2 changed files with 49 additions and 17 deletions

View File

@ -20,60 +20,83 @@ public class MarsMap {
private final int height; private final int height;
private final int width; private final int width;
private Coordinates currentPosition; private Coordinates currentPosition;
private final List<Coordinates> obstaclesLocalizations;
private final MapIncrementalPositionResolver positionResolver; private final MapIncrementalPositionResolver positionResolver;
@SafeVarargs
public MarsMap(int height, int width, Coordinates startingCoordinates, List<Coordinates>... obstaclesLocalizations) { public MarsMap(int height, int width, Coordinates startingCoordinates, List<Coordinates>... obstaclesLocalizations) {
this.height = height; this.height = height;
this.width = width; this.width = width;
currentPosition = startingCoordinates; currentPosition = startingCoordinates;
this.obstaclesLocalizations = obstaclesLocalizations.length > 0 ? obstaclesLocalizations[0] : List.of();
positionResolver = new MapIncrementalPositionResolver(FIRST_POSITION_IN_MAP, INCREMENT_UNIT); positionResolver = new MapIncrementalPositionResolver(FIRST_POSITION_IN_MAP, INCREMENT_UNIT);
} }
public Coordinates updatePositionTowards(Direction direction) { public Coordinates updatePositionTowards(Direction direction) {
switch (direction) { boolean updatedPosition = switch (direction) {
case NORTH -> decrementLatitude(); case NORTH -> decrementLatitude();
case SOUTH -> incrementLatitude(); case SOUTH -> incrementLatitude();
case WEST -> decrementLongitude(); case WEST -> decrementLongitude();
case EAST -> incrementLongitude(); case EAST -> incrementLongitude();
} };
logger.info(String.format("Updated coordinates towards %s, now is %d-%d", if (updatedPosition)
direction, currentPosition.latitude(), currentPosition.longitude())); logger.info(String.format("Updated coordinates towards %s, now is %d-%d",
direction, currentPosition.latitude(), currentPosition.longitude()));
return currentPosition; return currentPosition;
} }
private void incrementLatitude() { private boolean incrementLatitude() {
int latitude = currentPosition.latitude(); int latitude = currentPosition.latitude();
int newLatitude = positionResolver.getIncrementedPosition(latitude, height); int newLatitude = positionResolver.getIncrementedPosition(latitude, height);
setNewLatitude(newLatitude); return setNewLatitudeIfNoObstacle(newLatitude);
} }
private void decrementLatitude() { private boolean decrementLatitude() {
int latitude = currentPosition.latitude(); int latitude = currentPosition.latitude();
int newLatitude = positionResolver.getDecrementedPosition(latitude, height); int newLatitude = positionResolver.getDecrementedPosition(latitude, height);
setNewLatitude(newLatitude); return setNewLatitudeIfNoObstacle(newLatitude);
} }
private void incrementLongitude() { private boolean incrementLongitude() {
int longitude = currentPosition.longitude(); int longitude = currentPosition.longitude();
int newLongitude = positionResolver.getIncrementedPosition(longitude, width); int newLongitude = positionResolver.getIncrementedPosition(longitude, width);
setNewLongitude(newLongitude); return setNewLongitudeIfNoObstacle(newLongitude);
} }
private void decrementLongitude() { private boolean decrementLongitude() {
int longitude = currentPosition.longitude(); int longitude = currentPosition.longitude();
int newLongitude = positionResolver.getDecrementedPosition(longitude, width); int newLongitude = positionResolver.getDecrementedPosition(longitude, width);
setNewLongitude(newLongitude); return setNewLongitudeIfNoObstacle(newLongitude);
} }
private void setNewLatitude(int newLatitude) { private boolean setNewLatitudeIfNoObstacle(int newLatitude) {
currentPosition = currentPosition.ofUpdatedLatitude(newLatitude); Coordinates newCoordinates = currentPosition.ofUpdatedLatitude(newLatitude);
return setNewPositionIfNoObstacle(newCoordinates);
} }
private void setNewLongitude(int newLongitude) { private boolean setNewLongitudeIfNoObstacle(int newLongitude) {
currentPosition = currentPosition.ofUpdatedLongitude(newLongitude); Coordinates newCoordinates = currentPosition.ofUpdatedLongitude(newLongitude);
return setNewPositionIfNoObstacle(newCoordinates);
}
private boolean setNewPositionIfNoObstacle(Coordinates newCoordinates) {
if (willCollideWithObstacle(newCoordinates))
return false;
else {
currentPosition = newCoordinates;
return true;
}
}
private boolean willCollideWithObstacle(Coordinates newCoordinates) {
boolean willCollide = obstaclesLocalizations.contains(newCoordinates);
if (willCollide) {
var collisionReport = String.format("There is an obstacle at %s, therefore Rover will keep the same position", newCoordinates);
logger.info(collisionReport);
}
return willCollide;
} }
public Coordinates getCurrentPosition() { public Coordinates getCurrentPosition() {

View File

@ -1,10 +1,12 @@
package cat.hack3.codingtests.marsrover; package cat.hack3.codingtests.marsrover;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.List; import java.util.List;
import static cat.hack3.codingtests.marsrover.Direction.SOUTH; import static cat.hack3.codingtests.marsrover.Direction.SOUTH;
import static org.testng.Assert.assertEquals;
public class MarsRoverWithObstaclesTest { public class MarsRoverWithObstaclesTest {
@ -28,6 +30,13 @@ public class MarsRoverWithObstaclesTest {
rover = new MarsRover(marsMap, SOUTH); rover = new MarsRover(marsMap, SOUTH);
} }
@Test
public void findAnObstacleAtFirstMove() {
Coordinates originalCoordinates = rover.getCurrentCoordinates();
rover.moveForward();
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
}
} }