From d24dd6cc9dcf023e5db160d5600770692f293cc9 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 6 Apr 2014 21:18:14 +0200 Subject: Use enums for commands --- src/esieequest/controller/Interpreter.java | 24 +++++----- src/esieequest/controller/Parser.java | 24 ++++------ src/esieequest/model/commands/Command.java | 6 +-- src/esieequest/model/commands/CommandWord.java | 63 +------------------------- src/esieequest/view/app/UserInterface.java | 23 +++++----- src/esieequest/view/web/WebInterface.java | 17 +++---- 6 files changed, 48 insertions(+), 109 deletions(-) (limited to 'src') diff --git a/src/esieequest/controller/Interpreter.java b/src/esieequest/controller/Interpreter.java index 5c60f6d..32adeda 100644 --- a/src/esieequest/controller/Interpreter.java +++ b/src/esieequest/controller/Interpreter.java @@ -48,40 +48,40 @@ class Interpreter { public void dispatch(final Command command) { if (command.getAction() != null) { switch (command.getAction()) { - case "new": + case NEW: this.performer.newGame(); return; - case "load": + case LOAD: this.performer.loadGame(); return; - case "save": + case SAVE: this.performer.saveGame(); return; - case "sound": + case SOUND: this.performer.toggleSound(); return; - case "go": + case GO: this.performer.goTo(command.getOption()); return; - case "back": + case BACK: this.performer.goBack(); return; - case "look": + case LOOK: this.performer.look(); return; - case "inventory": + case INVENTORY: this.performer.listItems(); return; - case "take": + case TAKE: this.performer.take(command.getOption()); return; - case "drop": + case DROP: this.performer.drop(command.getOption()); return; - case "help": + case HELP: this.performer.showHelp(); return; - case "quit": + case QUIT: this.performer.quitGame(); return; } diff --git a/src/esieequest/controller/Parser.java b/src/esieequest/controller/Parser.java index 452c27c..cea2b78 100644 --- a/src/esieequest/controller/Parser.java +++ b/src/esieequest/controller/Parser.java @@ -1,6 +1,7 @@ package esieequest.controller; import esieequest.model.commands.Command; +import esieequest.model.commands.CommandWord; /** * The command parser. @@ -26,25 +27,20 @@ class Parser { public Command getCommand(final String commandString) { final String[] elements = commandString.split(" "); - String action = null; + CommandWord action = null; String option = null; - if (elements.length > 0) { - action = elements[0]; - } - if (elements.length > 1) { - option = elements[1]; + try { + action = CommandWord.valueOf(elements[0].toUpperCase()); + } catch (Exception e) { + action = CommandWord.UNKNOWN; + } finally { + if (elements.length > 1) { + option = elements[1]; + } } return new Command(action, option); } - /** - * Checks whether the command contains a valid action with a valid option. - */ - private boolean validateCommand() { - // TODO - return true; - } - } diff --git a/src/esieequest/model/commands/Command.java b/src/esieequest/model/commands/Command.java index ea33c35..15b4322 100644 --- a/src/esieequest/model/commands/Command.java +++ b/src/esieequest/model/commands/Command.java @@ -7,7 +7,7 @@ package esieequest.model.commands; */ public class Command { - private final String action; + private final CommandWord action; private final String option; /** @@ -18,7 +18,7 @@ public class Command { * @param option * the option */ - public Command(final String action, final String option) { + public Command(final CommandWord action, final String option) { this.action = action; this.option = option; } @@ -28,7 +28,7 @@ public class Command { * * @return the action */ - public String getAction() { + public CommandWord getAction() { return this.action; } diff --git a/src/esieequest/model/commands/CommandWord.java b/src/esieequest/model/commands/CommandWord.java index 2afa372..555fe15 100644 --- a/src/esieequest/model/commands/CommandWord.java +++ b/src/esieequest/model/commands/CommandWord.java @@ -1,71 +1,12 @@ package esieequest.model.commands; /** - * Contains the commands that the user can enter + * Contains the commands that the user can enter. * * @author Pacien TRAN-GIRARD */ public enum CommandWord { - // @formatter:off - // COMMAND ARGUMENTS ("" = no one ; null = any) - NEW ( "new", new String[] { "" } ), - LOAD ( "load", new String[] { "" } ), - SAVE ( "save", new String[] { "" } ), - SOUND ( "sound", new String[] { "" } ), - HELP ( "help", new String[] { "" } ), - QUIT ( "quit", new String[] { "" } ), - LOOK ( "look", new String[] { "" } ), - EAT ( "eat", new String[] { "" } ), - FORWARD ( "forward", new String[] { "" } ), - BACK ( "back", new String[] { "" } ), - GO ( "go", new String[] { "north", "south", "east", "west", "up", "down" } ), - TURN ( "turn", new String[] { "left", "right" } ), - DO ( "do", new String[] { "" } ), - TAKE ( "take", new String[] { "" } ), - TALK ( "talk", new String[] { "" } ), - INVENTORY ( "inventory", new String[] { "" } ), - USE ( "use", null ), - UNKNOWN ( "?", new String[] { "" } ); - // @formatter:on - - private String action; - private String[] validOptions; - - /** - * The constructor. - * - * @param action - * the action command - * @param validOption - * the list of the valid parameters - */ - CommandWord(final String action, final String[] validOption) { - this.action = action; - this.validOptions = validOption; - } - - @Override - public String toString() { - return this.action; - } - - /** - * Returns the action. - * - * @return the action - */ - public String getAction() { - return this.action; - } - - /** - * Returns the valid options. - * - * @return the valid options list - */ - public String[] getValidOptions() { - return this.validOptions; - } + NEW, LOAD, SAVE, SOUND, HELP, QUIT, LOOK, FORWARD, BACK, GO, TURN, DO, TAKE, DROP, TALK, INVENTORY, USE, UNKNOWN; } diff --git a/src/esieequest/view/app/UserInterface.java b/src/esieequest/view/app/UserInterface.java index e2fbc9a..9551522 100644 --- a/src/esieequest/view/app/UserInterface.java +++ b/src/esieequest/view/app/UserInterface.java @@ -18,6 +18,7 @@ import javax.swing.border.EmptyBorder; import com.wordpress.tipsforjava.swing.StretchIcon; import esieequest.controller.GameEngine; +import esieequest.model.commands.CommandWord; import esieequest.model.events.Quest; import esieequest.model.items.Item; import esieequest.model.map.Room; @@ -173,17 +174,17 @@ abstract class UserInterface implements View, ActionListener { leftButton.setPreferredSize(squareButton); rightButton.setPreferredSize(squareButton); - newButton.setActionCommand("new"); - soundButton.setActionCommand("sound"); - loadButton.setActionCommand("load"); - saveButton.setActionCommand("save"); - - forwardButton.setActionCommand("forward"); - inventoryButton.setActionCommand("inventory"); - actionButton.setActionCommand("do"); - backButton.setActionCommand("back"); - leftButton.setActionCommand("turn left"); - rightButton.setActionCommand("turn right"); + newButton.setActionCommand(CommandWord.NEW.name()); + soundButton.setActionCommand(CommandWord.SOUND.name()); + loadButton.setActionCommand(CommandWord.LOAD.name()); + saveButton.setActionCommand(CommandWord.SAVE.name()); + + forwardButton.setActionCommand(CommandWord.FORWARD.name()); + inventoryButton.setActionCommand(CommandWord.INVENTORY.name()); + actionButton.setActionCommand(CommandWord.DO.name()); + backButton.setActionCommand(CommandWord.BACK.name()); + leftButton.setActionCommand(CommandWord.TURN.name() + " left"); + rightButton.setActionCommand(CommandWord.TURN.name() + " right"); this.gameButtons = new HashMap(); diff --git a/src/esieequest/view/web/WebInterface.java b/src/esieequest/view/web/WebInterface.java index 000c794..409414b 100644 --- a/src/esieequest/view/web/WebInterface.java +++ b/src/esieequest/view/web/WebInterface.java @@ -23,6 +23,7 @@ import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.Widget; import esieequest.controller.GameEngine; +import esieequest.model.commands.CommandWord; import esieequest.model.events.Quest; import esieequest.model.items.Item; import esieequest.model.map.Room; @@ -149,14 +150,14 @@ class WebInterface extends Composite implements View { * Binds the buttons to send a defined command to the controller. */ private void bindButtons() { - this.newButton.addClickHandler(this.makeClickHandler("new")); - this.loadButton.addClickHandler(this.makeClickHandler("load")); - this.saveButton.addClickHandler(this.makeClickHandler("save")); - this.actionButton.addClickHandler(this.makeClickHandler("action")); - this.forwardButton.addClickHandler(this.makeClickHandler("go forward")); - this.backButton.addClickHandler(this.makeClickHandler("go back")); - this.leftButton.addClickHandler(this.makeClickHandler("turn left")); - this.rightButton.addClickHandler(this.makeClickHandler("turn right")); + this.newButton.addClickHandler(this.makeClickHandler(CommandWord.NEW.name())); + this.loadButton.addClickHandler(this.makeClickHandler(CommandWord.LOAD.name())); + this.saveButton.addClickHandler(this.makeClickHandler(CommandWord.SAVE.name())); + this.actionButton.addClickHandler(this.makeClickHandler(CommandWord.DO.name())); + this.forwardButton.addClickHandler(this.makeClickHandler(CommandWord.GO.name()+" forward")); + this.backButton.addClickHandler(this.makeClickHandler(CommandWord.GO.name()+" back")); + this.leftButton.addClickHandler(this.makeClickHandler(CommandWord.TURN.name()+" left")); + this.rightButton.addClickHandler(this.makeClickHandler(CommandWord.TURN.name()+" right")); this.soundButton.addClickHandler(new ClickHandler() { @Override -- cgit v1.2.3 From 159b168b2618e4bf73130cba262570e70a374438 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 6 Apr 2014 23:24:11 +0200 Subject: Reimplement help command --- src/esieequest/controller/Parser.java | 2 +- src/esieequest/controller/Performer.java | 12 +++++++++--- src/esieequest/view/web/WebInterface.java | 8 ++++---- 3 files changed, 14 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/esieequest/controller/Parser.java b/src/esieequest/controller/Parser.java index cea2b78..7b07efb 100644 --- a/src/esieequest/controller/Parser.java +++ b/src/esieequest/controller/Parser.java @@ -32,7 +32,7 @@ class Parser { try { action = CommandWord.valueOf(elements[0].toUpperCase()); - } catch (Exception e) { + } catch (final Exception e) { action = CommandWord.UNKNOWN; } finally { if (elements.length > 1) { diff --git a/src/esieequest/controller/Performer.java b/src/esieequest/controller/Performer.java index 8f8afeb..7ba8c94 100644 --- a/src/esieequest/controller/Performer.java +++ b/src/esieequest/controller/Performer.java @@ -1,6 +1,10 @@ package esieequest.controller; +import java.util.HashSet; +import java.util.Set; + import esieequest.model.Game; +import esieequest.model.commands.CommandWord; import esieequest.model.items.Inventory; import esieequest.model.map.Room; import esieequest.view.View; @@ -88,9 +92,11 @@ class Performer { * Displays the help message and the available commands. */ public void showHelp() { - this.notImplemented(); - // this.echo(this.game.getHelpMessage()); - // TODO: list commands + final Set commands = new HashSet(); + for (final CommandWord command : CommandWord.values()) { + commands.add(command.name().toLowerCase()); + } + this.echo(Utils.list(commands, "Commands:", "No commands.")); } /** diff --git a/src/esieequest/view/web/WebInterface.java b/src/esieequest/view/web/WebInterface.java index 409414b..452284d 100644 --- a/src/esieequest/view/web/WebInterface.java +++ b/src/esieequest/view/web/WebInterface.java @@ -154,10 +154,10 @@ class WebInterface extends Composite implements View { this.loadButton.addClickHandler(this.makeClickHandler(CommandWord.LOAD.name())); this.saveButton.addClickHandler(this.makeClickHandler(CommandWord.SAVE.name())); this.actionButton.addClickHandler(this.makeClickHandler(CommandWord.DO.name())); - this.forwardButton.addClickHandler(this.makeClickHandler(CommandWord.GO.name()+" forward")); - this.backButton.addClickHandler(this.makeClickHandler(CommandWord.GO.name()+" back")); - this.leftButton.addClickHandler(this.makeClickHandler(CommandWord.TURN.name()+" left")); - this.rightButton.addClickHandler(this.makeClickHandler(CommandWord.TURN.name()+" right")); + this.forwardButton.addClickHandler(this.makeClickHandler(CommandWord.GO.name() + " forward")); + this.backButton.addClickHandler(this.makeClickHandler(CommandWord.GO.name() + " back")); + this.leftButton.addClickHandler(this.makeClickHandler(CommandWord.TURN.name() + " left")); + this.rightButton.addClickHandler(this.makeClickHandler(CommandWord.TURN.name() + " right")); this.soundButton.addClickHandler(new ClickHandler() { @Override -- cgit v1.2.3