move backward successfully
This commit is contained in:
parent
6906fec8a1
commit
72ca8a2629
|
@ -3,7 +3,16 @@ package cat.hack3.codingtests.marsrover;
|
|||
import static cat.hack3.codingtests.marsrover.MarsMap.Direction.*;
|
||||
|
||||
public class MarsMap {
|
||||
public enum Direction {NORTH, SOUTH, EAST, WEST}
|
||||
public enum Direction {NORTH, SOUTH, EAST, WEST;
|
||||
public Direction reversed() {
|
||||
return switch (this) {
|
||||
case NORTH -> SOUTH;
|
||||
case SOUTH -> NORTH;
|
||||
case EAST -> WEST;
|
||||
case WEST -> EAST;
|
||||
};
|
||||
}
|
||||
}
|
||||
private final int width;
|
||||
private final int height;
|
||||
|
||||
|
|
|
@ -15,6 +15,10 @@ public class MarsRover {
|
|||
return currentCoordinates.updatePositionTowards(currentDirection);
|
||||
}
|
||||
|
||||
public Coordinates moveBackwards() {
|
||||
return currentCoordinates.updatePositionTowards(currentDirection.reversed());
|
||||
}
|
||||
|
||||
public MarsMap.Direction turnLeft() {
|
||||
currentDirection = marsMap.changeDirectionToLeft(currentDirection);
|
||||
return currentDirection;
|
||||
|
|
|
@ -37,6 +37,13 @@ public class MarsRoverTest {
|
|||
assertEquals(rover.getCurrentDirection(), SOUTH);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stepBackwards() {
|
||||
Coordinates currentPosition = rover.moveBackwards();
|
||||
assertEquals(currentPosition, Coordinates.of(1, 3));
|
||||
assertEquals(rover.getCurrentDirection(), SOUTH);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void turnLef() {
|
||||
Direction currentDirection = rover.turnLeft();
|
||||
|
|
Loading…
Reference in New Issue