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 width;
private Coordinates currentPosition;
private final List<Coordinates> obstaclesLocalizations;
private final MapIncrementalPositionResolver positionResolver;
@SafeVarargs
public MarsMap(int height, int width, Coordinates startingCoordinates, List<Coordinates>... obstaclesLocalizations) {
this.height = height;
this.width = width;
currentPosition = startingCoordinates;
this.obstaclesLocalizations = obstaclesLocalizations.length > 0 ? obstaclesLocalizations[0] : List.of();
positionResolver = new MapIncrementalPositionResolver(FIRST_POSITION_IN_MAP, INCREMENT_UNIT);
}
public Coordinates updatePositionTowards(Direction direction) {
switch (direction) {
boolean updatedPosition = switch (direction) {
case NORTH -> decrementLatitude();
case SOUTH -> incrementLatitude();
case WEST -> decrementLongitude();
case EAST -> incrementLongitude();
}
};
if (updatedPosition)
logger.info(String.format("Updated coordinates towards %s, now is %d-%d",
direction, currentPosition.latitude(), currentPosition.longitude()));
return currentPosition;
}
private void incrementLatitude() {
private boolean incrementLatitude() {
int latitude = currentPosition.latitude();
int newLatitude = positionResolver.getIncrementedPosition(latitude, height);
setNewLatitude(newLatitude);
return setNewLatitudeIfNoObstacle(newLatitude);
}
private void decrementLatitude() {
private boolean decrementLatitude() {
int latitude = currentPosition.latitude();
int newLatitude = positionResolver.getDecrementedPosition(latitude, height);
setNewLatitude(newLatitude);
return setNewLatitudeIfNoObstacle(newLatitude);
}
private void incrementLongitude() {
private boolean incrementLongitude() {
int longitude = currentPosition.longitude();
int newLongitude = positionResolver.getIncrementedPosition(longitude, width);
setNewLongitude(newLongitude);
return setNewLongitudeIfNoObstacle(newLongitude);
}
private void decrementLongitude() {
private boolean decrementLongitude() {
int longitude = currentPosition.longitude();
int newLongitude = positionResolver.getDecrementedPosition(longitude, width);
setNewLongitude(newLongitude);
return setNewLongitudeIfNoObstacle(newLongitude);
}
private void setNewLatitude(int newLatitude) {
currentPosition = currentPosition.ofUpdatedLatitude(newLatitude);
private boolean setNewLatitudeIfNoObstacle(int newLatitude) {
Coordinates newCoordinates = currentPosition.ofUpdatedLatitude(newLatitude);
return setNewPositionIfNoObstacle(newCoordinates);
}
private void setNewLongitude(int newLongitude) {
currentPosition = currentPosition.ofUpdatedLongitude(newLongitude);
private boolean setNewLongitudeIfNoObstacle(int 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() {

View File

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