-
Notifications
You must be signed in to change notification settings - Fork 0
/
Char.java
219 lines (198 loc) · 5.77 KB
/
Char.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Set;
/**
* Write a description of class Char here.
*
* @author (your name)
* @version (a version number or a date)
*/
public abstract class Char
{
// instance variables - replace the example below with your own
protected Location currentPosition; // the players current position
protected int hp; // health points
protected int maxHp; // max health points
protected int ap; // attack points
protected int dp; // damage points
protected int maxStorage;
protected HashMap<String, Item> inventory; // collection of items
/**
* Constructor for objects of class Char
*/
public Char()
{
// initialise instance variables
inventory = new HashMap<String, Item>(); // new empty hashmap for inventory
maxStorage = 10;
}
public boolean attack(Char target)
{
// this character/player attacking an opponent
return true;
}
public int getHealthPoints() {
return this.hp;
}
public int getMaxHp()
{
return this.maxHp;
}
public int getAp()
{
return this.ap;
}
public int getDamagePoints()
{
//return 100;
return this.dp;
}
/**
* Return a string describing the players inventory, for example
* "Inventory: health".
*
* @return Details of the players inventory
*/
public String getInventoryString()
{
if(!hasItems()){
return "Inventory Empty";
}
String returnString = "Inventory:";
Set<String> keys = inventory.keySet();
for(String item : keys) {
returnString += " " + item;
}
return returnString;
}
/**
* Return a string describing the players inventory, for example
* "Inventory: health".
*
* @return Details of the players inventory
*/
public ArrayList<String> getInventoryItemsArray()
{
if(!hasItems()){
return null;
}
ArrayList<String> items = new ArrayList<String>();
Set<String> keys = inventory.keySet();
for(String item : keys) {
items.add(item);
}
return items;
}
/**
* hasItem checks if the player has an item with
* particular key in inventory
*
* @param String k the items key
* @return true if the player has the item otherwise false
*/
public boolean hasItem(String k){
return inventory.containsKey(k);
}
/**
* hasItems checks if the player has ANY
* items in the inventory
*
* @return true if the player has >0 items otherwise false
*/
public boolean hasItems(){
return !inventory.isEmpty();
}
/**
* addItem adds a new item object to the players inventory
*
* @params String name - item key, Item item - item object
*/
public void addItem(String name, Item item)
{
inventory.put(name,item);
}
/**
* getItem retrieves an item from the players inventory
*
* @params String name - the name (key) of an item
* @return Item the item object if found
*/
public Item getItem(String name)
{
return inventory.get(name);
}
/**
* removeItem removes an item from the players inventory
*
* @params String name - the name (key) of an item
*/
public void removeItem(String name)
{
inventory.remove(name);
}
/**
* takeItem checks if inventory is not already full and
* proceeds to add the item to the players inventory and
* remove it from the players currentPosition (location)
*
* @params String name - the name (key) of an item
* @return boolean True if success throws a custom InventoryException otherwise
*/
public boolean takeItem(String itemName) throws InventoryException
{
if(inventory.size() >= this.maxStorage){
throw new InventoryException("Inventory Full");
}
if(!this.currentPosition.hasItem(itemName)){
throw new InventoryException("No Item Located");
}else{
Item thisItem = this.currentPosition.getItem(itemName);
Item playerItemKey = inventory.get(itemName);
if(playerItemKey != null) {
throw new InventoryException("Already have this item");
}else{
inventory.put(itemName, thisItem);
this.currentPosition.removeItem(itemName);
return true;
}
}
}
/**
* takeItem removes the item from the players inventory
* and then adds it to the players currentPosition
*
* @params String name - the name (key) of an item
* @return boolean True if success false otherwise
*/
public boolean dropItem(String itemName)
{
if(hasItem(itemName)){
Item thisItem = inventory.get(itemName);
this.currentPosition.addItem(itemName, thisItem);
inventory.remove(itemName);
return true;
}else{
return false;
}
}
/**
* getCurrentPosition returns the players current position as a location
*
* @return Location the players current location object
*/
public Location getCurrentPosition()
{
return currentPosition;
}
/**
* setCurrentPosition method
*
* Sets the players current location (brute force compared to move)
*
* @param Location a location object to set as the players current position
*/
public void setCurrentPosition(Location location)
{
currentPosition = location;
}
}