2017-04-21 22:50:48 +02:00
|
|
|
package de.dj_steam.bot.cli;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
2017-04-22 16:07:23 +02:00
|
|
|
import java.util.Optional;
|
2017-04-21 22:50:48 +02:00
|
|
|
|
2017-04-22 16:07:23 +02:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
|
|
import de.dj_steam.bot.domain.Command;
|
2017-04-21 22:50:48 +02:00
|
|
|
import de.dj_steam.bot.engine.RobotEngine;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author steam
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class LoopingConsole {
|
|
|
|
|
|
|
|
private static final String EXIT_COMMAND = "exit";
|
2017-04-22 16:07:23 +02:00
|
|
|
public static final String COMMAND_DELIMITER = " ";
|
2017-04-21 22:50:48 +02:00
|
|
|
|
|
|
|
public static void main(final String[] args) throws IOException {
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
|
|
|
|
|
|
|
printUsageBanner();
|
|
|
|
|
2017-04-21 23:01:58 +02:00
|
|
|
RobotEngine robotEngine = new RobotEngine();
|
|
|
|
|
2017-04-21 22:50:48 +02:00
|
|
|
while (true) {
|
|
|
|
System.out.print("> ");
|
|
|
|
String input = br.readLine();
|
2017-04-22 16:07:23 +02:00
|
|
|
robotEngine.commandBot(createCommand(input));
|
2017-04-21 22:50:48 +02:00
|
|
|
|
|
|
|
if (input.trim().toLowerCase().equals(EXIT_COMMAND)) {
|
|
|
|
System.out.println("exiting");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-22 16:07:23 +02:00
|
|
|
static Command createCommand(final String input) {
|
|
|
|
String[] split = input.split(COMMAND_DELIMITER);
|
|
|
|
|
|
|
|
Command command;
|
|
|
|
|
|
|
|
if(split.length == 2) {
|
|
|
|
command = new Command(split[0], Optional.of(split[1]));
|
|
|
|
} else if(split.length == 1 && !StringUtils.isEmpty(split[0])){
|
|
|
|
command = new Command(split[0], Optional.empty());
|
|
|
|
} else {
|
|
|
|
throw new InvalidUserInputException("Invalid user input");
|
|
|
|
}
|
|
|
|
|
|
|
|
return command;
|
|
|
|
}
|
|
|
|
|
2017-04-21 22:50:48 +02:00
|
|
|
private static void printUsageBanner() {
|
|
|
|
System.out.println("####################################################");
|
|
|
|
System.out.println("Commands:");
|
|
|
|
System.out.println("PLACE X,Y,F - place robot on position X,Y - coordinates, and direction (NORTH|SOUTH|WEST|EAST)");
|
|
|
|
System.out.println("MOVE - change the robot to the next field in facing direction");
|
|
|
|
System.out.println("LEFT - turn the robot to the left");
|
|
|
|
System.out.println("RIGHT - turn the robot to the right");
|
|
|
|
System.out.println("REPORT - show robots position and facing direction");
|
|
|
|
System.out.println("exit - exit the application");
|
|
|
|
System.out.println("####################################################");
|
|
|
|
System.out.println("Enter a command or '" + EXIT_COMMAND + "' to quit");
|
|
|
|
}
|
|
|
|
}
|