The code below is one of the key methods for my simple 'maze game' program (which, despite its simplicity, happens to be my biggest project yet). In this method, I am defining the possible user commands and giving instructions for their execution. The reason I have the "throw IllegalArgumentException" at the top is that, when I call this method in the main method, I want to catch random or erroneous user input as an 'IllegalArgumentException e' and print out a message ("That's not a valid command. Enter "help" if you want to know the valid commands.").
Unfortunately, because I haven't come up with a way of grouping together the eight possible commands as the only possible arguments in this method, the illegal argument exception isn't thrown when I run the program and enter invalid input. Instead, nothing happens when I enter invalid input.
So I'm looking for a neat way of encoding within this method that the argument "action" is only allowed to be "help", "status" ... "down" (and the messy nature of the "save" command throws a spanner in the works). Any suggestions? I'm thinking that the answer might involve turning the possible commands into an array, but exactly how to do it isn't clear to me (because I'm a n00b).
public static void performAction(String action) throws IllegalArgumentException {
if (action.equalsIgnoreCase("help")) {
printHelp(); }
if (action.equalsIgnoreCase("status")) {
printStatus(); }
if (action.equalsIgnoreCase("board")) {
printBoard(); }
if (((action.charAt(0) == 'S') || (action.charAt(0) == 's')) && ((action.charAt(1) == 'a') || (action.charAt(1) == 'A')) && ((action.charAt(2) == 'v') || (action.charAt(2) == 'V')) &&
((action.charAt(3) == 'e') || (action.charAt(3) == 'E')) && (action.charAt(4) == ' ')) {
String [] parts = action.split(" ");
String saveCommand = parts[0];
String fileName = parts[1];
try { saveGame(fileName); }
catch(IOException e) {
System.err.printf("Error: Could not save the current game configuration to '%s'.", fileName);
return; } }
if (action.equalsIgnoreCase("left")) {
int a = getCurrentXPosition();
int b = getCurrentYPosition();
moveTo((a - 1), b); }
if (action.equalsIgnoreCase("right")) {
int a = getCurrentXPosition();
int b = getCurrentYPosition();
moveTo((a + 1), b); }
if (action.equalsIgnoreCase("up")) {
int a = getCurrentXPosition();
int b = getCurrentYPosition();
moveTo(a, (b - 1)); }
if (action.equalsIgnoreCase("down")) {
int a = getCurrentXPosition();
int b = getCurrentYPosition();
moveTo(a, (b + 1)); }
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…