-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandWords.java
More file actions
81 lines (72 loc) · 2.29 KB
/
CommandWords.java
File metadata and controls
81 lines (72 loc) · 2.29 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
78
79
80
81
/**
* Class CommandWords - Holds an enumeration table of all command words known to the game.
* It is used to recognise commands as they are typed in.
*
* This class is part of the "World of Zuul" application.
* "World of Zuul" is a very simple, text based adventure game.
*
*
* @author Michael Kolling and David J. Barnes + D.Bureau
* @version 2008.03.30 + 2019.09.25
*/
public class CommandWords
{
// ### Attributes ###
// a constant array that will hold all valid command words
private final String[] aValidCommands;
// ### Constructor ###
/**
* Constructor - initialise the command words.
*/
public CommandWords()
{
this.aValidCommands = new String[14];
this.aValidCommands[0] = "go";
this.aValidCommands[1] = "look";
this.aValidCommands[2] = "help";
this.aValidCommands[3] = "quit";
this.aValidCommands[4] = "eat";
this.aValidCommands[5] = "back";
this.aValidCommands[6] = "test";
this.aValidCommands[7] = "take";
this.aValidCommands[8] = "drop";
this.aValidCommands[9] = "inventory";
this.aValidCommands[10] = "fire";
this.aValidCommands[11] = "charge";
this.aValidCommands[12] = "use";
this.aValidCommands[13] = "win";
} // CommandWords()
// ### Getter ###
/**
* Prints all valid commands to System.out.
*
* @return A list of all the valid commands.
*/
public String getCommandList()
{
String vCommandList = "";
for(String vCommand : this.aValidCommands)
{
vCommandList += (vCommand + " ");
}
return vCommandList;
} // showAll()
// ### Other Methods ###
/**
* Check whether a given String is a valid command word.
*
* @param pString Command to be checked.
*
* @return true if a given string is a valid command,
* false if it isn't.
*/
public boolean isCommand( final String pString )
{
for ( int vI=0; vI<this.aValidCommands.length; vI++ ) {
if ( this.aValidCommands[vI].equals( pString ) )
return true;
} // for
// if we get here, the string was not found in the commands :
return false;
} // isCommand()
} // CommandWords