Today we're creating characters for a game. Each Character
has two properties and two methods.
name
(string) - The Character's namehealth
(number) - A number between 0 and 50 representing the Character's current health
talk
(function) - When spoken to, the character will greet you with theirname
attack
(function) - When attacked, the character can:- take damage, decreasing their
health
- counter-attack by returning a number representing damage points against the attacker
- take damage, decreasing their
Given a base Character
class, implement an inherited class for one or more of the character types below. Your character should have the same interface (properties and method) as described above. Your character can have additional properties or methods if necessary, as long as it also implements the above interface.
Your solution should utilize the following:
- extends to allow your character to inherit from the base
Character
class - super to call and override methods from the base
Character
class when necessary
A wizard behaves as follows:
- When spoken to, a wizard loudly greets you and proclaims their name in all UPPER CASE.
- When first attacked, a wizard casts a protection spell.
- While the protection spell is active, the wizard receives no damage and counter-attacks are twice as powerful.
- The spell lasts for 3 attacks
- After the spell, the wizard recieves 10 points damage on every attack, and counter-attacks return to the default value.
A rogue behaves as follows:
- When spoken to, a rogue quietly greets you and whispers their name in all lower case.
- A rogue is difficult to attack. By default they will evade an attack, receive no damage, and return a 5 point counter-attack.
- On every 5th attack, a rogue lets their guard down and receives 5 points damage. In their weakened state, they return the default counter-attack value.
This repo contains source code in the src
repo to get you started, as well as Jest tests to validate your solution. To get started:
- Clone this repo
cd
into the directory and runnpm install
- Run the tests with
npm test:watch
- Locate the characters in the
src
directory and choose a character to implement.- Note:
src/character
is the base class that the others will inherit from, so don't choose that one.
- Note:
- Locate the character's set of tests in
index.test.js
- Pending tests are marked with
xit
. Unpend a test by changingxit
toit
, watch it fail, and write the code necessary to make the test pass. - Repeat the last step for the rest of the tests in the suite.
How would you implement a solution without using the class
keyword? For example, using a factory function and object composition?