-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandFactory.java
118 lines (108 loc) · 3.38 KB
/
CommandFactory.java
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
*
*/
/** CommandFactory parses through given text to look for and instantiate Commands appropriately
* @author Team Red
*
*/
import java.util.List;
import java.util.Arrays;
public class CommandFactory {
private static CommandFactory theInstance;
public static List<String> MOVEMENT_COMMANDS =
Arrays.asList("n","w","e","s","u","d" );
public static synchronized CommandFactory instance() {
if (theInstance == null) {
theInstance = new CommandFactory();
}
return theInstance;
}
/** Constructor for CommandFactory class
*
*
*/
private CommandFactory() {}
/** Creates command for user interaction with environment
*
* @param command String
* @return Command
* */
public Command parse(String command) {
String parts[] = command.split(" ");
String verb = parts[0];
String noun = parts.length >= 2 ? parts[1] : "";
if (verb.equals("save")) {
return new SaveCommand(noun);
}
if (verb.equals("take")) {
return new TakeCommand(noun);
}
if (verb.equals("drop")) {
return new DropCommand(noun);
}
if (verb.equals("i") || verb.equals("inventory")) {
return new InventoryCommand();
}
if (MOVEMENT_COMMANDS.contains(verb)) {
return new MovementCommand(verb);
}
//Wait and Item Specific Commands both can take a verb and a noun
if (parts.length == 2) {
//wait command for time
if (verb.equals("wait")) {
int time = Integer.parseInt(noun);
return new WaitCommand(time);
}
//In case of attack command with no weapon
if(verb.equals("attack")){
return new AttackCommand(null, noun);
}
//In case of trade command with no NPC
if(verb.equals("trade")){
return new TradeCommand(null, noun);
}
//ItemSpecificCommand
return new ItemSpecificCommand(verb, noun);
}
if(parts.length == 3)
{
//In case of attack command with no weapon but with a 'with'
if(verb.equals("attack")){
return new AttackCommand(null, noun);
}
//In case of trade command with no NPC but with a 'with'
if(verb.equals("trade")){
return new AttackCommand(null, noun);
}
}
if(parts.length == 4)
{
//Properly formatted attack command
if(verb.equals("attack")){
return new AttackCommand(parts[3], noun);
}
//Properly formatted trade command
if(verb.equals("trade")){
return new AttackCommand(parts[3], noun);
}
}
//Wait command that has no specified wait time
if (verb.equals("wait")) {
int time = Integer.parseInt(noun);
return new WaitCommand(time);
}
//health command
if (verb.equals("health")) {
return new HealthCommand();
}
//score command
if (verb.equals("score")) {
return new ScoreCommand();
}
//trade command
//if (verb.equals("trade")) {
//return new TradeCommand();
//}
return new UnknownCommand(command);
}
}