1
0
Fork 0

move the new coordinate resolution to MarsMap

This commit is contained in:
Xavier Fontanet 2024-06-20 21:18:07 +02:00
parent 9bac80db8e
commit eac2fb3446
4 changed files with 42 additions and 31 deletions

View File

@ -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

View File

@ -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);
}
}

View File

@ -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() {

View File

@ -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);
}