- Navigate to this project in your terminal.
- Run
http-server
. - Open http://localhost:8080 in Chrome. You should see failing tests.
- Complete the exercise by following the comments in
zombies.js
or the README.
For each test you complete:
- Save
zombies.js
. - Reload http://localhost:8080 in Chrome.
- Check if the test passes.
- If it passes, commit your work.
See help with class construction, parameters, properties, and inheritence in
https://github.com/devleague/js-zombies/blob/master/doc/oop-example.pdf
When instructed to call the super class, use the Function.prototype.call
method.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
// SUPER CLASS
function Item(name) {
this.name = name;
}
// SUB CLASS
function Weapon(name, damage) {
this.damage = damage;
Item.call(this, name); // Call super class
}
To extend the super class, use the Object.create
method introduced in
https://github.com/devleague/oop-factory-example
// EXTEND SUPER CLASS
Weapon.prototype = Object.create(Item.prototype, {
constructor: {
value: Item
}
});
Creates an item.
Parameters
name
: string, The item's name.
Public Properties
name
: string
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
Parameters
name
: string, The weapon's name.
damage
: number, The weapon's damage.
Public Properties
damage
: number
The Weapon class will extend the Item class prototype
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
Parameters
name
: string, The food's name.
energy
: number, The energy the food provides.
Public Properties
energy
: number
The Food class will extend the Item class prototype
Creates a player in a zombie-infested world.
Parameters
name
: string, The player's name.
health
: number, The player's health.
strength
: number, The player's strength.
speed
: number, The player's speed.
Private Properties
pack
: array, Default value should be empty.
maxHealth
: number, Default value should be set to health
.
Public Properties
name
: string
health
: number
strength
: number
speed
: number
isAlive
: boolean, Default value should be true
.
equipped
: Weapon/boolean, Default value should be false
.
getPack
: method, Returns private variable pack
.
getMaxHealth
: method, Returns private variable maxHealth
.
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.
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, return false.
Before returning true or false, print a message containing the player and item names 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.
Parameters
item
: Item/Weapon/Food, The item to take.
Returns: boolean, Whether player was able to store item in pack.
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.
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
Then print the player and item names and a message saying the item was discarded.
Return true for the successful discard.
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.
Parameters
item
: Item/Weapon/Food, The item to discard.
Returns: boolean, Whether player was able to remove item from pack.
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 has nothing equipped, simply equip itemToEquip and remove it from the pack.
You should be able to invoke this function on a Player instance.
Parameters
itemToEquip
: Weapon, The weapon item to equip.
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.
Parameters
itemToEat
: Food, The food item to eat.
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.
Parameters
item
: Item/Weapon/Food, The item to use.
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.
Returns: weapon name or false
: string/boolean, Weapon name or false
if nothing is equipped.
Creates a normal zombie.
Parameters
health
: number, The zombie's health.
strength
: number, The zombie's strength.
speed
: number, The zombie's speed.
Private Properties
maxHealth
: number, Default value should be set to health
.
Public Properties
health
: number
strength
: number
speed
: number
isAlive
: boolean, Default value should be true
.
Creates a fast zombie.
Use the call
method on the Zombie constructor.
Set FastZombie's prototype to a new instance of Zombie.
Parameters
health
: number, The zombie's health.
strength
: number, The zombie's strength.
speed
: number, The zombie's speed.
The FastZombie class will extend the Zombie class prototype
Creates a strong zombie.
Use the call
method on the Zombie constructor.
Set StrongZombie's prototype to a new instance of Zombie.
Parameters
health
: number, The zombie's health.
strength
: number, The zombie's strength.
speed
: number, The zombie's speed.
The StrongZombie class will extend the Zombie class prototype
Creates a ranged zombie.
Use the call
method on the Zombie constructor.
Set RangedZombie's prototype to a new instance of Zombie.
Parameters
health
: number, The zombie's health.
strength
: number, The zombie's strength.
speed
: number, The zombie's speed.
The RangedZombie class will extend the Zombie class prototype
Creates an exploding zombie.
Use the call
method on the Zombie constructor.
Set ExplodingZombie's prototype to a new instance of Zombie.
Parameters
health
: number, The zombie's health.
strength
: number, The zombie's strength.
speed
: number, The zombie's speed.
The ExplodingZombie class will extend the Zombie class prototype