draft of a full command interface implementation
This commit is contained in:
parent
530cf22983
commit
43bd81c8e3
|
@ -0,0 +1,166 @@
|
||||||
|
package cat.hack3.codingtests.marsrover.ui;
|
||||||
|
|
||||||
|
import cat.hack3.codingtests.marsrover.Coordinates;
|
||||||
|
import cat.hack3.codingtests.marsrover.Direction;
|
||||||
|
import cat.hack3.codingtests.marsrover.MarsMap;
|
||||||
|
import cat.hack3.codingtests.marsrover.MarsRover;
|
||||||
|
|
||||||
|
import java.util.InputMismatchException;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class ClientCommandInterface {
|
||||||
|
|
||||||
|
private static MarsRover rover;
|
||||||
|
|
||||||
|
public static final String INTRO = """
|
||||||
|
░▒▓██████████████▓▒░ ░▒▓██████▓▒░░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓████████▓▒░▒▓███████▓▒░
|
||||||
|
░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░
|
||||||
|
░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░
|
||||||
|
░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒▒▓█▓▒░░▒▓██████▓▒░ ░▒▓███████▓▒░
|
||||||
|
░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░
|
||||||
|
░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░
|
||||||
|
░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓███████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░░▒▓██████▓▒░ ░▒▓██▓▒░ ░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░
|
||||||
|
|
||||||
|
Welcome to Mars Rover Command Line Interface!
|
||||||
|
|
||||||
|
Please, establish the Mars dimensions. Bear in mind the Mars-Coordinates-Representation is aligned with the following principles:
|
||||||
|
- 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
|
||||||
|
|
||||||
|
Here a graphical representation of a 3x3 map
|
||||||
|
|
||||||
|
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 static final String READY_MESSAGE = """
|
||||||
|
|
||||||
|
Fantastic!
|
||||||
|
|
||||||
|
A GPS, named MarsMap, has been delivered to Rover, configured with:
|
||||||
|
- The size information of the planet
|
||||||
|
- The current location of the Rover in it
|
||||||
|
|
||||||
|
He now can start moving along the planet based on your commands
|
||||||
|
After every movement, he will check the GPS and report back the new position
|
||||||
|
About rotations, Rover has a build-in accelerometer, which let him know in what direction is pointing out at any moment
|
||||||
|
That's all for now. Have fun!
|
||||||
|
_
|
||||||
|
[ ]
|
||||||
|
( )
|
||||||
|
|>|
|
||||||
|
__/===\\__
|
||||||
|
//| o=o |\\\\
|
||||||
|
<] | o=o | [>
|
||||||
|
\\=====/
|
||||||
|
/ / | \\ \\
|
||||||
|
<_________>
|
||||||
|
|
||||||
|
(to continue, enter your name)
|
||||||
|
""";
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
output(INTRO);
|
||||||
|
|
||||||
|
try (var reader = new Scanner(System.in)) {
|
||||||
|
//autoInitialization();
|
||||||
|
initialization(reader);
|
||||||
|
commandLoop(reader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void autoInitialization() { //to test more quickly
|
||||||
|
deployRover(10, 10, 2, 3, Direction.SOUTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void initialization(Scanner reader) {
|
||||||
|
output("Please, introduce the latitude map size:");
|
||||||
|
int mapHeight = takeIntegerInput(reader);
|
||||||
|
output("Please, introduce the longitude map size:");
|
||||||
|
int mapWidth = takeIntegerInput(reader);
|
||||||
|
|
||||||
|
output("Great! Now please introduce the initial location of the Rover.");
|
||||||
|
output("Latitude:");
|
||||||
|
int latitudeStartingPoint = takeIntegerInput(reader);
|
||||||
|
output("Longitude:");
|
||||||
|
int longitudeStartingPoint = takeIntegerInput(reader);
|
||||||
|
|
||||||
|
Direction startingDirection = retrieveDirection(reader);
|
||||||
|
|
||||||
|
deployRover(mapHeight, mapWidth, latitudeStartingPoint, longitudeStartingPoint, startingDirection);
|
||||||
|
|
||||||
|
output(READY_MESSAGE);
|
||||||
|
reader.next(); //input ignored
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int takeIntegerInput(Scanner reader) {
|
||||||
|
try {
|
||||||
|
return reader.nextInt();
|
||||||
|
} catch (InputMismatchException e) {
|
||||||
|
output("(please, introduce a number)");
|
||||||
|
reader.next();
|
||||||
|
return takeIntegerInput(reader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Direction retrieveDirection(Scanner reader) {
|
||||||
|
var directionInput = "none";
|
||||||
|
Direction resolvedDirection;
|
||||||
|
do {
|
||||||
|
output("Insert initial rover direction (n = north, e = east, w = west, s = south, q = exit):");
|
||||||
|
directionInput = reader.next();
|
||||||
|
resolvedDirection = switch (directionInput) {
|
||||||
|
case "n" -> Direction.NORTH;
|
||||||
|
case "e" -> Direction.EAST;
|
||||||
|
case "w" -> Direction.WEST;
|
||||||
|
case "s" -> Direction.SOUTH;
|
||||||
|
default -> null;
|
||||||
|
};
|
||||||
|
} while (isNotResolvedDirection(resolvedDirection) && isNotExitSignal(directionInput));
|
||||||
|
|
||||||
|
if (isExitSignal(directionInput))
|
||||||
|
System.exit(0);
|
||||||
|
|
||||||
|
return resolvedDirection;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void deployRover(int mapHeight, int mapWidth, int latitudeStartingPoint, int longitudeStartingPoint, Direction startingDirection) {
|
||||||
|
var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint);
|
||||||
|
var marsMap = new MarsMap(mapHeight, mapWidth, startingCoordinates);
|
||||||
|
rover = new MarsRover(marsMap, startingDirection);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void commandLoop(Scanner reader) {
|
||||||
|
var nextCommand = "none";
|
||||||
|
do {
|
||||||
|
output("Insert command (f = forward, b = backward, l = turn left, r = turn right, q = quit):");
|
||||||
|
nextCommand = reader.next();
|
||||||
|
switch (nextCommand) {
|
||||||
|
case "f" -> rover.moveForward();
|
||||||
|
case "b" -> rover.moveBackwards();
|
||||||
|
case "l" -> rover.turnLeft();
|
||||||
|
case "r" -> rover.turnRight();
|
||||||
|
case "q" -> output("Disconnecting from Mars...");
|
||||||
|
default -> output("unknown command");
|
||||||
|
}
|
||||||
|
} while (isNotExitSignal(nextCommand));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isNotResolvedDirection(Direction resolvedDirection) {
|
||||||
|
return resolvedDirection == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isExitSignal(String nextCommand) {
|
||||||
|
return "q".equals(nextCommand);
|
||||||
|
}
|
||||||
|
private static boolean isNotExitSignal(String nextCommand) {
|
||||||
|
return !isExitSignal(nextCommand);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void output(String message) {
|
||||||
|
System.out.println(message);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue