-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathzombies.js
361 lines (305 loc) · 10.4 KB
/
zombies.js
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
* Class => Item(name)
* -----------------------------
* Creates an item.
*
* @name Item
* @param {string} name The item's name.
* @property {string} name
*/
/**
* Class => Weapon(name, damage)
* -----------------------------
* Creates a weapon item.
* Weapon items can be equipped for use in battle.
*
* The Weapon class constructor will call
* the super class (Item) constructor
* while passing in the 1 Item constructor param
*
* @name Weapon
* @param {string} name The weapon's name.
* @param {number} damage The weapon's damage.
* @property {number} damage
*/
/**
* Weapon Extends Item Class
* -----------------------------
*/
/**
* Class => Food(name, energy)
* -----------------------------
* Creates a food item.
* Food items give energy, restoring health to the player.
*
* The Food class constructor will call
* the super class (Item) constructor
* while passing in the 1 Item constructor param
*
* @name Food
* @param {string} name The food's name.
* @param {number} energy The energy the food provides.
* @property {number} energy
*/
/**
* Food Extends Item Class
* -----------------------------
*/
/**
* Class => Player(name, health, strength, speed)
* -----------------------------
* Creates a player in a zombie-infested world.
*
* @name Player
* @param {string} name The player's name.
* @param {number} health The player's health.
* @param {number} strength The player's strength.
* @param {number} speed The player's speed.
* @private {array} pack Default value should be empty.
* @private {number} maxHealth Default value should be set to `health`.
* @property {string} name
* @property {number} health
* @property {number} strength
* @property {number} speed
* @property {boolean} isAlive Default value should be `true`.
* @property {Weapon/boolean} equipped Default value should be `false`.
* @property {method} getPack Returns private variable `pack`.
* @property {method} getMaxHealth Returns private variable `maxHealth`.
*/
/**
* Player Class Method => checkPack()
* -----------------------------
* Player checks the contents of their pack.
*
* Nicely format and print the items in the player's pack.
* To access the pack, be sure to use Player's getPack method.
* You should be able to invoke this function on a Player instance.
*
* @name checkPack
*/
/**
* Player Class Method => takeItem(item)
* -----------------------------
* Player takes an item from the world and places it into their pack.
*
* Player's pack can only hold a maximum of 3 items, so if they try to add more
* than that to the pack, return false.
* Before returning true or false, print a message containing the player's
* name and item's name if successful. Otherwise, print a message saying
* that the pack is full so the item could not be stored.
* Note: The player is allowed to store similar items (items with the same name).
* You should be able to invoke this function on a Player instance.
*
* @name takeItem
* @param {Item/Weapon/Food} item The item to take.
* @return {boolean} true/false Whether player was able to store item in pack.
*/
/**
* Player Class Method => discardItem(item)
* -----------------------------
* Player discards an item from their pack.
*
* Use Array's indexOf method to check if the pack contains the item.
* If an item is not found in the pack, indexOf returns -1.
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
*
* If the item is in the pack, remove it from the pack using Array's splice method.
* Print the player and item names and a message saying the item was discarded.
* Return true for the successful discard.
* Note: The splice method can also be used for array element replacement.
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
*
* If the item is not in the pack, return a message with the item name saying
* nothing was discarded since the item could not be found.
* Return false in this case.
*
* You should be able to invoke this function on a Player instance.
*
* @name discardItem
* @param {Item/Weapon/Food} item The item to discard.
* @return {boolean} true/false Whether player was able to remove item from pack.
*/
/**
* Player Class Method => equip(itemToEquip)
* -----------------------------
* Player equips a weapon item.
*
* Player can only equip Weapon instances.
* Player can only equip weapon items from their pack.
*
* If the player already has a weapon equipped (the equipped property
* is set to an Item), find the itemToEquip in the pack and replace
* it with the currently equipped item. Then set the equipped property
* to the itemToEquip.
* However, if the player doesn't already have a weapon equipped, simply
* equip that item and remove it from the pack.
* You should be able to invoke this function on a Player instance.
*
* @name equip
* @param {Weapon} itemToEquip The weapon item to equip.
*/
/**
* Player Class Method => eat(itemToEat)
* -----------------------------
* Player eats a food item, restoring their health.
*
* Player can only eat Food instances.
* Player can only eat food items from their pack.
*
* Remove itemToEat from the pack.
* Increase the player's health by the food's energy amount, but do not
* exceed the player's max health. If exceeded, simply set player's health
* to max health instead.
* To access the player's max health, be sure to use Player's getMaxHealth method.
* You should be able to invoke this function on a Player instance.
*
* @name eat
* @param {Food} itemToEat The food item to eat.
*/
/**
* Player Class Method => useItem(item)
* -----------------------------
* Player uses an item from the pack.
*
* If the item is a weapon, the player should equip the item.
* If the item is food, the player should eat the item.
* You should be able to invoke this function on a Player instance.
*
* @name useItem
* @param {Item/Weapon/Food} item The item to use.
*/
/**
* Player Class Method => equippedWith()
* -----------------------------
* Player checks their equipment.
*
* Prints the player's name and equipped weapon's name.
* If nothing is equipped, prints a message saying so.
* Also returns the equipped weapon's name or false if nothing is equipped.
* You should be able to invoke this function on a Player instance.
*
* @name equippedWith
* @return {string/boolean} Weapon name or false if nothing is equipped.
*/
/**
* Class => Zombie(health, strength, speed)
* -----------------------------
* Creates a normal zombie.
*
* @name Zombie
* @param {number} health The zombie's health.
* @param {number} strength The zombie's strength.
* @param {number} speed The zombie's speed.
* @private {number} maxHealth Default value should be set to `health`.
* @property {number} health
* @property {number} strength
* @property {number} speed
* @property {boolean} isAlive Default value should be `true`.
*/
/**
* Class => FastZombie(health, strength, speed)
* -----------------------------
* Creates a fast zombie.
*
* The FastZombie class constructor will call
* the super class (Zombie) constructor
* while passing in the 3 Zombie constructor params
*
* @name FastZombie
* @param {number} health The zombie's health.
* @param {number} strength The zombie's strength.
* @param {number} speed The zombie's speed.
*/
/**
* FastZombie Extends Zombie Class
* -----------------------------
*/
/**
* Class => StrongZombie(health, strength, speed)
* -----------------------------
* Creates a strong zombie.
*
* The StrongZombie class constructor will call
* the super class (Zombie) constructor
* while passing in the 3 Zombie constructor params
*
* @name StrongZombie
* @param {number} health The zombie's health.
* @param {number} strength The zombie's strength.
* @param {number} speed The zombie's speed.
*/
/**
* StrongZombie Extends Zombie Class
* -----------------------------
*/
/**
* Class => RangedZombie(health, strength, speed)
* -----------------------------
* Creates a ranged zombie.
*
* The RangedZombie class constructor will call
* the super class (Zombie) constructor
* while passing in the 3 Zombie constructor params
*
* @name RangedZombie
* @param {number} health The zombie's health.
* @param {number} strength The zombie's strength.
* @param {number} speed The zombie's speed.
*/
/**
* StrongZombie Extends Zombie Class
* -----------------------------
*/
/**
* Class => ExplodingZombie(health, strength, speed)
* -----------------------------
* Creates an exploding zombie.
*
* The ExplodingZombie class constructor will call
* the super class (Zombie) constructor
* while passing in the 3 Zombie constructor params
*
* @name ExplodingZombie
* @param {number} health The zombie's health.
* @param {number} strength The zombie's strength.
* @param {number} speed The zombie's speed.
*/
/**
* ExplodingZombie Extends Zombie Class
* -----------------------------
*/
/**
* Sample run.
* Feel free to edit this and check your game logic.
*/
function runGame() {
// var player = new Player("Joan", 500, 30, 70);
// var zombie = new Zombie(40, 50, 20);
// var charger = new FastZombie(175, 25, 60);
// var tank = new StrongZombie(250, 100, 15);
// var spitter = new RangedZombie(150, 20, 20);
// var boomer = new ExplodingZombie(50, 15, 10);
// var shovel = new Weapon("shovel", 15);
// var sandwich = new Food("sandwich", 30);
// var chainsaw = new Weapon("chainsaw", 25);
// player.takeItem(shovel);
// player.takeItem(sandwich);
// player.takeItem(chainsaw);
// player.discardItem(new Weapon("scythe", 21));
// player.discardItem(shovel);
// player.checkPack();
// player.takeItem(shovel);
// player.checkPack();
// player.equippedWith();
// player.useItem(chainsaw);
// player.equippedWith();
// player.checkPack();
// player.useItem(shovel);
// player.equippedWith();
// player.checkPack();
// player.health = 487;
// console.log("Before health: " + player.health);
// player.useItem(sandwich);
// console.log("After health: " + player.health);
// player.checkPack();
}