move to PositionResolver the concern of the appropiated threshold for each case
This commit is contained in:
parent
3b8595c630
commit
cae3fa399e
|
@ -18,7 +18,7 @@ public class MarsRover implements RotableRiderRover{
|
|||
|
||||
@Override
|
||||
public void moveTowards(Direction direction) {
|
||||
Coordinates newPosition = marsMap.getNewPositionTowards(direction);
|
||||
Coordinates newPosition = marsMap.getNextPositionTowards(direction);
|
||||
boolean willCollide = marsMap.willCollideWithObstacle(newPosition);
|
||||
if (willCollide) {
|
||||
roverActionReporter.reportObstacle(newPosition);
|
||||
|
|
|
@ -10,7 +10,7 @@ public class RoverActionReporter {
|
|||
Logger logger = Logger.getLogger(this.getClass().getName());
|
||||
|
||||
private static final String OBSTACLE_REPORT_MESSAGE = "There is an obstacle at %s, therefore I will keep the same position";
|
||||
private static final String MOVEMENT_REPORT_MESSAGE = "Updated coordinates towards %s, now is %d-%d";
|
||||
private static final String MOVEMENT_REPORT_MESSAGE = "Updated coordinates towards %s, now is %s-%s";
|
||||
public static final String ROTATION_REPORT_MESSGE = "Rotated towards %s, now the direction is %s";
|
||||
|
||||
public void reportObstacle(Coordinates newPosition) {
|
||||
|
|
|
@ -1,17 +1,30 @@
|
|||
package cat.hack3.codingtests.marsrover.cartography;
|
||||
|
||||
public record Coordinates(int latitude, int longitude) {
|
||||
public record Coordinates(Latitude latitude, Longitude longitude) {
|
||||
|
||||
public static Coordinates of(int latitude, int longitude) {
|
||||
return new Coordinates(latitude, longitude);
|
||||
public static Coordinates of(long latitude, long longitude) {
|
||||
return new Coordinates(new Latitude(latitude), new Longitude(longitude));
|
||||
}
|
||||
|
||||
public Coordinates ofUpdatedLatitude(int newLatitude) {
|
||||
return new Coordinates(newLatitude, longitude);
|
||||
public Coordinates ofUpdatedLatitude(long newLatitude) {
|
||||
return Coordinates.of(newLatitude, longitude.pointer);
|
||||
}
|
||||
|
||||
public Coordinates ofUpdatedLongitude(int newLongitude) {
|
||||
return new Coordinates(latitude, newLongitude);
|
||||
public Coordinates ofUpdatedLongitude(long newLongitude) {
|
||||
return Coordinates.of(latitude.pointer, newLongitude);
|
||||
}
|
||||
|
||||
record Latitude(long pointer) {
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.valueOf(pointer);
|
||||
}
|
||||
}
|
||||
record Longitude(long pointer) {
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.valueOf(pointer);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,22 +1,45 @@
|
|||
package cat.hack3.codingtests.marsrover.cartography;
|
||||
|
||||
import cat.hack3.codingtests.marsrover.cartography.Coordinates.Latitude;
|
||||
import cat.hack3.codingtests.marsrover.cartography.Coordinates.Longitude;
|
||||
|
||||
public class MapIncrementalPositionResolver {
|
||||
|
||||
private final int firstPositionInMap;
|
||||
private final int incrementUnit;
|
||||
private final long maximumYPosition;
|
||||
private final long maximumXPosition;
|
||||
|
||||
public MapIncrementalPositionResolver(int firstPositionInMap, int incrementUnit) {
|
||||
public MapIncrementalPositionResolver(int firstPositionInMap, int incrementUnit, long maximumYPosition, long maximumXPosition) {
|
||||
this.firstPositionInMap = firstPositionInMap;
|
||||
this.incrementUnit = incrementUnit;
|
||||
this.maximumYPosition = maximumYPosition;
|
||||
this.maximumXPosition = maximumXPosition;
|
||||
}
|
||||
|
||||
int getIncrementedPosition(int currentPosition, int threshold) {
|
||||
long getIncremented(Longitude longitude) {
|
||||
return getIncremented(longitude.pointer(), maximumXPosition);
|
||||
}
|
||||
|
||||
long getIncremented(Latitude latitude) {
|
||||
return getIncremented(latitude.pointer(), maximumYPosition);
|
||||
}
|
||||
|
||||
private long getIncremented(long currentPosition, long threshold) {
|
||||
return currentPosition + incrementUnit > threshold
|
||||
? firstPositionInMap
|
||||
: ++currentPosition;
|
||||
}
|
||||
|
||||
int getDecrementedPosition(int currentPosition, int threshold) {
|
||||
long getDecremented(Latitude latitude) {
|
||||
return getDecremented(latitude.pointer(), maximumYPosition);
|
||||
}
|
||||
|
||||
long getDecremented(Longitude longitude) {
|
||||
return getDecremented(longitude.pointer(), maximumYPosition);
|
||||
}
|
||||
|
||||
private long getDecremented(long currentPosition, long threshold) {
|
||||
return currentPosition - incrementUnit < firstPositionInMap
|
||||
? threshold
|
||||
: --currentPosition;
|
||||
|
|
|
@ -31,10 +31,10 @@ public class MarsMap {
|
|||
this.width = width;
|
||||
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, height, width);
|
||||
}
|
||||
|
||||
public Coordinates getNewPositionTowards(Direction direction) {
|
||||
public Coordinates getNextPositionTowards(Direction direction) {
|
||||
return switch (direction) {
|
||||
case NORTH -> getDecrementLatitude();
|
||||
case SOUTH -> getIncrementLatitude();
|
||||
|
@ -44,26 +44,26 @@ public class MarsMap {
|
|||
}
|
||||
|
||||
private Coordinates getIncrementLatitude() {
|
||||
int latitude = currentPosition.latitude();
|
||||
int newLatitude = positionResolver.getIncrementedPosition(latitude, height);
|
||||
var latitude = currentPosition.latitude();
|
||||
long newLatitude = positionResolver.getIncremented(latitude);
|
||||
return currentPosition.ofUpdatedLatitude(newLatitude);
|
||||
}
|
||||
|
||||
private Coordinates getDecrementLatitude() {
|
||||
int latitude = currentPosition.latitude();
|
||||
int newLatitude = positionResolver.getDecrementedPosition(latitude, height);
|
||||
var latitude = currentPosition.latitude();
|
||||
long newLatitude = positionResolver.getDecremented(latitude);
|
||||
return currentPosition.ofUpdatedLatitude(newLatitude);
|
||||
}
|
||||
|
||||
private Coordinates getIncrementLongitude() {
|
||||
int longitude = currentPosition.longitude();
|
||||
int newLongitude = positionResolver.getIncrementedPosition(longitude, width);
|
||||
var longitude = currentPosition.longitude();
|
||||
long newLongitude = positionResolver.getIncremented(longitude);
|
||||
return currentPosition.ofUpdatedLongitude(newLongitude);
|
||||
}
|
||||
|
||||
private Coordinates getDecrementLongitude() {
|
||||
int longitude = currentPosition.longitude();
|
||||
int newLongitude = positionResolver.getDecrementedPosition(longitude, width);
|
||||
var longitude = currentPosition.longitude();
|
||||
long newLongitude = positionResolver.getDecremented(longitude);
|
||||
return currentPosition.ofUpdatedLongitude(newLongitude);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue