Explorer.java
package robot;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Explorer {
public static void main(String[] args) {
System.out.println("Base consumption of the exploration robot ");
Scanner scanner = new Scanner(System.in);
double energy = scanner.nextDouble();
Robot robot = new Robot(energy, new Battery(), new RoadBookCalculator());
boolean quitter = false;
while (!quitter) {
displayMenu();
String commande;
do {
commande = scanner.nextLine();
} while (commande.length() != 1);
switch (commande.charAt(0)) {
case 'A':
System.out.println("coordinates column,row grouded robot");
Coordinates coord = lireCoordonnee(scanner);
try {
robot.land(coord, new LandSensor(new Random(10)));
System.out.println("Current position: (" + robot.getXposition() + " ," + robot.getYposition() + ") - orientation : " + robot.getDirection());
} catch (UnlandedRobotException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (LandSensorDefaillance landSensorDefaillance) {
landSensorDefaillance.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
break;
case 'Z':
try {
robot.moveForward();
System.out.println("Current position: (" + robot.getXposition() + " ," + robot.getYposition() + ") - orientation: " + robot.getDirection());
} catch (UnlandedRobotException e) {
System.out.println("The robot is still in the air, it must land first.");
} catch (InsufficientChargeException e) {
System.out.println("Oups, batteries empty... be patient, the sun is doing its work");
} catch (LandSensorDefaillance landSensorDefaillance) {
System.out.println("Ouch, the terrain detection module is malfunctioning. Abandoning exploration.");
throw new RuntimeException(landSensorDefaillance);
} catch (InaccessibleCoordinate inaccessibleCoordinate) {
System.out.println("the terrain in front of the robot is unusable");
}
break;
case 'S':
try {
robot.moveBackward();
System.out.println("Current position: (" + robot.getXposition() + " ," + robot.getYposition() + ") - orientation: " + robot.getDirection());
} catch (UnlandedRobotException e) {
System.out.println("The robot is still in the air, it must land first.");
} catch (InsufficientChargeException e) {
System.out.println("Oups, batteries empty... be patient, the sun is doing its work");
} catch (LandSensorDefaillance landSensorDefaillance) {
System.out.println("Ouch, the terrain detection module is malfunctioning. Abandoning exploration.");
throw new RuntimeException(landSensorDefaillance);
} catch (InaccessibleCoordinate inaccessibleCoordinate) {
System.out.println("the terrain in front of the robot is unusable");
}
break;
case 'Q':
try {
robot.turnLeft();
System.out.println("Current position: (" + robot.getXposition() + " ," + robot.getYposition() + ") - orientation: " + robot.getDirection());
} catch (UnlandedRobotException e) {
System.out.println("The robot is still in the air, it must land first.");
} catch (InsufficientChargeException e) {
System.out.println("Oups, batteries empty... be patient, the sun is doing its work");
}catch (LandSensorDefaillance landSensorDefaillance) {
System.out.println("Hello Houston, we have a problem. We lost the ground return.");
displayBlackBox(robot.blackBox);
}catch (InaccessibleCoordinate inaccessibleCoordinate) {
System.out.println("Unfortunately, I am unable to reach the requested destination.");
displayBlackBox(robot.blackBox);
}
break;
case 'D':
try {
robot.turnRight();
System.out.println("Current position: (" + robot.getXposition() + ", " + robot.getYposition() + ") - orientation: " + robot.getDirection());
} catch (UnlandedRobotException e) {
System.out.println("The robot is still in the air, it must land first.");
} catch (InsufficientChargeException e) {
System.out.println("Oups, batteries empty... be patient, the sun is doing its work");
}catch (LandSensorDefaillance landSensorDefaillance) {
System.out.println("Hello Houston, we have a problem. We lost the ground return.");
displayBlackBox(robot.blackBox);
}catch (InaccessibleCoordinate inaccessibleCoordinate) {
System.out.println("Unfortunately, I am unable to reach the requested destination.");
displayBlackBox(robot.blackBox);
}
break;
case 'L':
try {
robot.cartographier();
} catch (LandSensorDefaillance landSensorDefaillance) {
System.out.println("Unable to establish mapping");
break;
} catch (UnlandedRobotException e) {
System.out.println("The robot is still in the air, it must land first.");
}
for (String ligne : robot.carte()) {
System.out.println(ligne);
}
break;
case 'M':
System.out.println("coordinates column, row of the destination");
Coordinates destination = lireCoordonnee(scanner);
try {
robot.computeRoadTo(destination);
} catch (UnlandedRobotException e) {
System.out.println("Unable to establish a route, the robot is still in the air.");
break;
} catch (LandSensorDefaillance landSensorDefaillance) {
System.out.println("Hello Houston, we have a problem. We lost the ground return.");
break;
} catch (UndefinedRoadbookException e) {
System.out.println("Unable to establish a route, destination is unreachable");
}
try {
List<CheckPoint> checkPoints = robot.letsGo();
for (CheckPoint point : checkPoints) {
System.out.println("Position: (" + point.position.getX() + " ," + point.position.getY() + ") - orientation: " + point.direction);
}
} catch (UnlandedRobotException e) {
System.out.println("The robot is still in the air, it must land first.");
} catch (UndefinedRoadbookException e) {
System.out.println("It appears that the road book is missing.");
} catch (InsufficientChargeException e) {
System.out.println("Sorry, I don't have enough energy to finish my journey.");
displayBlackBox(robot.blackBox);
} catch (LandSensorDefaillance landSensorDefaillance) {
System.out.println("Hello Houston, we have a problem. We lost the ground return.");
displayBlackBox(robot.blackBox);
} catch (InaccessibleCoordinate inaccessibleCoordinate) {
System.out.println("Unfortunately, I am unable to reach the requested destination.");
displayBlackBox(robot.blackBox);
}
break;
case 'X':
quitter = true;
}
}
}
static Coordinates lireCoordonnee(Scanner scanner) {
boolean conforme;
int x = 0;
int y = 0;
do {
conforme = true;
String line = scanner.nextLine();
String[] tokens = line.replace("(", "").replace(")", "").split(",");
if (tokens.length != 2) {
conforme = false;
System.out.println("Format incorrect. c, r ou (c, r)");
}
else
try {
x = Integer.valueOf(tokens[0].trim());
y = Integer.valueOf(tokens[1].trim());
} catch (NumberFormatException e) {
conforme = false;
}
} while (!conforme);
return new Coordinates(x, y);
}
private static void displayBlackBox(BlackBox blackBox) {
System.out.println("Black box contents");
for (CheckPoint point : blackBox.getCheckPointList()) {
System.out.println("Position: (" + point.position.getX() + " ," + point.position.getY() + ") - orientation: " + point.direction + " commande mode: " + (point.manualDirective ? "manual" : "automatic"));
}
}
private static void displayMenu() {
System.out.println("Control panel");
System.out.println("A: lAnd robot");
System.out.println("Z : forward");
System.out.println("Q : turn to left");
System.out.println("D : turn to right");
System.out.println("S : backward");
System.out.println("L : map around the robot");
System.out.println("M : give a coordinate to reach");
System.out.println("X : Quit");
}
}