Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

JavaScript extends

So I got bored while following along the OO JavaScript course and I decided to make a little console based player vs monster game.

I am noticing that my objects Monster and Player have near identical prototypes for attack and success (<--- success is a simple bool for detecting if a attack could be blocked or if the monster can counter).

"use strict";

function Player (name) {
    this.name = name;
    this.health = 10;
    this.luck = 5;
}
Player.prototype.attack = function(target) {
    clear();
    if (this.health > 0) {
        if (target.health > 0) {
            console.log(this.name + ' attacks ' + target.name);
            if (target.success(this)) {
                console.log(target.name + ' dodged the attack!');
            } else {
                target.health--;
                console.log(target.name + '\'s health is ' + target.health);
            }
            if (target.success(this)) {
                console.log(target.name + ' counter attacks!');
                target.attack(this);
            }
            console.log('\n \n');
            console.log('SIMULATED NEW TURN');
            target.attack(this);
        } else {
            console.log(target.name + ' is defeated!');
        }
    } else {
        console.log(this.name + ' has fallen!');
    }
}

Player.prototype.success = function(target) {
    this.attackerBonus = this.luck;
    this.defenderBonus = target.luck;
    var odds = Math.ceil(Math.random() * 100) + (this.attackerBonus - this.defenderBonus);
    if (odds > 70) {
        return true;
    }
    return false;
}

function Enemy (name, type) {
    this.name = name;
    this.health = 10;
    this.type = type;
    this.luck = 4;
}

Enemy.prototype.attack = function(target) {
    if (this.health > 0) {
        if (target.health > 0) {
            console.log(this.name + ' bites'); 
            if (target.success(this)) {         
                console.log(target.name + ' dodged the attack!');
            } else {
                target.health--;
                console.log(target.name + '\'s health is ' + target.health);
            }

        } else {
            console.log(target.name + ' has fallen!');
        }    
    } else {
        console.log(this.name + ' has been defeated!');
    }    
}
Enemy.prototype.success = function(target) {
    this.attackerBonus = this.luck;
    this.defenderBonus = target.luck;
    var odds = Math.ceil(Math.random() * 100) + (this.attackerBonus - this.defenderBonus);
    if (odds > 70) {
        return true;
    }
    return false;
}

var Fenrir = new Enemy('Fenrir', 'wolf');
var Thor = new Player('Thor');

I was thinking of creating another object called Action that has the shared prototypes and the Player and Monster can extend Action... but I can't figure it out.

BTW copy the code and past it in the console and then try Thor.attack(Fenrir); to play

UPDATED CODE ::

here is my updated code, working although I'm not happy with the console logs because I want to output different things for the object type ie player.attack (target) should output => "player attacks", enemy.attack(target) should maybe output => "bites".

"use strict";

function Character (name) {
    this.name = name;
}

Character.prototype.attack = function(target) {
    clear();
    if (!this.health > 0) {
        console.log(this.name + ' has fallen!');
    } else {
        if (!target.health > 0) {
            console.log(target.name + ' is dead.');
        } else {
            console.log(this.name + ' attacks.');
            if (target.success(this)) {
                console.log(target.name + ' dodged the attack!')
            } else {
                target.health--;
                console.log(target.name + '\'s health is ' + target.health);
            }

            if (target.success(this)) {
                console.log(target.name + ' counters...');
                target.attack(this);
            } 
        }
    } 
}
Character.prototype.success = function(target) {
    this.attackerBonus = this.luck;
    this.defenderBonus = target.luck;
    var odds = Math.ceil(Math.random() * 100) + (this.attackerBonus - this.defenderBonus);
    if (odds > 70) {
        return true;
    }
    return false;
}

function Player (name) {
    Character.call(this, name);
    this.health = 10;
    this.luck = 5;
}
Player.prototype = Object.create(Character.prototype);

function Enemy (name, type) {
    Character.call(this, name);
    this.health = 10;
    this.type = type;
    this.luck = 4;
}
Enemy.prototype = Object.create(Character.prototype);

var fenrir = new Enemy('Fenrir', 'wolf');
var thor = new Player('Thor');

1 Answer

Steven Parker
Steven Parker
215,939 Points

Since your characters already have a type, you could do this:

            console.log(this.name + (this.type=='wolf' ? ' bites.' : ' attacks.'));

Or, you could add an attacktype property to each character, set to 'attack' for players and 'bite' for fenrir, and do this:

            console.log(this.name + ' ' + this.attacktype + 's.');

Oh, and good job overall! :+1:

:information_source: Bonus tip:

// you can replace all this:
    if (odds > 70) {
        return true;
    }
    return false;

// with this:
    return odds > 70;

Thanks, I actually went with the attacktype before reading this. (good to know I'm on the same page as others)

I had types because I thought about having types of enemies and even ranks of types so a House Cat vs Lion they are both feline but the lion might be substantially more powerful. Also I could later add things like ground attacks are useless against flying type enemies. for example.

Of course this would be the very basics of only a part of this like battle mechanics in a larger game engine.

While this started out as a way for me to better understand objects, I am thinking of adding promises so I can add cooldowns for attacks etc. IDK.... I think I want to build a little game maybe make it an open project.