some refinements and cleaning code
This commit is contained in:
parent
607c908e9d
commit
adbca95241
|
@ -0,0 +1,24 @@
|
||||||
|
package cat.hack3.codingtests.marsrover;
|
||||||
|
|
||||||
|
public class MapIncrementalPositionResolver {
|
||||||
|
|
||||||
|
private final int firstPositionInMap;
|
||||||
|
private final int incrementUnit;
|
||||||
|
|
||||||
|
public MapIncrementalPositionResolver(int firstPositionInMap, int incrementUnit) {
|
||||||
|
this.firstPositionInMap = firstPositionInMap;
|
||||||
|
this.incrementUnit = incrementUnit;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getIncrementedPosition(int currentPosition, int threshold) {
|
||||||
|
return currentPosition + incrementUnit > threshold
|
||||||
|
? firstPositionInMap
|
||||||
|
: ++currentPosition;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getDecrementedPosition(int currentPosition, int threshold) {
|
||||||
|
return currentPosition - incrementUnit < firstPositionInMap
|
||||||
|
? threshold
|
||||||
|
: --currentPosition;
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,15 +13,21 @@ import java.util.logging.Logger;
|
||||||
*/
|
*/
|
||||||
public class MarsMap {
|
public class MarsMap {
|
||||||
Logger logger = Logger.getLogger(this.getClass().getName());
|
Logger logger = Logger.getLogger(this.getClass().getName());
|
||||||
|
private static final int FIRST_POSITION_IN_MAP = 1;
|
||||||
|
private static final int INCREMENT_UNIT = 1;
|
||||||
|
|
||||||
private final int width;
|
|
||||||
private final int height;
|
private final int height;
|
||||||
|
private final int width;
|
||||||
private Coordinates currentPosition;
|
private Coordinates currentPosition;
|
||||||
|
|
||||||
public MarsMap(int width, int height, Coordinates startingCoordinates) {
|
private final MapIncrementalPositionResolver positionResolver;
|
||||||
this.width = width;
|
|
||||||
|
public MarsMap(int height, int width, Coordinates startingCoordinates) {
|
||||||
this.height = height;
|
this.height = height;
|
||||||
|
this.width = width;
|
||||||
currentPosition = startingCoordinates;
|
currentPosition = startingCoordinates;
|
||||||
|
|
||||||
|
positionResolver = new MapIncrementalPositionResolver(FIRST_POSITION_IN_MAP, INCREMENT_UNIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Coordinates updatePositionTowards(Direction direction) {
|
public Coordinates updatePositionTowards(Direction direction) {
|
||||||
|
@ -39,40 +45,28 @@ public class MarsMap {
|
||||||
|
|
||||||
private void incrementLatitude() {
|
private void incrementLatitude() {
|
||||||
int latitude = currentPosition.latitude();
|
int latitude = currentPosition.latitude();
|
||||||
int newLatitude = getIncrementedPosition(latitude, height);
|
int newLatitude = positionResolver.getIncrementedPosition(latitude, height);
|
||||||
|
setNewLatitude(newLatitude);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void decrementLatitude() {
|
||||||
|
int latitude = currentPosition.latitude();
|
||||||
|
int newLatitude = positionResolver.getDecrementedPosition(latitude, height);
|
||||||
setNewLatitude(newLatitude);
|
setNewLatitude(newLatitude);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void incrementLongitude() {
|
private void incrementLongitude() {
|
||||||
int longitude = currentPosition.longitude();
|
int longitude = currentPosition.longitude();
|
||||||
int newLongitude = getIncrementedPosition(longitude, width);
|
int newLongitude = positionResolver.getIncrementedPosition(longitude, width);
|
||||||
setNewLongitude(newLongitude);
|
setNewLongitude(newLongitude);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getIncrementedPosition(int currentPosition, int threshold) {
|
|
||||||
return currentPosition + 1 > threshold
|
|
||||||
? 1
|
|
||||||
: ++currentPosition;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void decrementLatitude() {
|
|
||||||
int latitude = currentPosition.latitude();
|
|
||||||
int newLatitude = decrementPosition(latitude, height);
|
|
||||||
setNewLatitude(newLatitude);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void decrementLongitude() {
|
private void decrementLongitude() {
|
||||||
int longitude = currentPosition.longitude();
|
int longitude = currentPosition.longitude();
|
||||||
int newLongitude = decrementPosition(longitude, width);
|
int newLongitude = positionResolver.getDecrementedPosition(longitude, width);
|
||||||
setNewLongitude(newLongitude);
|
setNewLongitude(newLongitude);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int decrementPosition(int currentPosition, int threshold) {
|
|
||||||
return currentPosition - 1 < 1
|
|
||||||
? threshold
|
|
||||||
: --currentPosition;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setNewLatitude(int newLatitude) {
|
private void setNewLatitude(int newLatitude) {
|
||||||
currentPosition = currentPosition.ofUpdatedLatitude(newLatitude);
|
currentPosition = currentPosition.ofUpdatedLatitude(newLatitude);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ public class MarsRoverTest {
|
||||||
int latitudeStartingPoint = 2;
|
int latitudeStartingPoint = 2;
|
||||||
int longitudeStartingPoint = 3;
|
int longitudeStartingPoint = 3;
|
||||||
var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint);
|
var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint);
|
||||||
var marsMap = new MarsMap(mapWidth, mapHeight, startingCoordinates);
|
var marsMap = new MarsMap(mapHeight, mapWidth, startingCoordinates);
|
||||||
rover = new MarsRover(marsMap, SOUTH);
|
rover = new MarsRover(marsMap, SOUTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue