1
0
Fork 0

simplify internal contracts of Map

This commit is contained in:
Xavier Fontanet 2024-06-22 15:27:06 +02:00
parent ff548d0338
commit 026195c849
1 changed files with 22 additions and 23 deletions

View File

@ -20,6 +20,7 @@ public class MarsMap {
private final int height;
private final int width;
private Coordinates currentPosition;
private Direction currentMovementDirection;
private final List<Coordinates> obstaclesLocalizations;
private final MapIncrementalPositionResolver positionResolver;
@ -33,58 +34,56 @@ public class MarsMap {
positionResolver = new MapIncrementalPositionResolver(FIRST_POSITION_IN_MAP, INCREMENT_UNIT);
}
public void updatePositionTowards(Direction direction) {
boolean updatedPosition = switch (direction) {
public synchronized void updatePositionTowards(Direction direction) {
currentMovementDirection = direction;
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()));
}
currentMovementDirection = null;
}
private boolean incrementLatitude() {
private void incrementLatitude() {
int latitude = currentPosition.latitude();
int newLatitude = positionResolver.getIncrementedPosition(latitude, height);
return setNewLatitudeIfNoObstacle(newLatitude);
setNewLatitudeIfNoObstacle(newLatitude);
}
private boolean decrementLatitude() {
private void decrementLatitude() {
int latitude = currentPosition.latitude();
int newLatitude = positionResolver.getDecrementedPosition(latitude, height);
return setNewLatitudeIfNoObstacle(newLatitude);
setNewLatitudeIfNoObstacle(newLatitude);
}
private boolean incrementLongitude() {
private void incrementLongitude() {
int longitude = currentPosition.longitude();
int newLongitude = positionResolver.getIncrementedPosition(longitude, width);
return setNewLongitudeIfNoObstacle(newLongitude);
setNewLongitudeIfNoObstacle(newLongitude);
}
private boolean decrementLongitude() {
private void decrementLongitude() {
int longitude = currentPosition.longitude();
int newLongitude = positionResolver.getDecrementedPosition(longitude, width);
return setNewLongitudeIfNoObstacle(newLongitude);
setNewLongitudeIfNoObstacle(newLongitude);
}
private boolean setNewLatitudeIfNoObstacle(int newLatitude) {
private void setNewLatitudeIfNoObstacle(int newLatitude) {
Coordinates newCoordinates = currentPosition.ofUpdatedLatitude(newLatitude);
return setNewPositionIfNoObstacle(newCoordinates);
setNewPositionIfNoObstacle(newCoordinates);
}
private boolean setNewLongitudeIfNoObstacle(int newLongitude) {
private void setNewLongitudeIfNoObstacle(int newLongitude) {
Coordinates newCoordinates = currentPosition.ofUpdatedLongitude(newLongitude);
return setNewPositionIfNoObstacle(newCoordinates);
setNewPositionIfNoObstacle(newCoordinates);
}
private boolean setNewPositionIfNoObstacle(Coordinates newCoordinates) {
if (willCollideWithObstacle(newCoordinates))
return false;
else {
private void setNewPositionIfNoObstacle(Coordinates newCoordinates) {
if (!willCollideWithObstacle(newCoordinates)) {
currentPosition = newCoordinates;
return true;
logger.info(String.format("Updated coordinates towards %s, now is %d-%d",
currentMovementDirection, currentPosition.latitude(), currentPosition.longitude()));
}
}