Learn JavaScript · lesson 56 of 60 · about 11 minutes
Project: Game Characters
Model a coder and two kinds of bug as classes, and run the hunt in turns.
What you'll learn
- Design a class for each kind of character
- Rely on an overridden method so each kind acts differently
- Run a simple turn-based game
The hunt
Now the game itself. Sam is a Coder, with samEnergy energy and samPower power. Sam works through bugs in order, one hit a round: fix(bug) calls bug.takeHit(this.power), and if the bug isn't fixed by it, Sam loses 1 energy. Each round adds a line to the hunt, like Round 3: Off by one: 2 of 5 hp left, until every bug is fixed (All 5 bugs fixed in 8 rounds!) or Sam is too tired to go on (Sam is tired: 3 of 5 bugs fixed).
The plan
Every character has a name and health, can take damage and can attack. Heroes and monsters attack differently. The game will:
1. Have a Character base class with name, hp, takeHit() and a getter alive
2. Have Hero and Monster children that override attack()
3. Let them take turns until one is no longer alive
The characters
class Character {
constructor(name, hp) {
this.name = name;
this.hp = hp;
}
get alive() {
return this.hp > 0;
}
takeHit(damage) {
this.hp = Math.max(0, this.hp - damage);
}
attack(target) {
target.takeHit(1);
}
}
class Hero extends Character {
attack(target) {
target.takeHit(5);
}
}
class Monster extends Character {
attack(target) {
target.takeHit(3);
}
}
const hero = new Hero("Ada", 10);
const orc = new Monster("Orc", 12);
hero.attack(orc);
console.log(orc.hp, orc.alive);Output
7 trueTaking turns
A while loop keeps the fight going while both are alive. Each round the hero attacks, and if the monster survives, it attacks back. Because both share Character, the loop doesn't care which kind is which.
The fight
class Character {
constructor(name, hp, power) {
this.name = name;
this.hp = hp;
this.power = power;
}
get alive() {
return this.hp > 0;
}
attack(target) {
target.hp = Math.max(0, target.hp - this.power);
}
}
const hero = new Character("Ada", 10, 5);
const orc = new Character("Orc", 12, 3);
let round = 0;
while (hero.alive && orc.alive) {
round++;
hero.attack(orc);
if (orc.alive) orc.attack(hero);
}
console.log(`${hero.alive ? hero.name : orc.name} wins in round ${round}`);Output
Ada wins in round 3One fix for every kind of bug
fix(bug) doesn't check whether it was given a Bug or a BigBug: it calls bug.takeHit(...) and JavaScript runs the right one. A plain bug takes the full hit, and the memory leak's own takeHit takes its shield off first. That's the point of overriding: the hunt loop stays the same however many kinds of bug there are. If Sam's power is no bigger than a shield, Sam can't hurt that bug at all, and just gets tired.
Tip
Math.max(0, ...) keeps health from going below zero, so the output never shows -3 hp.
Now try it yourself
This lesson is free in Codiqo, with 8 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.