-
Notifications
You must be signed in to change notification settings - Fork 0
/
Item.java
194 lines (167 loc) · 4.85 KB
/
Item.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
*
*/
/** Item represents an object in a bork game. It can be interacted with and affected by the player and knows whether or
* not it can be used as a weapon. If it can, then it knows how much damage it can deal.
* @author Team Red
*
*/
import java.util.Scanner;
import java.util.Hashtable;
import java.util.ArrayList;
public class Item {
static class NoItemException extends Exception {}
private boolean isAWeapon;
private Hashtable<String, ArrayList<String>> events;
private int damage;
private String primaryName;
private int weight;
private Hashtable<String,String> messages;
Item(Scanner s) throws NoItemException,
Dungeon.IllegalDungeonFormatException {
messages = new Hashtable<String,String>();
events = new Hashtable<String, ArrayList<String>>();
// Read item name
String nextLine = s.nextLine();
String[] nextLineParsed = nextLine.split(" ");
primaryName = nextLineParsed[0];
if (primaryName.equals(Dungeon.TOP_LEVEL_DELIM)) {
throw new NoItemException();
}
// Read item weight.
weight = Integer.valueOf(nextLineParsed[1]);
// Read and parse verbs lines, as long as there are more.
String verbLine = s.nextLine();
if(verbLine.startsWith("weapon"))
{
isAWeapon = true;
String[] weaponLineParsed = verbLine.split(":");
if(weaponLineParsed.length < 2)
{
throw new Dungeon.IllegalDungeonFormatException("No damage stat on weapon line.");
}
try{
damage = Integer.parseInt(weaponLineParsed[1]);
}catch(Exception e){
throw new Dungeon.IllegalDungeonFormatException("No damage stat on weapon line or damage stat is."+
"is not an integer.");
}
}
while (!verbLine.equals(Dungeon.SECOND_LEVEL_DELIM)) {
if (verbLine.equals(Dungeon.TOP_LEVEL_DELIM)) {
throw new Dungeon.IllegalDungeonFormatException("No '" +
Dungeon.SECOND_LEVEL_DELIM + "' after item.");
}
//Gets an array of the verb and events, then the message
String[] verbParts = verbLine.split(":");
//Gets an array of the verb, then the events
String[] verbAndEvents = verbParts[0].split("\\[");
//Gets an array of the events
String[] eventsParsed = null;
if(verbAndEvents.length > 1)
{
eventsParsed = verbAndEvents[1].substring(0,verbAndEvents[1].length()-1).split(",");
}
ArrayList<String> eventsList = new ArrayList<>();
if(eventsParsed != null)
{
for(String event:eventsParsed)
{
eventsList.add(event);
}
}
this.events.put(verbAndEvents[0], eventsList);
messages.put(verbAndEvents[0],verbParts[1]);
verbLine = s.nextLine();
}
}
boolean goesBy(String name) {
// could have other aliases
return this.primaryName.equals(name);
}
/** Returns whether or not the Item is considered a weapon
*
* @return The Item's isAWeapon field, True if the weapon is considered a weapon, false otherwise
*/
boolean getIsAWeapon()
{
return isAWeapon;
}
/** Returns the Item's damage stat
*
* @return The Item's damage field, -1 if the Item is not a weapon
*/
int getDamage()
{
return damage;
}
/** Finds a list of events applying to a specific verb
*
* @param verb Verb to find applicable events for
* @return A list of events that apply to the parameter verb, can be empty
*/
ArrayList<String> getEventForVerb(String verb)
{
return events.get(verb);
}
String getPrimaryName() { return primaryName; }
public String getMessageForVerb(String verb) {
return (messages.get(verb) +"\n");
}
public String toString() {
return primaryName;
}
/** Item event that adds the parameter score to the player's current score
*
* @param addToScore Score to add to player's current score
*/
void scoreEvent(int addToScore)
{
GameState.instance().changeScore(addToScore);
}
/** Item event that lowers the player's health by the parameter amount
*
* @param wound Amount to lower player's health by, if negative, player's health is increased
*/
void woundEvent(int wound)
{
GameState.instance().changeHealth(wound);
}
/** Item event that kills the player by lowering player's health as low as it can possibly go
*
*/
void dieEvent()
{
GameState.instance().changeHealth(Integer.MAX_VALUE);
}
/** Item event that causes the player to win
*
*/
void winEvent()
{
GameState.instance().changeScore(Integer.MAX_VALUE);
}
/** Item event that removes this Item from the Dungeon
*
*/
void disappearEvent()
{
GameState.instance().obliterateItem(this);
}
/** Item event that removes this Item from the Dungeon and replaces it with the parameter Item
*
* @param newItem Item to replace this Item with
*/
void transformEvent(Item newItem)
{
GameState instance = GameState.instance();
instance.transformItem(this, newItem);
}
/** Item event that moves the player to a random Room in the Dungeon
*
*/
void teleportEvent()
{
GameState.instance().getDungeon().teleportAdventurer();
}
}