-
Notifications
You must be signed in to change notification settings - Fork 2
/
Player.java~
193 lines (164 loc) · 5.8 KB
/
Player.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
/**
* Player.java
* @author Emma Shumadine, Lily Orth-Smith, Rachel Zhang
* */
import java.util.NoSuchElementException;
public class Player extends GameCharacter {
final static private int playerHp = 100;
final static private Weapon defaultWeapon = new Weapon("Stick", "A simple stick. Hit Rate: 90%, 1d4 damage", 0.9, "1d4");
final static private int maxInventorySize = 8;
final static Item firstItem = new HealItem("Apple", "A crunchy apple. Can be eaten.", 10);
private Item[] inventory;
private int lastItemIndex;
/**
* Creates a player with an empty inventory
*/
public Player() {
super(playerHp, defaultWeapon, "Player");
inventory = new Item[maxInventorySize];
lastItemIndex = 0;
inventory[lastItemIndex] = firstItem;
}
/**
* Adds an item to the inventory
* @param newItem the item to add
* @throws FullInventoryException if the inventory is full
*/
public void pickUpItem(Item newItem) throws FullInventoryException {
try {
//does make changes even if error is thrown?
inventory[lastItemIndex+1] = newItem;
lastItemIndex++;
} catch (IndexOutOfBoundsException e) {
throw new FullInventoryException();
}
}
/**
* Switches the player's weapon
* @param weaponToPickUp the weapon to be switched
* @return the weapon the player was previously using
*/
public Weapon pickUpWeapon(Weapon weaponToPickUp) {
Weapon savedWeapon = weapon;
weapon = weaponToPickUp;
return savedWeapon;
}
/**
* Drops the item from the player's inventory
* @param itemToDrop the item to remove
* @return the removed item
* @throws NoSuchElementException if the item doesn't exist in the inventory
*/
public Item dropItem(Item itemToDrop) throws NoSuchElementException {
int nullIndex = -1;
for (int i = 0; i <= lastItemIndex; i++) {
if (itemToDrop.equals(inventory[i])) {
inventory[i] = null;
nullIndex = i;
break;
}
}
if (nullIndex == -1) {
throw new NoSuchElementException();
}
for (int i = nullIndex; i < inventory.length-1; i++) {
inventory[i] = inventory[i+1];
}
inventory[inventory.length-1] = null;
lastItemIndex--;
return itemToDrop;
}
/**
* Returns true if the player's inventory is empty
* @return true if the player's inventory is empty
*/
public boolean inventoryIsEmpty() {
return (lastItemIndex == -1);
}
/**
* Uses a health item to heal the player, dropping the item in the process
* @param itemToUse the item to use
* @return the amount healed
* @throws NoSuchElementException if the item doesn't exist in the player's inventory
*/
public int useHealthItem(Item itemToUse) throws NoSuchElementException {
dropItem(itemToUse);
currentHP += itemToUse.getHp();
return itemToUse.getHp();
}
/**
* Returns the player's inventory
* @return the inventory
*/
public Item[] getInventory() {
return inventory;
}
/**
* Returns the maximum inventory size
* @return the maximum inventory size
*/
public int getMaxInventorySize() {
return maxInventorySize;
}
/**
* Returns true if the player has the item in their inventory
* @param item the item
* @return true if the player has the item in their inventory
*/
public boolean hasItem(Item item) {
for (int i = 0; i <= lastItemIndex; i++) {
if (inventory[i].equals(item)) {
return true;
}
}
return false;
}
/**
* Returns the item at the index in the player's inventory
* @param index the index of the item in the inventory
* @return the item at the index
*/
public Item getItem(int index) {
return inventory[index];
}
/**
* Returns a string representation of the player
* @return a string representation of the player
*/
public String toString() {
String inventoryStr;
if (lastItemIndex == -1) {
inventoryStr = "[]";
} else {
inventoryStr = "[" + inventory[0];
for (int i = 1; i <= lastItemIndex; i++) {
inventoryStr += ", " + inventory[i];
}
inventoryStr += "]";
}
return (super.toString() + " Inventory: " + inventoryStr);
}
public static void main(String[] args) {
Player myPlayer = new Player();
HealItem apple = new HealItem("Apple", "A crunchy apple. Can be eaten.", 5);
System.out.println("myPlayer with empty inventory: " + myPlayer);
Item rock = new Item("Rock", "Can be thrown.");
for (int i = 1; i < 10; i++) { //player has default item so can only add 9 items before inventory is full
myPlayer.pickUpItem(rock);
}
System.out.println("myPlayer with full inventory: " + myPlayer);
try {
myPlayer.pickUpItem(rock);
} catch (FullInventoryException e) {
System.out.println("Player's inventory is full, could not pick up another rock");
}
System.out.println("Does Player have an apple? (true): " + myPlayer.hasItem(apple));
System.out.println("Does Player have a rock? (true): " + myPlayer.hasItem(rock));
System.out.println("Dropped: " + myPlayer.dropItem(apple));
System.out.println("Does Player have an apple? (false): " + myPlayer.hasItem(apple));
while (!myPlayer.inventoryIsEmpty()) {
System.out.println("Removed: " + myPlayer.dropItem(rock));
}
System.out.println(myPlayer);
}
}