1
\$\begingroup\$

I'm working on a little text based javascript game and I've been using chromes Javascript console for the output. The 'game' works off a series of methods written into an object called Player created from a constructor Character that are linked to HTML buttons and manipulated with jQuery 1.11.

The code has been validated by multiple sources to be 100% clean JS however, we all know that doesn't always matter. There are a number of bugs i've found that I can't find a solution to.

The two biggest bugs i've noticed:

  • certain instances of enemies will spawn with zero health when as far as I can tell i've made there health to a minimum of 1
  • After a number of battles the player automatically wins every time with 1 hit.

I've included a JSFiddle to hopefully give you an idea of the working code although, you won't see output due to the fact it is all returned in the console.

JSFiddle

Any and all help is really appreciated!

Thanks!

Note: In the the script, the buttons, explore and run aren't linked to anything. The way to trigger the fights is to rest and an enemy will randomly come and there attributes will be loaded into the div enemy. The 'Fight Log' is logged to the console.

Here are is my JS

$(document).ready(function () {
var m = Math;
    //function in Character constructor
    function do_dmg(enemy) {
        //calculates amount of damage done by by finding the lower number between a (rand int from 0 to personal health - a rand int from 0 to enemy health) and
        var dmg = m.min(m.max(
        m.round(m.random() * this.health) - m.round(m.random() * enemy.health), 0),
        enemy.health);
        $('#hp').text(this.health);
        $('#ehp').text(enemy.health -= dmg);
        if (dmg === 0) {
            console.log(this.name + " evades " + enemy.name + "'s attack!");
        } else {
            console.log(this.name + " hurts " + enemy.name + " for " + dmg);
            return enemy.health <= 0;
        }
    }
}

//Methods from Player Object.
p.rest = function () {
    console.log(this.awoken);
    if (this.awoken === 0) {
        window.enemy = Enemies[m.floor(m.random() * Enemies.length)];
        window.enemy = enemy;
        console.log(this.name + ' is awoken by a ' + enemy.name + '!');
        $('#ename').text(enemy.name);
        $('#ehp').text(enemy.health);
        this.state = 'fight';
        $('#state').text(this.state);
        this.enemy_atk();

    } else if (this.awoken == 1) {
        console.log(this.name + ' rests.');
        if (this.health < this.max_health) {
            this.health += 1;
            $('#hp').text(p.health);
        } else if (this.energy < this.max_energy) {
            this.energy += 1;
            $('#ep').text(p.energy);
        } else {
            console.log(this.name + ' slept too long.');
            this.energy -= 1;
            $('#ep').text(p.energy);
        }
    } else {
        console.log(this.name + 'can\'t rest right now');
    }

},

p.enemy_atk = function () {
    if (enemy.health >= 1) {
        if (enemy.do_dmg(this) >= this.health) {
            this.health = 0;
            console.log(this.name + ' was killed by the ' + enemy.name);
        } else {
            $('#ehp').text(enemy.health);
            $('#hp').text(this.health);
        }
    } else {
        $('#ename').text('');
        $('#ehp').text('');
        console.log(this.name + ' killed the ' + enemy.name);
        console.log(this.name + ' gained ' + enemy.XP / this.level + ' EXP!');
        this.state = 'normal';
        $('#state').text(this.state);
    }


},

p.atk = function () {
    if (this.state != 'fight') {
        console.log(this.name + ' swings at nothing and falls.');
        this.tired();
    } else {
        if (this.do_dmg(enemy) >= enemy.health) {
            enemy.health = 0;
            console.log(this.name + ' killed ' + enemy.name);
            console.log(this.name + ' gained ' + enemy.XP / this.level + ' EXP!');
            $('#ename').text('');
            $('#ehp').text('');
            this.state = 'normal';
            $('#state').text(this.state);
        } else {
            this.do_dmg(enemy);
            this.enemy_atk();
        }
    }
},
\$\endgroup\$
1
  • \$\begingroup\$ This is the kind of issue i would solve by re-designing the code : Have clear objects with defined responsabilities, and de-correlate the codes handling 1) the input / 2) the game / 3) and especially the output.!! Clarify !! I started a bit here : jsfiddle.net/vxbp7fxL/6 \$\endgroup\$ Oct 22 '14 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.