Page 1 of 1

Could anyone help me find what's wrong with this script?

PostPosted: Wed Oct 22, 2014 2:13 am
by KingScar
Hey guys, been awhile since I posted on here, but I figured if anyone could help me figure this out, it'd be you lot. :lol:

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.

http://jsfiddle.net/ryanhagz/vxbp7fxL/5/

**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

Code: Select all
$(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();
            }
        }
    },


If you'd like, I could post the whole script to give more reference. I just didn't wanna spam a bunch of code though either. lol


Any and all help is really appreciated!

Thanks! :D

Re: Could anyone help me find what's wrong with this script?

PostPosted: Wed Oct 22, 2014 4:50 am
by MikuzA
Hello,

I tested your game through the JSFiddle, I see something really fishy about the 'REST' button, or that enables me to do the instakills frequently.
Or to be more precise, the enemy_atk() function seems to have some glitch.

I added a health-check here:
Code: Select all
    p.enemy_atk = function () {
        console.log(enemy.health);
        if (enemy.health >= 1) {


And this is what I got in console after I have gained defeated one enemy.
Ryan is awoken by a Goblin! (index):140
0 (index):167
Ryan killed the Goblin (index):179
Ryan gained 25 EXP! (index):180
0 (index):136
Ryan is awoken by a Demon! (index):140
0 (index):167
Ryan killed the Demon (index):179
Ryan gained 50 EXP! (index):180
0 (index):136
Ryan is awoken by a Demon! (index):140
0 (index):167
Ryan killed the Demon (index):179
Ryan gained 50 EXP! (index):180
0 (index):136
Ryan is awoken by a Dragon! (index):140
0 (index):167
Ryan killed the Dragon (index):179
Ryan gained 100 EXP!


So, the enemies spawn with zero hp.. :-)
Also I noticed that when I press REST frequently sometimes I see my health popping up to 8, instead of expected 3 or 4. Like matching the enemies HP, but didn't see any direct flaw in the code to grasp this.

Hope this helps :-)

Re: Could anyone help me find what's wrong with this script?

PostPosted: Thu Oct 23, 2014 1:32 am
by Winawer
The code seems to pick from a pool of three actual enemies, not types of enemies. When the player kills an enemy it will have 0 HP and will basically respawn as a corpse next time.

Re: Could anyone help me find what's wrong with this script?

PostPosted: Thu Oct 23, 2014 3:42 am
by MikuzA
Winawer wrote:The code seems to pick from a pool of three actual enemies, not types of enemies. When the player kills an enemy it will have 0 HP and will basically respawn as a corpse next time.

Respawn as a corpse.. ZOMBIE ALERT :cry: