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