diff options
-rw-r--r-- | src/esieequest/CommandWords.java | 25 | ||||
-rw-r--r-- | src/esieequest/Parser.java | 35 |
2 files changed, 26 insertions, 34 deletions
diff --git a/src/esieequest/CommandWords.java b/src/esieequest/CommandWords.java index 3d8855d..de1d132 100644 --- a/src/esieequest/CommandWords.java +++ b/src/esieequest/CommandWords.java | |||
@@ -1,25 +1,23 @@ | |||
1 | package esieequest; | 1 | package esieequest; |
2 | 2 | ||
3 | /** | 3 | /** |
4 | * This class is part of the "World of Zuul" application. "World of Zuul" is a | ||
5 | * very simple, text based adventure game. | ||
6 | * | ||
7 | * This class holds an enumeration table of all command words known to the game. | 4 | * This class holds an enumeration table of all command words known to the game. |
8 | * It is used to recognise commands as they are typed in. | 5 | * It is used to recognize commands as they are typed in. |
6 | * | ||
7 | * @author Pacien TRAN-GIRARD | ||
8 | * @author Benoit LUBRANO DI SBARAGLIONE | ||
9 | * | 9 | * |
10 | * @author Michael Kolling and David J. Barnes + D.Bureau | 10 | * @version February 2014 |
11 | * @version 2008.03.30 + 2013.09.15 | ||
12 | */ | 11 | */ |
13 | public class CommandWords { | 12 | public class CommandWords { |
14 | // a constant array that holds all valid command words | ||
15 | private static final String[] sValidCommands = { "go", "quit", "help", "look", "eat" }; | 13 | private static final String[] sValidCommands = { "go", "quit", "help", "look", "eat" }; |
16 | 14 | ||
17 | /** | 15 | /** |
18 | * Constructor - initialise the command words. | 16 | * Constructor - initialize the command words. |
19 | */ | 17 | */ |
20 | public CommandWords() { | 18 | public CommandWords() { |
21 | // nothing to do at the moment... | 19 | |
22 | } // CommandWords() | 20 | } |
23 | 21 | ||
24 | /** | 22 | /** |
25 | * Check whether a given String is a valid command word. | 23 | * Check whether a given String is a valid command word. |
@@ -30,8 +28,7 @@ public class CommandWords { | |||
30 | for (int i = 0; i < sValidCommands.length; i++) { | 28 | for (int i = 0; i < sValidCommands.length; i++) { |
31 | if (sValidCommands[i].equals(pString)) | 29 | if (sValidCommands[i].equals(pString)) |
32 | return true; | 30 | return true; |
33 | } // for | 31 | } |
34 | // if we get here, the string was not found in the commands | ||
35 | return false; | 32 | return false; |
36 | } // isCommand() | 33 | } |
37 | } // CommandWords | 34 | } |
diff --git a/src/esieequest/Parser.java b/src/esieequest/Parser.java index 4c09013..08f2bca 100644 --- a/src/esieequest/Parser.java +++ b/src/esieequest/Parser.java | |||
@@ -3,9 +3,6 @@ package esieequest; | |||
3 | import java.util.Scanner; | 3 | import java.util.Scanner; |
4 | 4 | ||
5 | /** | 5 | /** |
6 | * This class is part of the "World of Zuul" application. "World of Zuul" is a | ||
7 | * very simple, text based adventure game. | ||
8 | * | ||
9 | * This parser reads user input and tries to interpret it as an "Adventure" | 6 | * This parser reads user input and tries to interpret it as an "Adventure" |
10 | * command. Every time it is called it reads a line from the terminal and tries | 7 | * command. Every time it is called it reads a line from the terminal and tries |
11 | * to interpret the line as a two word command. It returns the command as an | 8 | * to interpret the line as a two word command. It returns the command as an |
@@ -15,12 +12,14 @@ import java.util.Scanner; | |||
15 | * known commands, and if the input is not one of the known commands, it returns | 12 | * known commands, and if the input is not one of the known commands, it returns |
16 | * a command object that is marked as an unknown command. | 13 | * a command object that is marked as an unknown command. |
17 | * | 14 | * |
18 | * @author Michael Kolling and David J. Barnes + D.Bureau | 15 | * @author Pacien TRAN-GIRARD |
19 | * @version 2008.03.30 + 2013.09.15 | 16 | * @author Benoit LUBRANO DI SBARAGLIONE |
17 | * | ||
18 | * @version February 2014 | ||
20 | */ | 19 | */ |
21 | public class Parser { | 20 | public class Parser { |
22 | private CommandWords aValidCommands; // holds all valid command words | 21 | private CommandWords aValidCommands; |
23 | private Scanner aReader; // source of command input | 22 | private Scanner aReader; |
24 | 23 | ||
25 | /** | 24 | /** |
26 | * Create a parser to read from the terminal window. | 25 | * Create a parser to read from the terminal window. |
@@ -28,36 +27,32 @@ public class Parser { | |||
28 | public Parser() { | 27 | public Parser() { |
29 | this.aValidCommands = new CommandWords(); | 28 | this.aValidCommands = new CommandWords(); |
30 | this.aReader = new Scanner(System.in); | 29 | this.aReader = new Scanner(System.in); |
31 | } // Parser() | 30 | } |
32 | 31 | ||
33 | /** | 32 | /** |
34 | * @return The next command from the user. | 33 | * @return The next command from the user. |
35 | */ | 34 | */ |
36 | public Command getCommand() { | 35 | public Command getCommand() { |
37 | String vInputLine; // will hold the full input line | 36 | String vInputLine; |
38 | String vWord1 = null; | 37 | String vWord1 = null; |
39 | String vWord2 = null; | 38 | String vWord2 = null; |
40 | 39 | ||
41 | System.out.print("> "); // print prompt | 40 | System.out.print("> "); |
42 | 41 | ||
43 | vInputLine = this.aReader.nextLine(); | 42 | vInputLine = this.aReader.nextLine(); |
44 | 43 | ||
45 | // Find up to two words on the line. | ||
46 | Scanner vTokenizer = new Scanner(vInputLine); | 44 | Scanner vTokenizer = new Scanner(vInputLine); |
47 | if (vTokenizer.hasNext()) { | 45 | if (vTokenizer.hasNext()) { |
48 | vWord1 = vTokenizer.next(); // get first word | 46 | vWord1 = vTokenizer.next(); |
49 | if (vTokenizer.hasNext()) { | 47 | if (vTokenizer.hasNext()) { |
50 | vWord2 = vTokenizer.next(); // get second word | 48 | vWord2 = vTokenizer.next(); |
51 | // note: we just ignore the rest of the input line. | 49 | } |
52 | } // if | 50 | } |
53 | } // if | ||
54 | 51 | ||
55 | // Now check whether this word is known. If so, create a command | ||
56 | // with it. If not, create a "null" command (for unknown command). | ||
57 | if (this.aValidCommands.isCommand(vWord1)) { | 52 | if (this.aValidCommands.isCommand(vWord1)) { |
58 | return new Command(vWord1, vWord2); | 53 | return new Command(vWord1, vWord2); |
59 | } else { | 54 | } else { |
60 | return new Command(null, null); | 55 | return new Command(null, null); |
61 | } | 56 | } |
62 | } // getCommand() | 57 | } |
63 | } // Parser | 58 | } |