move the new coordinate resolution to MarsMap
This commit is contained in:
parent
9bac80db8e
commit
eac2fb3446
|
@ -1,22 +1,8 @@
|
||||||
package cat.hack3.codingtests.marsrover;
|
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 {
|
public class Coordinates {
|
||||||
private final int latitude;
|
private final int latitude;
|
||||||
private final int longitude;
|
private final int longitude;
|
||||||
private final Logger logger = Logger.getLogger(Coordinates.class.getName());
|
|
||||||
|
|
||||||
public static Coordinates of(int latitude, int longitude) {
|
public static Coordinates of(int latitude, int longitude) {
|
||||||
return new Coordinates(latitude, longitude);
|
return new Coordinates(latitude, longitude);
|
||||||
|
@ -27,19 +13,12 @@ public class Coordinates {
|
||||||
this.longitude = longitude;
|
this.longitude = longitude;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Coordinates getNextPositionTowards(Direction direction) {
|
public int getLatitude() {
|
||||||
var newLatitude = latitude;
|
return 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));
|
|
||||||
|
|
||||||
return new Coordinates(newLatitude, newLongitude);
|
public int getLongitude() {
|
||||||
|
return longitude;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,12 +1,45 @@
|
||||||
package cat.hack3.codingtests.marsrover;
|
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 {
|
public class MarsMap {
|
||||||
|
Logger logger = Logger.getLogger(this.getClass().getName());
|
||||||
|
|
||||||
private final int width;
|
private final int width;
|
||||||
private final int height;
|
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.width = width;
|
||||||
this.height = height;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,11 +12,11 @@ public class MarsRover {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Coordinates moveForward() {
|
public Coordinates moveForward() {
|
||||||
return currentCoordinates = currentCoordinates.getNextPositionTowards(currentDirection);
|
return currentCoordinates = marsMap.updatePositionTowards(currentDirection);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Coordinates moveBackwards() {
|
public Coordinates moveBackwards() {
|
||||||
return currentCoordinates = currentCoordinates.getNextPositionTowards(currentDirection.reversed());
|
return currentCoordinates = marsMap.updatePositionTowards(currentDirection.reversed());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Direction turnLeft() {
|
public Direction turnLeft() {
|
||||||
|
|
|
@ -17,11 +17,10 @@ public class MarsRoverTest {
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
int mapWidth = 10;
|
int mapWidth = 10;
|
||||||
int mapHeight = 10;
|
int mapHeight = 10;
|
||||||
var marsMap = new MarsMap(mapWidth, mapHeight);
|
|
||||||
|
|
||||||
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);
|
||||||
rover = new MarsRover(marsMap, startingCoordinates, SOUTH);
|
rover = new MarsRover(marsMap, startingCoordinates, SOUTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue