forked from SkylerLutz/adventure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerInterpreter.java
More file actions
111 lines (96 loc) · 2.6 KB
/
Copy pathPlayerInterpreter.java
File metadata and controls
111 lines (96 loc) · 2.6 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
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
import java.util.Arrays;
public class PlayerInterpreter {
public Action interpretString(String string) {
if(string.equals("")) {
return Action.ActionPass;
}
return action(string.toLowerCase().split(" "));
}
private Action action(String[] string) throws ArrayIndexOutOfBoundsException {
if(string == null || string.length == 0) {
return Action.ActionPass;
}
if(string[0].compareTo("go") == 0 || string[0].compareTo("travel") == 0 || string[0].compareTo("proceed") == 0){
String[] command = Arrays.copyOfRange(string, 1, string.length);
return action(command);
}
else {
// input could be northeast, put cpu in vax, throw shovel, examine bin
String s = string[0];
Action action = null;
out:{
for(Action a : Action.values()) {
for(String alias : a.getAliases()) {
if(s.compareTo(alias) == 0) {
action = a;
break out;
}
}
}
}
if(action == null) {
return Action.ActionError;
}
switch(action.type()) {
case TYPE_DIRECTIONAL:
return action;
case TYPE_HASDIRECTOBJECT:
// test direct object
// "throw SHOVEL"
if(string.length > 1) {
String d = string[1];
Item item = Item.getInstance(d);
//
// item is the direct object of the action
action.setDirectObject(item);
return action;
}
else {
System.out.println("You must supply a direct object.");
return Action.ActionPass;
}
case TYPE_HASINDIRECTOBJECT:
// test if it has indirect object
// "put CPU IN VAX"
// "Take Diamond from Microwave"
if(string.length > 1) {
String d = string[1];
Item item = Item.getInstance(d);
// item is the direct object of the action
action.setDirectObject(item);
if(string.length > 2) {
String in = string[2];
if(in.equals("in") || in.equals("from")) {
if(string.length > 3) {
String io = string[3];
Item indob = Item.getInstance(io);
action.setIndirectObject(indob);
return action;
}
else {
System.out.println("You must supply an indirect object.");
return Action.ActionError;
}
}
else {
return Action.ActionPass;
}
}
}
else {
System.out.println("You must supply a direct object.");
return Action.ActionError;
}
break;
case TYPE_HASNOOBJECT:
return action;
case TYPE_UNKNOWN:
return Action.ActionError;
default:
System.out.println("Unknown type");
break;
}
}
return Action.ActionPass;
}
}