diff --git a/src/main/java/cat/hack3/codingtests/marsrover/Coordinates.java b/src/main/java/cat/hack3/codingtests/marsrover/Coordinates.java index 60aa99e..a45f87e 100644 --- a/src/main/java/cat/hack3/codingtests/marsrover/Coordinates.java +++ b/src/main/java/cat/hack3/codingtests/marsrover/Coordinates.java @@ -1,22 +1,8 @@ package cat.hack3.codingtests.marsrover; - - -import java.util.logging.Logger; - -/** - * Notice that the representation is the following: - * Latitude is Y, starts from 1 on the top and goes down incrementally - * Longitude is X, starts from 1 on the left and goes to right incrementally - * 1 2 3 - * 1 1-1 1-2 1-3 - * 2 2-1 2-2 2-3 - * 3 3-1 3-2 3-3 - */ public class Coordinates { private final int latitude; private final int longitude; - private final Logger logger = Logger.getLogger(Coordinates.class.getName()); public static Coordinates of(int latitude, int longitude) { return new Coordinates(latitude, longitude); @@ -27,19 +13,12 @@ public class Coordinates { this.longitude = longitude; } - public Coordinates getNextPositionTowards(Direction direction) { - var newLatitude = latitude; - var newLongitude = longitude; - switch (direction) { - case NORTH -> newLatitude--; - case SOUTH -> newLatitude++; - case WEST -> newLongitude--; - case EAST -> newLongitude++; - } - logger.info(String.format("Updated coordinates towards %s, now is %d-%d", - direction, newLatitude, newLongitude)); + public int getLatitude() { + return latitude; + } - return new Coordinates(newLatitude, newLongitude); + public int getLongitude() { + return longitude; } @Override diff --git a/src/main/java/cat/hack3/codingtests/marsrover/MarsMap.java b/src/main/java/cat/hack3/codingtests/marsrover/MarsMap.java index 6892c06..2c7e772 100644 --- a/src/main/java/cat/hack3/codingtests/marsrover/MarsMap.java +++ b/src/main/java/cat/hack3/codingtests/marsrover/MarsMap.java @@ -1,12 +1,45 @@ package cat.hack3.codingtests.marsrover; +import java.util.logging.Logger; + +/** + * Notice that the representation is the following: + * Latitude is Y, starts from 1 on the top and goes down incrementally + * Longitude is X, starts from 1 on the left and goes to right incrementally + * 1 2 3 + * 1 1-1 1-2 1-3 + * 2 2-1 2-2 2-3 + * 3 3-1 3-2 3-3 + */ public class MarsMap { + Logger logger = Logger.getLogger(this.getClass().getName()); + private final int width; private final int height; + private Coordinates currentPosition; - public MarsMap(int width, int height) { + public MarsMap(int width, int height, Coordinates startingCoordinates) { this.width = width; this.height = height; + currentPosition = startingCoordinates; } + public Coordinates updatePositionTowards(Direction direction) { + return currentPosition = resolveNextPositionTowards(direction); + } + + private Coordinates resolveNextPositionTowards(Direction direction) { + var newLatitude = currentPosition.getLatitude(); + var newLongitude = currentPosition.getLongitude(); + switch (direction) { + case NORTH -> newLatitude--; + case SOUTH -> newLatitude++; + case WEST -> newLongitude--; + case EAST -> newLongitude++; + } + logger.info(String.format("Updated coordinates towards %s, now is %d-%d", + direction, newLatitude, newLongitude)); + + return new Coordinates(newLatitude, newLongitude); + } } diff --git a/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java b/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java index 446f799..28dc585 100644 --- a/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java +++ b/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java @@ -12,11 +12,11 @@ public class MarsRover { } public Coordinates moveForward() { - return currentCoordinates = currentCoordinates.getNextPositionTowards(currentDirection); + return currentCoordinates = marsMap.updatePositionTowards(currentDirection); } public Coordinates moveBackwards() { - return currentCoordinates = currentCoordinates.getNextPositionTowards(currentDirection.reversed()); + return currentCoordinates = marsMap.updatePositionTowards(currentDirection.reversed()); } public Direction turnLeft() { diff --git a/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverTest.java b/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverTest.java index 03adc83..2f10333 100644 --- a/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverTest.java +++ b/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverTest.java @@ -17,11 +17,10 @@ public class MarsRoverTest { public void setUp() { int mapWidth = 10; int mapHeight = 10; - var marsMap = new MarsMap(mapWidth, mapHeight); - int latitudeStartingPoint = 2; int longitudeStartingPoint = 3; var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint); + var marsMap = new MarsMap(mapWidth, mapHeight, startingCoordinates); rover = new MarsRover(marsMap, startingCoordinates, SOUTH); }