4

I am trying to make a text adventure that will output the correct console log statement based on a specific input. I want it so that if the goblin does enough attack damage so that the player's defense is less than 0 that the console log will print out "The Goblin did" x amount of "damage!". Here is my variable list at the top of the code:

//player stats
var atk= 1;
var def= 1;
var hp= 10;
var mp= 0;
var block= 1;
var magic= 0;
//goblin stats
var gobAtk= [3,4,5];
var gobDef= 1;
var gobHp= 5;
var gobMagDef= 0;
var rand= Math.floor(Math.random()* gobAtk.length);

At the bottom of my code a specific if/else condition is called as long as the goblin's hp (var gobHp) has not gone down to zero after the player attacks the goblin in the earlier section of the code. Here it is:

else {
    def-=rand;
        if(def<0) {
            hp-=Math.abs(rand);
            console.log("The Goblin did"+ " "+ Math.abs(rand)+ " "+ "Damage!");
                if(hp<=0) {
                    console.log("The Goblin defeated you! You died");
                    console.log("Game Over.");

                }

        }
        else {
            console.log("The Goblin did 0 damage!");
        }
}

But no matter how many times I run through the code the console log always prints out "The Goblin did 0 damage!" even if the goblin's atk minuses the player's def to less than 0. If the player's def is less than 0 the console log should print out the correct amount of damage.

4

1 回答 1

2

从评论中,您的随机生成器应包括下限和上限:

function getRandomArbitrary(min, max) {
   return Math.random() * (max - min) + min;
}
var rand = getRandomArbitrary(3,5)

评论 :

正如另一个(已删除)答案所述,您应该重新计算地精每次攻击的随机数,否则地精将始终具有相同的攻击力

于 2015-11-01T23:18:41.240 回答