Three

<< Pre | Next >>

Students Do: Character Creation (30 mins)

  • Each character created using your constructor should have the following properties...
  • Name: The character's name ‑‑> String
  • Profession: What the character does for a living ‑‑> String
  • Gender: The character's gender ‑‑> String
  • Age: The character's age ‑‑> Integer
  • Strength: Abstraction for how strong the character is ‑‑> Integer
  • HitPoints (HP): Abstraction for how much health the character has ‑‑> Integer
  • PrintStats: Function which prints all of a character's properties to the screen

// constructor function which can take in a series of values and create objects
// with the properties contained inside
function Character(name, profession, gender, age, strength, hitpoints) {
  this.name = name;
  this.profession = profession;
  this.gender = gender;
  this.age = age;
  this.strength = strength;
  this.hitpoints = hitpoints;

  // method which prints all of the stats for a character
  this.printStats = function() {
    console.log("Name: " + this.name + "\nProfession: " + this.profession +
    "\nGender: " + this.gender + "\nAge: " + this.age + "\nStrength: " +
    this.strength + "\nHitPoints: " + this.hitpoints);
    console.log("\n-------------\n");
  };
  // method which determines whether or not a character's "hitpoints" are less then zero
  // and returns true or false depending upon the outcome
  this.isAlive = function() {
    if (this.hitpoints > 0) {
      console.log(this.name + " is still alive!");
      console.log("\n-------------\n");
      return true;
    }
    console.log(this.name + " has died!");
    return false;
  };

  // method which takes in a second object and decreases their "hitpoints" by this character's strength
  this.attack = function(character2) {
    character2.hitpoints -= this.strength;
  };

  // method which increases this character's stats when called
  this.levelUp = function() {
    this.age += 1;
    this.strength += 5;
    this.hitpoints += 25;
  };
}

// creates two unique characters using the "character" constructor
var warrior = new Character("Crusher", "Warrior", "Male", 25, 10, 75);
var rogue = new Character("Dodger", "Rogue", "Female", 23, 20, 50);

warrior.printStats();
rogue.printStats();

rogue.attack(warrior);
warrior.printStats();
warrior.isAlive();

rogue.levelUp();
rogue.printStats();

node 03-characterCreate-withRPG.js

output>
Name: Crusher
Profession: Warrior
Gender: Male
Age: 25
Strength: 10
HitPoints: 75

-------------

Name: Dodger
Profession: Rogue
Gender: Female
Age: 23
Strength: 20
HitPoints: 50

-------------

Name: Crusher
Profession: Warrior
Gender: Male
Age: 25
Strength: 10
HitPoints: 55

-------------

Crusher is still alive!

-------------

Name: Dodger
Profession: Rogue
Gender: Female
Age: 24
Strength: 25
HitPoints: 75

-------------

Crusher is still alive!

-------------

Dodger is still alive!

-------------

Name: Crusher
Profession: Warrior
Gender: Male
Age: 25
Strength: 10
HitPoints: 30

-------------

Name: Dodger
Profession: Rogue
Gender: Female
Age: 24
Strength: 25
HitPoints: 65

-------------

Crusher is still alive!

-------------

Dodger is still alive!

-------------

Name: Crusher
Profession: Warrior
Gender: Male
Age: 25
Strength: 10
HitPoints: 5

-------------

Name: Dodger
Profession: Rogue
Gender: Female
Age: 24
Strength: 25
HitPoints: 55

-------------

Crusher is still alive!

-------------

Dodger is still alive!

-------------

Name: Crusher
Profession: Warrior
Gender: Male
Age: 25
Strength: 10
HitPoints: -20

-------------

Name: Dodger
Profession: Rogue
Gender: Female
Age: 24
Strength: 25
HitPoints: 45

-------------

Crusher has died!