diff options
author | Pacien TRAN-GIRARD | 2014-04-06 21:18:14 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2014-04-06 21:18:14 +0200 |
commit | d24dd6cc9dcf023e5db160d5600770692f293cc9 (patch) | |
tree | 283578e829b91920635f64d429efa475bd8dbe97 /src | |
parent | 7cec06bea8172ceb7d819f4d2d4474585200c74d (diff) | |
download | esieequest-d24dd6cc9dcf023e5db160d5600770692f293cc9.tar.gz |
Use enums for commands
Diffstat (limited to 'src')
-rw-r--r-- | src/esieequest/controller/Interpreter.java | 24 | ||||
-rw-r--r-- | src/esieequest/controller/Parser.java | 24 | ||||
-rw-r--r-- | src/esieequest/model/commands/Command.java | 6 | ||||
-rw-r--r-- | src/esieequest/model/commands/CommandWord.java | 63 | ||||
-rw-r--r-- | src/esieequest/view/app/UserInterface.java | 23 | ||||
-rw-r--r-- | src/esieequest/view/web/WebInterface.java | 17 |
6 files changed, 48 insertions, 109 deletions
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 { | |||
48 | public void dispatch(final Command command) { | 48 | public void dispatch(final Command command) { |
49 | if (command.getAction() != null) { | 49 | if (command.getAction() != null) { |
50 | switch (command.getAction()) { | 50 | switch (command.getAction()) { |
51 | case "new": | 51 | case NEW: |
52 | this.performer.newGame(); | 52 | this.performer.newGame(); |
53 | return; | 53 | return; |
54 | case "load": | 54 | case LOAD: |
55 | this.performer.loadGame(); | 55 | this.performer.loadGame(); |
56 | return; | 56 | return; |
57 | case "save": | 57 | case SAVE: |
58 | this.performer.saveGame(); | 58 | this.performer.saveGame(); |
59 | return; | 59 | return; |
60 | case "sound": | 60 | case SOUND: |
61 | this.performer.toggleSound(); | 61 | this.performer.toggleSound(); |
62 | return; | 62 | return; |
63 | case "go": | 63 | case GO: |
64 | this.performer.goTo(command.getOption()); | 64 | this.performer.goTo(command.getOption()); |
65 | return; | 65 | return; |
66 | case "back": | 66 | case BACK: |
67 | this.performer.goBack(); | 67 | this.performer.goBack(); |
68 | return; | 68 | return; |
69 | case "look": | 69 | case LOOK: |
70 | this.performer.look(); | 70 | this.performer.look(); |
71 | return; | 71 | return; |
72 | case "inventory": | 72 | case INVENTORY: |
73 | this.performer.listItems(); | 73 | this.performer.listItems(); |
74 | return; | 74 | return; |
75 | case "take": | 75 | case TAKE: |
76 | this.performer.take(command.getOption()); | 76 | this.performer.take(command.getOption()); |
77 | return; | 77 | return; |
78 | case "drop": | 78 | case DROP: |
79 | this.performer.drop(command.getOption()); | 79 | this.performer.drop(command.getOption()); |
80 | return; | 80 | return; |
81 | case "help": | 81 | case HELP: |
82 | this.performer.showHelp(); | 82 | this.performer.showHelp(); |
83 | return; | 83 | return; |
84 | case "quit": | 84 | case QUIT: |
85 | this.performer.quitGame(); | 85 | this.performer.quitGame(); |
86 | return; | 86 | return; |
87 | } | 87 | } |
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 @@ | |||
1 | package esieequest.controller; | 1 | package esieequest.controller; |
2 | 2 | ||
3 | import esieequest.model.commands.Command; | 3 | import esieequest.model.commands.Command; |
4 | import esieequest.model.commands.CommandWord; | ||
4 | 5 | ||
5 | /** | 6 | /** |
6 | * The command parser. | 7 | * The command parser. |
@@ -26,25 +27,20 @@ class Parser { | |||
26 | public Command getCommand(final String commandString) { | 27 | public Command getCommand(final String commandString) { |
27 | final String[] elements = commandString.split(" "); | 28 | final String[] elements = commandString.split(" "); |
28 | 29 | ||
29 | String action = null; | 30 | CommandWord action = null; |
30 | String option = null; | 31 | String option = null; |
31 | 32 | ||
32 | if (elements.length > 0) { | 33 | try { |
33 | action = elements[0]; | 34 | action = CommandWord.valueOf(elements[0].toUpperCase()); |
34 | } | 35 | } catch (Exception e) { |
35 | if (elements.length > 1) { | 36 | action = CommandWord.UNKNOWN; |
36 | option = elements[1]; | 37 | } finally { |
38 | if (elements.length > 1) { | ||
39 | option = elements[1]; | ||
40 | } | ||
37 | } | 41 | } |
38 | 42 | ||
39 | return new Command(action, option); | 43 | return new Command(action, option); |
40 | } | 44 | } |
41 | 45 | ||
42 | /** | ||
43 | * Checks whether the command contains a valid action with a valid option. | ||
44 | */ | ||
45 | private boolean validateCommand() { | ||
46 | // TODO | ||
47 | return true; | ||
48 | } | ||
49 | |||
50 | } | 46 | } |
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; | |||
7 | */ | 7 | */ |
8 | public class Command { | 8 | public class Command { |
9 | 9 | ||
10 | private final String action; | 10 | private final CommandWord action; |
11 | private final String option; | 11 | private final String option; |
12 | 12 | ||
13 | /** | 13 | /** |
@@ -18,7 +18,7 @@ public class Command { | |||
18 | * @param option | 18 | * @param option |
19 | * the option | 19 | * the option |
20 | */ | 20 | */ |
21 | public Command(final String action, final String option) { | 21 | public Command(final CommandWord action, final String option) { |
22 | this.action = action; | 22 | this.action = action; |
23 | this.option = option; | 23 | this.option = option; |
24 | } | 24 | } |
@@ -28,7 +28,7 @@ public class Command { | |||
28 | * | 28 | * |
29 | * @return the action | 29 | * @return the action |
30 | */ | 30 | */ |
31 | public String getAction() { | 31 | public CommandWord getAction() { |
32 | return this.action; | 32 | return this.action; |
33 | } | 33 | } |
34 | 34 | ||
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 @@ | |||
1 | package esieequest.model.commands; | 1 | package esieequest.model.commands; |
2 | 2 | ||
3 | /** | 3 | /** |
4 | * Contains the commands that the user can enter | 4 | * Contains the commands that the user can enter. |
5 | * | 5 | * |
6 | * @author Pacien TRAN-GIRARD | 6 | * @author Pacien TRAN-GIRARD |
7 | */ | 7 | */ |
8 | public enum CommandWord { | 8 | public enum CommandWord { |
9 | 9 | ||
10 | // @formatter:off | 10 | NEW, LOAD, SAVE, SOUND, HELP, QUIT, LOOK, FORWARD, BACK, GO, TURN, DO, TAKE, DROP, TALK, INVENTORY, USE, UNKNOWN; |
11 | // COMMAND ARGUMENTS ("" = no one ; null = any) | ||
12 | NEW ( "new", new String[] { "" } ), | ||
13 | LOAD ( "load", new String[] { "" } ), | ||
14 | SAVE ( "save", new String[] { "" } ), | ||
15 | SOUND ( "sound", new String[] { "" } ), | ||
16 | HELP ( "help", new String[] { "" } ), | ||
17 | QUIT ( "quit", new String[] { "" } ), | ||
18 | LOOK ( "look", new String[] { "" } ), | ||
19 | EAT ( "eat", new String[] { "" } ), | ||
20 | FORWARD ( "forward", new String[] { "" } ), | ||
21 | BACK ( "back", new String[] { "" } ), | ||
22 | GO ( "go", new String[] { "north", "south", "east", "west", "up", "down" } ), | ||
23 | TURN ( "turn", new String[] { "left", "right" } ), | ||
24 | DO ( "do", new String[] { "" } ), | ||
25 | TAKE ( "take", new String[] { "" } ), | ||
26 | TALK ( "talk", new String[] { "" } ), | ||
27 | INVENTORY ( "inventory", new String[] { "" } ), | ||
28 | USE ( "use", null ), | ||
29 | UNKNOWN ( "?", new String[] { "" } ); | ||
30 | // @formatter:on | ||
31 | |||
32 | private String action; | ||
33 | private String[] validOptions; | ||
34 | |||
35 | /** | ||
36 | * The constructor. | ||
37 | * | ||
38 | * @param action | ||
39 | * the action command | ||
40 | * @param validOption | ||
41 | * the list of the valid parameters | ||