diff --git a/mars-station/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java b/mars-station/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java index 1627df4..61b35d4 100644 --- a/mars-station/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java +++ b/mars-station/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java @@ -9,7 +9,7 @@ import java.util.logging.Logger; public class MarsRover { Logger logger = Logger.getLogger(this.getClass().getName()); - private enum Rotation {LEFT, RIGHT} + enum Rotation {LEFT, RIGHT} private final MarsMap marsMap; private Direction currentDirection; @@ -22,25 +22,18 @@ public class MarsRover { marsMap.updatePositionTowards(direction); } - public Coordinates moveBackwards() { - return marsMap.updatePositionTowards(currentDirection.reversed()); + public void rotateTowards(Rotation rotation) { + var newDirection = switch (rotation) { + case LEFT -> currentDirection.getNextDirectionRotatingMinus90Degrees(); + case RIGHT -> currentDirection.getNextDirectionRotating90Degrees(); + }; + reportNewDirection(rotation, newDirection); + currentDirection = newDirection; } - public Direction turnLeft() { - currentDirection = currentDirection.getNextDirectionRotatingToLeft(); - reportNewDirection(Rotation.LEFT); - return currentDirection; - } - - public Direction turnRight() { - currentDirection = currentDirection.getNextDirectionRotatingToRight(); - reportNewDirection(Rotation.RIGHT); - return currentDirection; - } - - private void reportNewDirection(Rotation rotation) { + private void reportNewDirection(Rotation rotation, Direction newDirection) { logger.info(String.format("Rotated towards %s, now the direction is %s", - rotation, currentDirection)); + rotation, newDirection)); } public Coordinates getCurrentCoordinates() { diff --git a/mars-station/src/main/java/cat/hack3/codingtests/marsrover/TurnRightCommand.java b/mars-station/src/main/java/cat/hack3/codingtests/marsrover/TurnRightCommand.java new file mode 100644 index 0000000..4b1738c --- /dev/null +++ b/mars-station/src/main/java/cat/hack3/codingtests/marsrover/TurnRightCommand.java @@ -0,0 +1,14 @@ +package cat.hack3.codingtests.marsrover; + +public class TurnRightCommand implements MarsRoverCommand{ + private final MarsRover marsRover; + + public TurnRightCommand(MarsRover marsRover) { + this.marsRover = marsRover; + } + + @Override + public void execute() { + marsRover.rotateTowards(MarsRover.Rotation.RIGHT); + } +} diff --git a/mars-station/src/main/java/cat/hack3/codingtests/marsrover/cartography/Direction.java b/mars-station/src/main/java/cat/hack3/codingtests/marsrover/cartography/Direction.java index fc3fbe3..6e2205c 100644 --- a/mars-station/src/main/java/cat/hack3/codingtests/marsrover/cartography/Direction.java +++ b/mars-station/src/main/java/cat/hack3/codingtests/marsrover/cartography/Direction.java @@ -12,7 +12,7 @@ public enum Direction { }; } - public Direction getNextDirectionRotatingToLeft() { + public Direction getNextDirectionRotatingMinus90Degrees() { return switch (this) { case NORTH -> WEST; case SOUTH -> EAST; @@ -21,7 +21,7 @@ public enum Direction { }; } - public Direction getNextDirectionRotatingToRight() { + public Direction getNextDirectionRotating90Degrees() { return switch (this) { case NORTH -> EAST; case SOUTH -> WEST; diff --git a/mars-station/src/main/java/cat/hack3/codingtests/marsrover/cartography/MarsMap.java b/mars-station/src/main/java/cat/hack3/codingtests/marsrover/cartography/MarsMap.java index 5551ed7..fa38991 100644 --- a/mars-station/src/main/java/cat/hack3/codingtests/marsrover/cartography/MarsMap.java +++ b/mars-station/src/main/java/cat/hack3/codingtests/marsrover/cartography/MarsMap.java @@ -33,7 +33,7 @@ public class MarsMap { positionResolver = new MapIncrementalPositionResolver(FIRST_POSITION_IN_MAP, INCREMENT_UNIT); } - public Coordinates updatePositionTowards(Direction direction) { + public void updatePositionTowards(Direction direction) { boolean updatedPosition = switch (direction) { case NORTH -> decrementLatitude(); case SOUTH -> incrementLatitude(); @@ -43,8 +43,6 @@ public class MarsMap { if (updatedPosition) logger.info(String.format("Updated coordinates towards %s, now is %d-%d", direction, currentPosition.latitude(), currentPosition.longitude())); - - return currentPosition; } private boolean incrementLatitude() { diff --git a/mars-station/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverTest.java b/mars-station/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverTest.java index ac4d45d..304a722 100644 --- a/mars-station/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverTest.java +++ b/mars-station/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverTest.java @@ -15,6 +15,9 @@ public class MarsRoverTest { private MarsRover rover; private MoveForwardCommand moveForwardCommand; + private MoveBackwardsCommand moveBackwardsCommand; + private TurnLeftCommand turnLeftCommand; + private TurnRightCommand turnRightCommand; @BeforeMethod public void setUp() { @@ -27,6 +30,9 @@ public class MarsRoverTest { rover = new MarsRover(marsMap, SOUTH); moveForwardCommand = new MoveForwardCommand(rover); + moveBackwardsCommand = new MoveBackwardsCommand(rover); + turnLeftCommand = new TurnLeftCommand(rover); + turnRightCommand = new TurnRightCommand(rover); } @Test @@ -43,7 +49,8 @@ public class MarsRoverTest { public void stepBackwards() { Direction originalDirection = rover.getCurrentDirection(); - Coordinates currentPosition = rover.moveBackwards(); + moveBackwardsCommand.execute(); + Coordinates currentPosition = rover.getCurrentCoordinates(); assertEquals(currentPosition, Coordinates.of(1, 3)); assertEquals(rover.getCurrentDirection(), originalDirection); @@ -51,20 +58,30 @@ public class MarsRoverTest { @Test public void turnLef() { - assertEquals(rover.turnLeft(), EAST); - assertEquals(rover.turnLeft(), NORTH); - assertEquals(rover.turnLeft(), WEST); - assertEquals(rover.turnLeft(), SOUTH); - assertEquals(rover.turnLeft(), EAST); + turnLeftCommand.execute(); + assertEquals(rover.getCurrentDirection(), EAST); + turnLeftCommand.execute(); + assertEquals(rover.getCurrentDirection(), NORTH); + turnLeftCommand.execute(); + assertEquals(rover.getCurrentDirection(), WEST); + turnLeftCommand.execute(); + assertEquals(rover.getCurrentDirection(), SOUTH); + turnLeftCommand.execute(); + assertEquals(rover.getCurrentDirection(), EAST); } @Test public void turnRight() { - assertEquals(rover.turnRight(), WEST); - assertEquals(rover.turnRight(), NORTH); - assertEquals(rover.turnRight(), EAST); - assertEquals(rover.turnRight(), SOUTH); - assertEquals(rover.turnRight(), WEST); + turnRightCommand.execute(); + assertEquals(rover.getCurrentDirection(), WEST); + turnRightCommand.execute(); + assertEquals(rover.getCurrentDirection(), NORTH); + turnRightCommand.execute(); + assertEquals(rover.getCurrentDirection(), EAST); + turnRightCommand.execute(); + assertEquals(rover.getCurrentDirection(), SOUTH); + turnRightCommand.execute(); + assertEquals(rover.getCurrentDirection(), WEST); } @Test @@ -83,7 +100,7 @@ public class MarsRoverTest { Coordinates originalCoordinates = rover.getCurrentCoordinates(); Direction originalDirection = rover.getCurrentDirection(); - repeatAction(10, () -> rover.moveBackwards()); + repeatAction(10, () -> moveBackwardsCommand.execute()); assertEquals(rover.getCurrentCoordinates(), originalCoordinates); assertEquals(rover.getCurrentDirection(), originalDirection); @@ -93,7 +110,7 @@ public class MarsRoverTest { public void loopTheWorldInLongitudeMovingForward() { Coordinates originalCoordinates = rover.getCurrentCoordinates(); - rover.turnLeft(); + turnLeftCommand.execute(); repeatAction(10, () -> moveForwardCommand.execute()); assertEquals(rover.getCurrentCoordinates(), originalCoordinates); @@ -104,8 +121,8 @@ public class MarsRoverTest { public void loopTheWorldInLongitudeMovingBackwards() { Coordinates originalCoordinates = rover.getCurrentCoordinates(); - rover.turnLeft(); - repeatAction(10, () -> rover.moveBackwards()); + turnLeftCommand.execute(); + repeatAction(10, () -> moveBackwardsCommand.execute()); assertEquals(rover.getCurrentCoordinates(), originalCoordinates); assertEquals(rover.getCurrentDirection(), EAST); @@ -117,18 +134,18 @@ public class MarsRoverTest { assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 3)); assertEquals(rover.getCurrentDirection(), SOUTH); - rover.turnLeft(); + turnLeftCommand.execute(); repeatAction(23, () -> moveForwardCommand.execute()); assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 6)); assertEquals(rover.getCurrentDirection(), EAST); - repeatAction(3, () -> rover.turnRight()); + repeatAction(3, () -> turnRightCommand.execute()); repeatAction(9, () -> moveForwardCommand.execute()); assertEquals(rover.getCurrentCoordinates(), Coordinates.of(4, 6)); assertEquals(rover.getCurrentDirection(), NORTH); - rover.moveBackwards(); - rover.turnRight(); + moveBackwardsCommand.execute(); + turnRightCommand.execute(); moveForwardCommand.execute(); assertEquals(rover.getCurrentCoordinates(), Coordinates.of(5, 7)); assertEquals(rover.getCurrentDirection(), EAST); diff --git a/mars-station/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverWithObstaclesTest.java b/mars-station/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverWithObstaclesTest.java index 965b21d..d0c6e3b 100644 --- a/mars-station/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverWithObstaclesTest.java +++ b/mars-station/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverWithObstaclesTest.java @@ -16,6 +16,8 @@ public class MarsRoverWithObstaclesTest { private MarsRover rover; private MoveForwardCommand moveForwardCommand; + private TurnLeftCommand turnLeftCommand; + private TurnRightCommand turnRightCommand; @BeforeMethod public void setUp() { @@ -35,6 +37,8 @@ public class MarsRoverWithObstaclesTest { rover = new MarsRover(marsMap, SOUTH); moveForwardCommand = new MoveForwardCommand(rover); + turnLeftCommand = new TurnLeftCommand(rover); + turnRightCommand = new TurnRightCommand(rover); } @Test @@ -54,10 +58,10 @@ public class MarsRoverWithObstaclesTest { Coordinates oneStepForward = rover.getCurrentCoordinates(); assertNotEquals(oneStepForward, Coordinates.of(3, 3)); - rover.turnLeft(); + turnLeftCommand.execute(); moveForwardCommand.execute(); moveForwardCommand.execute(); - rover.turnRight(); + turnRightCommand.execute(); moveForwardCommand.execute(); moveForwardCommand.execute(); var currentPosition = Coordinates.of(4, 5); diff --git a/mars-station/src/test/java/cat/hack3/codingtests/marsrover/MoveBackwardsCommand.java b/mars-station/src/test/java/cat/hack3/codingtests/marsrover/MoveBackwardsCommand.java new file mode 100644 index 0000000..c4f53e0 --- /dev/null +++ b/mars-station/src/test/java/cat/hack3/codingtests/marsrover/MoveBackwardsCommand.java @@ -0,0 +1,14 @@ +package cat.hack3.codingtests.marsrover; + +public class MoveBackwardsCommand implements MarsRoverCommand { + private final MarsRover marsRover; + + public MoveBackwardsCommand(MarsRover marsRover) { + this.marsRover = marsRover; + } + + @Override + public void execute() { + marsRover.moveTowards(marsRover.getCurrentDirection().reversed()); + } +} diff --git a/mars-station/src/test/java/cat/hack3/codingtests/marsrover/TurnLeftCommand.java b/mars-station/src/test/java/cat/hack3/codingtests/marsrover/TurnLeftCommand.java new file mode 100644 index 0000000..ddfca68 --- /dev/null +++ b/mars-station/src/test/java/cat/hack3/codingtests/marsrover/TurnLeftCommand.java @@ -0,0 +1,14 @@ +package cat.hack3.codingtests.marsrover; + +public class TurnLeftCommand implements MarsRoverCommand{ + private final MarsRover marsRover; + + public TurnLeftCommand(MarsRover marsRover) { + this.marsRover = marsRover; + } + + @Override + public void execute() { + marsRover.rotateTowards(MarsRover.Rotation.LEFT); + } +}