1
0
Fork 0

Merge branch 'JPMS-experimentation'

# Conflicts:
#	SOLUTION.md
#	misc/images/mars-rover_design.png
This commit is contained in:
Xavier Fontanet 2024-06-24 17:28:23 +02:00
commit 8595c1c83d
43 changed files with 511 additions and 262 deletions

View File

@ -1,4 +1,7 @@
# MY SOLUTION
## Current design
## Current design with JPMS
![Current design](misc/images/mars-rover_design.png)
![Current design](misc/images/mars-rover_design_JPMS.png)
## Previous design
![Previous design](misc/images/mars-rover_design.png)

View File

@ -11,6 +11,13 @@
<artifactId>mars-station</artifactId>
<version>2.0</version>
<dependencies>
<dependency>
<groupId>cat.hack3.codingtests</groupId>
<artifactId>rover-api</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>17</maven.compiler.source>

View File

@ -1,16 +1,24 @@
package cat.hack3.codingtests.marsrover;
import cat.hack3.codingtests.marsrover.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.cartography.Direction;
import cat.hack3.codingtests.marsrover.cartography.MarsMap;
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
public class MarsRover implements RotableRiderRover{
public class MarsRover implements RotableRiderRover {
private final MarsMap marsMap;
private final PlanetMap marsMap;
private final RoverActionReporter roverActionReporter;
private Direction currentDirection;
public MarsRover(MarsMap marsMap, Direction startingDirection) {
public static class Provider implements RotableRiderRover.Provider {
@Override
public RotableRiderRover provideWith(PlanetMap map, Direction startingDirection) {
return new MarsRover(map, startingDirection);
}
}
private MarsRover(PlanetMap marsMap, Direction startingDirection) {
this.marsMap = marsMap;
currentDirection = startingDirection;
roverActionReporter = new RoverActionReporter();

View File

@ -1,16 +0,0 @@
package cat.hack3.codingtests.marsrover;
import cat.hack3.codingtests.marsrover.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.cartography.Direction;
public interface RotableRiderRover {
enum Rotation {LEFT, RIGHT}
void moveTowards(Direction direction);
void rotateTowards(Rotation rotation);
Coordinates getCurrentCoordinates();
Direction getCurrentDirection();
}

View File

@ -1,8 +1,8 @@
package cat.hack3.codingtests.marsrover;
import cat.hack3.codingtests.marsrover.RotableRiderRover.Rotation;
import cat.hack3.codingtests.marsrover.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.cartography.Direction;
import cat.hack3.codingtests.marsrover.api.RotableRiderRover.Rotation;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
import java.util.logging.Logger;

View File

@ -1,7 +1,7 @@
package cat.hack3.codingtests.marsrover.cartography;
package cat.hack3.codingtests.marsrover.gps;
import cat.hack3.codingtests.marsrover.cartography.Coordinates.Latitude;
import cat.hack3.codingtests.marsrover.cartography.Coordinates.Longitude;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates.Latitude;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates.Longitude;
public class MapIncrementalPositionResolver {

View File

@ -1,7 +1,10 @@
package cat.hack3.codingtests.marsrover.cartography;
package cat.hack3.codingtests.marsrover.gps;
import cat.hack3.codingtests.marsrover.cartography.Coordinates.Latitude;
import cat.hack3.codingtests.marsrover.cartography.Coordinates.Longitude;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates.Latitude;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates.Longitude;
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
import java.util.List;
@ -14,7 +17,7 @@ import java.util.List;
* 2 2-1 2-2 2-3
* 3 3-1 3-2 3-3
*/
public class MarsMap {
public class MarsMap implements PlanetMap {
private static final int FIRST_POSITION_IN_MAP = 1;
private static final int INCREMENT_UNIT = 1;
@ -24,13 +27,21 @@ public class MarsMap {
private final MapIncrementalPositionResolver positionResolver;
public static class Provider implements PlanetMap.Provider {
@Override
public PlanetMap provideWith(int height, int width, Coordinates startingCoordinates, List<Coordinates> obstaclesLocalizations) {
return new MarsMap(height, width, startingCoordinates, obstaclesLocalizations);
}
}
@SafeVarargs
public MarsMap(int height, int width, Coordinates startingCoordinates, List<Coordinates>... obstaclesLocalizations) {
private MarsMap(int height, int width, Coordinates startingCoordinates, List<Coordinates>... obstaclesLocalizations) {
currentPosition = startingCoordinates;
this.obstaclesLocalizations = obstaclesLocalizations.length > 0 ? obstaclesLocalizations[0] : List.of();
positionResolver = new MapIncrementalPositionResolver(FIRST_POSITION_IN_MAP, INCREMENT_UNIT, height, width);
}
@Override
public Coordinates getNextPositionTowards(Direction direction) {
return switch (direction) {
case NORTH -> getDecrementLatitude();
@ -64,14 +75,17 @@ public class MarsMap {
return currentPosition.withUpdated(Longitude.of(newLongitude));
}
@Override
public synchronized void updateLocation(Coordinates newPosition) {
currentPosition = newPosition;
}
@Override
public boolean willCollideWithObstacle(Coordinates newCoordinates) {
return obstaclesLocalizations.contains(newCoordinates);
}
@Override
public Coordinates getCurrentPosition() {
return currentPosition;
}

View File

@ -0,0 +1,15 @@
import cat.hack3.codingtests.marsrover.MarsRover;
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
import cat.hack3.codingtests.marsrover.gps.MarsMap;
module mars.station {
requires rover.api;
requires java.logging;
provides PlanetMap.Provider
with MarsMap.Provider;
provides RotableRiderRover.Provider
with MarsRover.Provider;
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 156 KiB

View File

@ -13,6 +13,8 @@
<module>mars-station</module>
<module>user-interface-console</module>
<module>rover-commands</module>
<module>rover-api</module>
<module>tests-suite</module>
</modules>
<properties>

20
rover-api/pom.xml Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mars-rover</artifactId>
<groupId>cat.hack3.codingtests</groupId>
<version>10.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>rover-api</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,21 @@
package cat.hack3.codingtests.marsrover.api;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
public interface RotableRiderRover {
enum Rotation {LEFT, RIGHT}
void moveTowards(Direction direction);
void rotateTowards(Rotation rotation);
Coordinates getCurrentCoordinates();
Direction getCurrentDirection();
interface Provider {
RotableRiderRover provideWith(PlanetMap map, Direction startingDirection);
}
}

View File

@ -1,4 +1,4 @@
package cat.hack3.codingtests.marsrover.cartography;
package cat.hack3.codingtests.marsrover.api.cartography;
public record Coordinates(Latitude latitude, Longitude longitude) {
@ -14,8 +14,8 @@ public record Coordinates(Latitude latitude, Longitude longitude) {
return Coordinates.of(latitude.pointer, newLongitude.pointer);
}
record Latitude(long pointer) {
static Latitude of(long pointer) {
public record Latitude(long pointer) {
public static Latitude of(long pointer) {
return new Latitude(pointer);
}
@Override
@ -23,8 +23,8 @@ public record Coordinates(Latitude latitude, Longitude longitude) {
return String.valueOf(pointer);
}
}
record Longitude(long pointer) {
static Longitude of(long pointer) {
public record Longitude(long pointer) {
public static Longitude of(long pointer) {
return new Longitude(pointer);
}
@Override

View File

@ -1,4 +1,4 @@
package cat.hack3.codingtests.marsrover.cartography;
package cat.hack3.codingtests.marsrover.api.cartography;
public enum Direction {
NORTH, SOUTH, EAST, WEST;

View File

@ -0,0 +1,18 @@
package cat.hack3.codingtests.marsrover.api.cartography;
import java.util.List;
public interface PlanetMap {
Coordinates getNextPositionTowards(Direction direction);
void updateLocation(Coordinates newPosition);
boolean willCollideWithObstacle(Coordinates newCoordinates);
Coordinates getCurrentPosition();
interface Provider {
PlanetMap provideWith(int height, int width, Coordinates startingCoordinates, List<Coordinates> obstaclesLocalizations);
}
}

View File

@ -1,4 +1,4 @@
package cat.hack3.codingtests.marsrover;
package cat.hack3.codingtests.marsrover.api.commands;
public interface RoverCommand {
enum Type {MOVE_FORWARD, MOVE_BACKWARDS, TURN_LEFT, TURN_RIGHT}

View File

@ -0,0 +1,11 @@
package cat.hack3.codingtests.marsrover.api.commands;
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
public interface RoverCommandFactory {
RoverCommand create(RoverCommand.Type type);
interface Provider {
RoverCommandFactory provideWith(RotableRiderRover rover);
}
}

View File

@ -0,0 +1,5 @@
module rover.api {
exports cat.hack3.codingtests.marsrover.api;
exports cat.hack3.codingtests.marsrover.api.cartography;
exports cat.hack3.codingtests.marsrover.api.commands;
}

View File

@ -12,17 +12,17 @@
<artifactId>rover-commands</artifactId>
<version>2.0</version>
<dependencies>
<dependency>
<groupId>cat.hack3.codingtests</groupId>
<artifactId>rover-api</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>cat.hack3.codingtests</groupId>
<artifactId>mars-station</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
</project>

View File

@ -1,18 +0,0 @@
package cat.hack3.codingtests.marsrover;
public class CommandFactory {
private final RotableRiderRover rover;
public CommandFactory(RotableRiderRover rover) {
this.rover = rover;
}
public RoverCommand createCommand(RoverCommand.Type type) {
return switch (type) {
case MOVE_FORWARD -> new MoveForwardCommand(rover);
case MOVE_BACKWARDS -> new MoveBackwardsCommand(rover);
case TURN_LEFT -> new TurnLeftCommand(rover);
case TURN_RIGHT -> new TurnRightCommand(rover);
};
}
}

View File

@ -1,13 +0,0 @@
package cat.hack3.codingtests.marsrover;
class MoveForwardCommand implements RoverCommand {
private final RotableRiderRover rover;
public MoveForwardCommand(RotableRiderRover rover) {
this.rover = rover;
}
public void execute() {
rover.moveTowards(rover.getCurrentDirection());
}
}

View File

@ -1,16 +0,0 @@
package cat.hack3.codingtests.marsrover;
import static cat.hack3.codingtests.marsrover.RotableRiderRover.Rotation.LEFT;
class TurnLeftCommand implements RoverCommand {
private final RotableRiderRover rover;
public TurnLeftCommand(RotableRiderRover rover) {
this.rover = rover;
}
@Override
public void execute() {
rover.rotateTowards(LEFT);
}
}

View File

@ -1,16 +0,0 @@
package cat.hack3.codingtests.marsrover;
import static cat.hack3.codingtests.marsrover.RotableRiderRover.Rotation.RIGHT;
class TurnRightCommand implements RoverCommand {
private final RotableRiderRover rover;
public TurnRightCommand(RotableRiderRover rover) {
this.rover = rover;
}
@Override
public void execute() {
rover.rotateTowards(RIGHT);
}
}

View File

@ -1,6 +1,10 @@
package cat.hack3.codingtests.marsrover;
package cat.hack3.codingtests.marsrover.commands;
class MoveBackwardsCommand implements RoverCommand {
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
public class MoveBackwardsCommand implements RoverCommand {
private final RotableRiderRover rover;
public MoveBackwardsCommand(RotableRiderRover rover) {

View File

@ -0,0 +1,17 @@
package cat.hack3.codingtests.marsrover.commands;
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
public class MoveForwardCommand implements RoverCommand {
private final RotableRiderRover rover;
public MoveForwardCommand(RotableRiderRover rover) {
this.rover = rover;
}
public void execute() {
rover.moveTowards(rover.getCurrentDirection());
}
}

View File

@ -0,0 +1,31 @@
package cat.hack3.codingtests.marsrover.commands;
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommandFactory;
public class RoverCommandFactoryImpl implements RoverCommandFactory {
private final RotableRiderRover rover;
public static class Provider implements RoverCommandFactory.Provider {
@Override
public RoverCommandFactory provideWith(RotableRiderRover rover) {
return new RoverCommandFactoryImpl(rover);
}
}
private RoverCommandFactoryImpl(RotableRiderRover rover) {
this.rover = rover;
}
@Override
public RoverCommand create(RoverCommand.Type type) {
return switch (type) {
case MOVE_FORWARD -> new MoveForwardCommand(rover);
case MOVE_BACKWARDS -> new MoveBackwardsCommand(rover);
case TURN_LEFT -> new TurnLeftCommand(rover);
case TURN_RIGHT -> new TurnRightCommand(rover);
};
}
}

View File

@ -0,0 +1,20 @@
package cat.hack3.codingtests.marsrover.commands;
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
import static cat.hack3.codingtests.marsrover.api.RotableRiderRover.Rotation.LEFT;
public class TurnLeftCommand implements RoverCommand {
private final RotableRiderRover rover;
public TurnLeftCommand(RotableRiderRover rover) {
this.rover = rover;
}
@Override
public void execute() {
rover.rotateTowards(LEFT);
}
}

View File

@ -0,0 +1,20 @@
package cat.hack3.codingtests.marsrover.commands;
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
import static cat.hack3.codingtests.marsrover.api.RotableRiderRover.Rotation.RIGHT;
public class TurnRightCommand implements RoverCommand {
private final RotableRiderRover rover;
public TurnRightCommand(RotableRiderRover rover) {
this.rover = rover;
}
@Override
public void execute() {
rover.rotateTowards(RIGHT);
}
}

View File

@ -0,0 +1,9 @@
import cat.hack3.codingtests.marsrover.api.commands.RoverCommandFactory;
import cat.hack3.codingtests.marsrover.commands.RoverCommandFactoryImpl;
module rover.commands {
requires rover.api;
provides RoverCommandFactory.Provider
with RoverCommandFactoryImpl.Provider;
}

View File

@ -1,77 +0,0 @@
package cat.hack3.codingtests.marsrover;
import cat.hack3.codingtests.marsrover.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.cartography.Direction;
import cat.hack3.codingtests.marsrover.cartography.MarsMap;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.List;
import static cat.hack3.codingtests.marsrover.RoverCommand.Type.*;
import static cat.hack3.codingtests.marsrover.cartography.Direction.SOUTH;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
public class MarsRoverWithObstaclesTest {
private MarsRover rover;
private RoverCommand moveForwardCommand;
private RoverCommand turnLeftCommand;
private RoverCommand turnRightCommand;
@BeforeMethod
public void setUp() {
int mapWidth = 10;
int mapHeight = 10;
int latitudeStartingPoint = 2;
int longitudeStartingPoint = 3;
var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint);
List<Coordinates> obstaclesLocalizations = List.of(
Coordinates.of(3, 3),
Coordinates.of(5, 5),
Coordinates.of(7, 7),
Coordinates.of(9, 9),
Coordinates.of(10, 10)
);
var marsMap = new MarsMap(mapHeight, mapWidth, startingCoordinates, obstaclesLocalizations);
rover = new MarsRover(marsMap, SOUTH);
var commandFactory = new CommandFactory(rover);
moveForwardCommand = commandFactory.createCommand(MOVE_FORWARD);
turnLeftCommand = commandFactory.createCommand(TURN_LEFT);
turnRightCommand = commandFactory.createCommand(TURN_RIGHT);
}
@Test
public void findAnObstacleAtFirstMove() {
Coordinates originalCoordinates = rover.getCurrentCoordinates();
Direction originalDirection = rover.getCurrentDirection();
moveForwardCommand.execute();
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
assertEquals(rover.getCurrentDirection(), originalDirection);
}
@Test
public void afterFindingObstacleWalkAroundUntilFindingNext() {
moveForwardCommand.execute();
Coordinates oneStepForward = rover.getCurrentCoordinates();
assertNotEquals(oneStepForward, Coordinates.of(3, 3));
turnLeftCommand.execute();
moveForwardCommand.execute();
moveForwardCommand.execute();
turnRightCommand.execute();
moveForwardCommand.execute();
moveForwardCommand.execute();
var currentPosition = Coordinates.of(4, 5);
assertEquals(rover.getCurrentCoordinates(), currentPosition);
moveForwardCommand.execute();
var positionAfterMove = Coordinates.of(5, 4);
assertNotEquals(rover.getCurrentCoordinates(), positionAfterMove);
}
}

39
tests-suite/pom.xml Normal file
View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mars-rover</artifactId>
<groupId>cat.hack3.codingtests</groupId>
<version>10.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>tests-suite</artifactId>
<dependencies>
<dependency>
<groupId>cat.hack3.codingtests</groupId>
<artifactId>rover-api</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cat.hack3.codingtests</groupId>
<artifactId>rover-commands</artifactId>
<version>2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cat.hack3.codingtests</groupId>
<artifactId>mars-station</artifactId>
<version>2.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,64 @@
package cat.hack3.codingtests.marsrover.test;
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommandFactory;
import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.IntStream;
import static cat.hack3.codingtests.marsrover.api.cartography.Direction.SOUTH;
import static cat.hack3.codingtests.marsrover.api.commands.RoverCommand.Type.*;
public class CommonTests {
public static final Direction STARTING_DIRECTION = SOUTH;
protected RotableRiderRover rover;
protected RoverCommand moveForwardCommand;
protected RoverCommand moveBackwardsCommand;
protected RoverCommand turnLeftCommand;
protected RoverCommand turnRightCommand;
protected void setup() {
int mapWidth = 10;
int mapHeight = 10;
int latitudeStartingPoint = 2;
int longitudeStartingPoint = 3;
var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint);
var marsMap = getImplProviderOf(PlanetMap.Provider.class)
.provideWith(mapHeight, mapWidth, startingCoordinates, getObstacles());
rover = getImplProviderOf(RotableRiderRover.Provider.class)
.provideWith(marsMap, CommonTests.STARTING_DIRECTION);
var commandFactory = getImplProviderOf(RoverCommandFactory.Provider.class)
.provideWith(rover);
moveForwardCommand = commandFactory.create(MOVE_FORWARD);
moveBackwardsCommand = commandFactory.create(MOVE_BACKWARDS);
turnLeftCommand = commandFactory.create(TURN_LEFT);
turnRightCommand = commandFactory.create(TURN_RIGHT);
}
private <T> T getImplProviderOf(Class<T> interfaceProvider) {
return ServiceLoader
.load(interfaceProvider)
.findFirst()
.orElseThrow();
}
protected List<Coordinates> getObstacles() {
return List.of();
}
protected void repeatAction(int times, Runnable actionToRepeat) {
IntStream.rangeClosed(1, times)
.forEach(i -> actionToRepeat.run());
}
}

View File

@ -1,41 +1,18 @@
package cat.hack3.codingtests.marsrover;
package cat.hack3.codingtests.marsrover.test;
import cat.hack3.codingtests.marsrover.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.cartography.Direction;
import cat.hack3.codingtests.marsrover.cartography.MarsMap;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.stream.IntStream;
import static cat.hack3.codingtests.marsrover.RoverCommand.Type.*;
import static cat.hack3.codingtests.marsrover.cartography.Direction.*;
import static cat.hack3.codingtests.marsrover.api.cartography.Direction.*;
import static org.testng.Assert.assertEquals;
public class MarsRoverTest {
private MarsRover rover;
private RoverCommand moveForwardCommand;
private RoverCommand moveBackwardsCommand;
private RoverCommand turnLeftCommand;
private RoverCommand turnRightCommand;
public class MarsRoverTest extends CommonTests {
@BeforeMethod
public void setUp() {
int mapWidth = 10;
int mapHeight = 10;
int latitudeStartingPoint = 2;
int longitudeStartingPoint = 3;
var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint);
var marsMap = new MarsMap(mapHeight, mapWidth, startingCoordinates);
rover = new MarsRover(marsMap, SOUTH);
var commandFactory = new CommandFactory(rover);
moveForwardCommand = commandFactory.createCommand(MOVE_FORWARD);
moveBackwardsCommand = commandFactory.createCommand(MOVE_BACKWARDS);
turnLeftCommand = commandFactory.createCommand(TURN_LEFT);
turnRightCommand = commandFactory.createCommand(TURN_RIGHT);
setup();
}
@Test
@ -154,8 +131,4 @@ public class MarsRoverTest {
assertEquals(rover.getCurrentDirection(), EAST);
}
private void repeatAction(int times, Runnable actionToRepeat) {
IntStream.rangeClosed(1, times)
.forEach(i -> actionToRepeat.run());
}
}

View File

@ -0,0 +1,55 @@
package cat.hack3.codingtests.marsrover.test;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.List;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
public class MarsRoverWithObstaclesTest extends CommonTests {
@BeforeMethod
public void setUp() {
super.setup();
}
@Test
public void findAnObstacleAtFirstMove() {
Coordinates originalCoordinates = rover.getCurrentCoordinates();
Direction originalDirection = rover.getCurrentDirection();
moveForwardCommand.execute();
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
assertEquals(rover.getCurrentDirection(), originalDirection);
}
@Test
public void afterFindingObstacleWalkAroundUntilFindingNext() {
moveForwardCommand.execute();
Coordinates oneStepForward = rover.getCurrentCoordinates();
assertNotEquals(oneStepForward, Coordinates.of(3, 3));
assertEquals(oneStepForward, Coordinates.of(2, 3));
turnLeftCommand.execute();
repeatAction(2, () -> moveForwardCommand.execute());
turnRightCommand.execute();
repeatAction(2, () -> moveForwardCommand.execute());
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(4, 5));
moveForwardCommand.execute();
assertNotEquals(rover.getCurrentCoordinates(), Coordinates.of(5, 5));
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(4, 5));
}
@Override
protected List<Coordinates> getObstacles() {
return List.of(
Coordinates.of(3, 3),
Coordinates.of(5, 5)
);
}
}

View File

@ -0,0 +1,17 @@
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommandFactory;
module tests.suite {
requires rover.api;
requires mars.station;
requires rover.commands;
uses PlanetMap.Provider;
uses RotableRiderRover.Provider;
uses RoverCommandFactory.Provider;
requires org.testng;
exports cat.hack3.codingtests.marsrover.test;
}

View File

@ -10,23 +10,31 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>user-interface-console</artifactId>
<dependencies>
<dependency>
<groupId>cat.hack3.codingtests</groupId>
<artifactId>rover-api</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cat.hack3.codingtests</groupId>
<artifactId>mars-station</artifactId>
<version>2.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cat.hack3.codingtests</groupId>
<artifactId>rover-commands</artifactId>
<version>2.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>cat.hack3.codingtests</groupId>
<artifactId>mars-station</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>cat.hack3.codingtests</groupId>
<artifactId>rover-commands</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
</project>

View File

@ -1,6 +1,6 @@
package cat.hack3.codingtests.marsrover.ui.console;
import cat.hack3.codingtests.marsrover.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import java.util.Scanner;
@ -26,8 +26,8 @@ public class ClientCommandInterface {
private void start() {
output(PresentationMessage.INTRO);
//RotableRiderRover rover = roverInitializer.autoInitialize();
RotableRiderRover rover = roverInitializer.initializeFromUserInputs();
RotableRiderRover rover = roverInitializer.autoInitialize();
//RotableRiderRover rover = roverInitializer.initializeFromUserInputs();
var commandsPerformer = new RoverCommandsPerformer(reader, rover);
commandsPerformer.acceptCommandsUntilExitSignal();

View File

@ -1,7 +1,7 @@
package cat.hack3.codingtests.marsrover.ui.console;
import cat.hack3.codingtests.marsrover.cartography.Direction;
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
import java.util.Scanner;

View File

@ -1,19 +1,18 @@
package cat.hack3.codingtests.marsrover.ui.console;
import cat.hack3.codingtests.marsrover.CommandFactory;
import cat.hack3.codingtests.marsrover.RotableRiderRover;
import cat.hack3.codingtests.marsrover.RoverCommand;
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommandFactory;
import java.util.Scanner;
import static cat.hack3.codingtests.marsrover.RoverCommand.Type.*;
import static cat.hack3.codingtests.marsrover.api.commands.RoverCommand.Type.*;
import static cat.hack3.codingtests.marsrover.ui.console.UICommons.isNotExitSignal;
import static cat.hack3.codingtests.marsrover.ui.console.UICommons.output;
public class RoverCommandsPerformer {
private final Scanner reader;
CommandFactory commandFactory;
private final RoverCommand moveForwardCommand;
private final RoverCommand moveBackwardsCommand;
private final RoverCommand turnLeftCommand;
@ -21,11 +20,13 @@ public class RoverCommandsPerformer {
public RoverCommandsPerformer(Scanner reader, RotableRiderRover rover) {
this.reader = reader;
commandFactory = new CommandFactory(rover);
moveForwardCommand = commandFactory.createCommand(MOVE_FORWARD);
moveBackwardsCommand = commandFactory.createCommand(MOVE_BACKWARDS);
turnLeftCommand = commandFactory.createCommand(TURN_LEFT);
turnRightCommand = commandFactory.createCommand(TURN_RIGHT);
var roverCommandFactory = UICommons
.getImplProviderOf(RoverCommandFactory.Provider.class)
.provideWith(rover);
moveForwardCommand = roverCommandFactory.create(MOVE_FORWARD);
moveBackwardsCommand = roverCommandFactory.create(MOVE_BACKWARDS);
turnLeftCommand = roverCommandFactory.create(TURN_LEFT);
turnRightCommand = roverCommandFactory.create(TURN_RIGHT);
}
public void acceptCommandsUntilExitSignal() {

View File

@ -1,10 +1,9 @@
package cat.hack3.codingtests.marsrover.ui.console;
import cat.hack3.codingtests.marsrover.MarsRover;
import cat.hack3.codingtests.marsrover.RotableRiderRover;
import cat.hack3.codingtests.marsrover.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.cartography.Direction;
import cat.hack3.codingtests.marsrover.cartography.MarsMap;
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
import java.util.InputMismatchException;
import java.util.List;
@ -36,7 +35,7 @@ public class RoverInitializer {
Direction startingDirection = directionRetriever.retrieveDirection();
RotableRiderRover rover = deployRover(mapHeight, mapWidth, latitudeStartingPoint, longitudeStartingPoint, startingDirection);
RotableRiderRover rover = deployRover(mapHeight, mapWidth, latitudeStartingPoint, longitudeStartingPoint, startingDirection, List.of());
output(PresentationMessage.READY_MESSAGE);
reader.next(); //input ignored
@ -65,10 +64,12 @@ public class RoverInitializer {
return deployRover(10, 10, 2, 3, Direction.SOUTH, obstacles);
}
@SafeVarargs
private RotableRiderRover deployRover(int mapHeight, int mapWidth, int latitudeStartingPoint, int longitudeStartingPoint, Direction startingDirection, List<Coordinates>... obstaclesLocalizations) {
private RotableRiderRover deployRover(int mapHeight, int mapWidth, int latitudeStartingPoint, int longitudeStartingPoint, Direction startingDirection, List<Coordinates> obstaclesLocalizations) {
var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint);
var marsMap = new MarsMap(mapHeight, mapWidth, startingCoordinates, obstaclesLocalizations);
return new MarsRover(marsMap, startingDirection);
var mapProvider = UICommons.getImplProviderOf(PlanetMap.Provider.class);
var roverProvider = UICommons.getImplProviderOf(RotableRiderRover.Provider.class);
var marsMap = mapProvider.provideWith(mapHeight, mapWidth, startingCoordinates, obstaclesLocalizations);
return roverProvider.provideWith(marsMap, startingDirection);
}
}

View File

@ -1,5 +1,7 @@
package cat.hack3.codingtests.marsrover.ui.console;
import java.util.ServiceLoader;
public class UICommons {
public static final String QUIT_COMMAND_TEXT = "q";
@ -15,4 +17,11 @@ public class UICommons {
static void output(String message) {
System.out.println(message);
}
static <T> T getImplProviderOf(Class<T> interfaceProvider) {
return ServiceLoader
.load(interfaceProvider)
.findFirst()
.orElseThrow();
}
}

View File

@ -0,0 +1,13 @@
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommandFactory;
module ui.console {
requires rover.api;
requires mars.station;
requires rover.commands;
uses PlanetMap.Provider;
uses RotableRiderRover.Provider;
uses RoverCommandFactory.Provider;
}