-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.java
More file actions
77 lines (64 loc) · 2.31 KB
/
Parser.java
File metadata and controls
77 lines (64 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.util.StringTokenizer;
/**
* Class Parser - Takes user input and tries to interpret it as a "Zuul"
* command. Every time it is called it takes a line as a String and
* tries to interpret the line as a two word command. It returns the command
* as an object of class Command.
*
* The parser has a set of known command words. It checks user input against
* the known commands, and if the input is not one of the known commands, it
* returns a command object that is marked as an unknown command.
*
* This class is part of "World of Zuul". "World of Zuul" is a simple,
* text based adventure game.
*
* @author Michael Kolling and David J. Barnes
* @version 2.0 (Jan 2003) DB edited (2019)
*/
public class Parser
{
private CommandWords aCommandWords; // holds all valid command words
/**
* Create a new Parser.
*/
public Parser()
{
this.aCommandWords = new CommandWords();
} // Parser()
/**
* Get a new command from the user. The command is read by
* parsing the 'inputLine'.
*
* @param pInputLine Command from the user to be read.
* @return a command after checking if the words are known or not.
*/
public Command getCommand( final String pInputLine )
{
String vWord1;
String vWord2;
StringTokenizer tokenizer = new StringTokenizer( pInputLine );
if ( tokenizer.hasMoreTokens() )
vWord1 = tokenizer.nextToken(); // get first word
else
vWord1 = null;
if ( tokenizer.hasMoreTokens() )
vWord2 = tokenizer.nextToken(); // get second word
else
vWord2 = null;
// note: we just ignore the rest of the input line.
// Now check whether this word is known. If so, create a command
// with it. If not, create a "null" command (for unknown command).
if ( this.aCommandWords.isCommand( vWord1 ) )
return new Command( vWord1, vWord2 );
else
return new Command( null, vWord2 );
} // getCommand(.)
/**
* Returns a String with valid command words.
* @return a list of all the valid command words.
*/
public String getCommandString() // was showCommands()
{
return this.aCommandWords.getCommandList();
} // getCommandString()
} // Parser