adapt UI to new commands design
This commit is contained in:
parent
535ae91e6c
commit
f0f3bf31fc
|
@ -1,6 +1,6 @@
|
||||||
package cat.hack3.codingtests.marsrover.ui.console;
|
package cat.hack3.codingtests.marsrover.ui.console;
|
||||||
|
|
||||||
import cat.hack3.codingtests.marsrover.MarsRover;
|
import cat.hack3.codingtests.marsrover.RotableRiderRover;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
@ -13,8 +13,8 @@ public class ClientCommandInterface {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try (var userInputSource = new Scanner(System.in)) {
|
try (var userInputSource = new Scanner(System.in)) {
|
||||||
ClientCommandInterface userInterface = new ClientCommandInterface(userInputSource);
|
new ClientCommandInterface(userInputSource)
|
||||||
userInterface.start();
|
.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,8 +26,8 @@ public class ClientCommandInterface {
|
||||||
private void start() {
|
private void start() {
|
||||||
output(PresentationMessage.INTRO);
|
output(PresentationMessage.INTRO);
|
||||||
|
|
||||||
MarsRover rover = roverInitializer.autoInitialize();
|
//RotableRiderRover rover = roverInitializer.autoInitialize();
|
||||||
//MarsRover rover = roverInitializer.initializeFromUserInputs();
|
RotableRiderRover rover = roverInitializer.initializeFromUserInputs();
|
||||||
|
|
||||||
var commandsPerformer = new RoverCommandsPerformer(reader, rover);
|
var commandsPerformer = new RoverCommandsPerformer(reader, rover);
|
||||||
commandsPerformer.acceptCommandsUntilExitSignal();
|
commandsPerformer.acceptCommandsUntilExitSignal();
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package cat.hack3.codingtests.marsrover.ui.console;
|
package cat.hack3.codingtests.marsrover.ui.console;
|
||||||
|
|
||||||
import cat.hack3.codingtests.marsrover.MarsRover;
|
import cat.hack3.codingtests.marsrover.*;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
@ -9,11 +9,19 @@ import static cat.hack3.codingtests.marsrover.ui.console.UICommons.output;
|
||||||
|
|
||||||
public class RoverCommandsPerformer {
|
public class RoverCommandsPerformer {
|
||||||
private final Scanner reader;
|
private final Scanner reader;
|
||||||
private final MarsRover rover;
|
private final RotableRiderRover rover;
|
||||||
|
private final MarsRoverCommand moveForwardCommand;
|
||||||
|
private final MarsRoverCommand moveBackwardsCommand;
|
||||||
|
private final MarsRoverCommand turnLeftCommand;
|
||||||
|
private final MarsRoverCommand turnRightCommand;
|
||||||
|
|
||||||
public RoverCommandsPerformer(Scanner reader, MarsRover rover) {
|
public RoverCommandsPerformer(Scanner reader, RotableRiderRover rover) {
|
||||||
this.reader = reader;
|
this.reader = reader;
|
||||||
this.rover = rover;
|
this.rover = rover;
|
||||||
|
moveForwardCommand = new MoveForwardCommand(rover);
|
||||||
|
moveBackwardsCommand = new MoveBackwardsCommand(rover);
|
||||||
|
turnLeftCommand = new TurnLeftCommand(rover);
|
||||||
|
turnRightCommand = new TurnRightCommand(rover);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void acceptCommandsUntilExitSignal() {
|
public void acceptCommandsUntilExitSignal() {
|
||||||
|
@ -22,10 +30,10 @@ public class RoverCommandsPerformer {
|
||||||
output("Insert command (f = forward, b = backward, l = turn left, r = turn right, q = quit):");
|
output("Insert command (f = forward, b = backward, l = turn left, r = turn right, q = quit):");
|
||||||
nextCommand = reader.next();
|
nextCommand = reader.next();
|
||||||
switch (nextCommand) {
|
switch (nextCommand) {
|
||||||
case "f" -> rover.moveForward();
|
case "f" -> moveForwardCommand.execute();
|
||||||
case "b" -> rover.moveBackwards();
|
case "b" -> moveBackwardsCommand.execute();
|
||||||
case "l" -> rover.turnLeft();
|
case "l" -> turnLeftCommand.execute();
|
||||||
case "r" -> rover.turnRight();
|
case "r" -> turnRightCommand.execute();
|
||||||
case "q" -> output("Disconnecting from Mars...");
|
case "q" -> output("Disconnecting from Mars...");
|
||||||
default -> output("unknown command");
|
default -> output("unknown command");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package cat.hack3.codingtests.marsrover.ui.console;
|
package cat.hack3.codingtests.marsrover.ui.console;
|
||||||
|
|
||||||
import cat.hack3.codingtests.marsrover.MarsRover;
|
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.Coordinates;
|
||||||
import cat.hack3.codingtests.marsrover.cartography.Direction;
|
import cat.hack3.codingtests.marsrover.cartography.Direction;
|
||||||
import cat.hack3.codingtests.marsrover.cartography.MarsMap;
|
import cat.hack3.codingtests.marsrover.cartography.MarsMap;
|
||||||
|
@ -21,7 +22,7 @@ public class RoverInitializer {
|
||||||
directionRetriever = new DirectionRetriever(reader);
|
directionRetriever = new DirectionRetriever(reader);
|
||||||
}
|
}
|
||||||
|
|
||||||
MarsRover initializeFromUserInputs() {
|
RotableRiderRover initializeFromUserInputs() {
|
||||||
output("Please, introduce the latitude map size:");
|
output("Please, introduce the latitude map size:");
|
||||||
int mapHeight = takeIntegerInput();
|
int mapHeight = takeIntegerInput();
|
||||||
output("Please, introduce the longitude map size:");
|
output("Please, introduce the longitude map size:");
|
||||||
|
@ -35,7 +36,7 @@ public class RoverInitializer {
|
||||||
|
|
||||||
Direction startingDirection = directionRetriever.retrieveDirection();
|
Direction startingDirection = directionRetriever.retrieveDirection();
|
||||||
|
|
||||||
MarsRover rover = deployRover(mapHeight, mapWidth, latitudeStartingPoint, longitudeStartingPoint, startingDirection);
|
RotableRiderRover rover = deployRover(mapHeight, mapWidth, latitudeStartingPoint, longitudeStartingPoint, startingDirection);
|
||||||
|
|
||||||
output(PresentationMessage.READY_MESSAGE);
|
output(PresentationMessage.READY_MESSAGE);
|
||||||
reader.next(); //input ignored
|
reader.next(); //input ignored
|
||||||
|
@ -53,7 +54,7 @@ public class RoverInitializer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MarsRover autoInitialize() { //to test more quickly
|
RotableRiderRover autoInitialize() { //to test more quickly
|
||||||
List<Coordinates> obstacles = List.of(
|
List<Coordinates> obstacles = List.of(
|
||||||
Coordinates.of(3, 3),
|
Coordinates.of(3, 3),
|
||||||
Coordinates.of(5, 5),
|
Coordinates.of(5, 5),
|
||||||
|
@ -65,7 +66,7 @@ public class RoverInitializer {
|
||||||
}
|
}
|
||||||
|
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
private MarsRover 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 startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint);
|
||||||
var marsMap = new MarsMap(mapHeight, mapWidth, startingCoordinates, obstaclesLocalizations);
|
var marsMap = new MarsMap(mapHeight, mapWidth, startingCoordinates, obstaclesLocalizations);
|
||||||
return new MarsRover(marsMap, startingDirection);
|
return new MarsRover(marsMap, startingDirection);
|
||||||
|
|
Loading…
Reference in New Issue