-6

I am making a basic game with popup boxes.

My problem is that if you look at the goblinAttack function it will repeat till the goblin is dead but the damage is random.

For example, I hit the goblin 6 damage every time until it dies instead of it being a random number between 1 and 25 and the goblin does 4 damage on me every time it hits. Its boring. I wanted it to be random each hit but repeating the function seems to not give a random number each time.

    //VARIABLES
    var dmgMultiply = Math.floor(Math.random() * 10 + 1);
    var dmg = 10;
    var armour = 0;
    var hp = 100;

    //ENEMY VARIABLES
    var dmgGoblin = Math.floor(Math.random() * 10 + 1);
    var goblinHP = 100;

    //ARRAYS
    var choiceArray = ["Type 'sword' equip your sword.", 
                       "Type 'run' if you're scared.",
                       "Type 'stats' to see your stats."];
    var answerArray = ["sword", "run", "stats"];
    var outcomeArrayOne = ["You equip your sword.", 
                           "You run away. Game Over."];            
    var outcomeArrayTwo = ["Sword Equipped."]

    //GAME CHOICE
    function choice(a, b, c, x, y, z, aa, bb)
    {
         var answer = prompt(a + "\n" + b + "\n" + c);

         if(answer == x)
         {
              alert(aa);
              swordEquipped(outcomeArrayTwo[0]);
         }

         if(answer == y)
         {
              alert(bb);
         }

         if(answer == z)
         {
              displayStats();   
         }
    }

    //EQUIPPED SWORD
    function swordEquipped(a)
    {
        dmg = 25;
        dmgMultiply = Math.floor(Math.random() * 25 + 1);
        alert(a + "\n" + "DMG : " + dmg);
        goblinCombatStart("goblin");
    }

    //GOBLIN COMBAT START
    function goblinCombatStart(g)
    {
        alert("A wild " + g + " appears!" + "\n" + "The " + g + " has 100 HP!");
        goblinAttack("goblin")
    }

    function goblinAttack(g)
    {
        alert("The " + g + " swings his axe at you!" + "\n"  + "The " + g + " does " + dmgGoblin + " damage to you!");
        hp -= dmgGoblin;
        var attack = prompt("Type 'attack' to swing your sword at the " + g + "\n" + "HP : " + hp);

        if(attack == "attack")
        {
            alert("You swing your sword at the " + g + "\n" + "You did " + dmgMultiply + " to the " + g);
            goblinHP -= dmgMultiply;
            alert("The " + g + " is on " + goblinHP + "HP");

            if(goblinHP < 0)
            {
                goblinDead();
            }

            if(goblinHP > 0)
            {
                goblinAttack("goblin");
            }
        }
        else
        {
            alert("You dropped your sword an shouted " + "'" + attack + "'" + " in the goblins face. It wasn't very effective and the goblin choppped your head off");
        }
    }

    function goblinDead()
    {
        alert("You have slain the puny goblin with only a few scratches!");
    }

    //GAME START
    choice(choiceArray[0], choiceArray[1], choiceArray[2], 
           answerArray[0], answerArray[1], answerArray[2],
           outcomeArrayOne[0], outcomeArrayOne[1]);

    //STATISTICS
    function displayStats()
    {
         var statCheck = confirm("HP : " + hp + "\n" + 
                                 "ARMOUR : " + armour + "\n" + 
                                 "DMG : " + dmg + "\n" +
                                 "Press OK to continue");
         if(statCheck == true)
         {
              choice(choiceArray[0], choiceArray[1], choiceArray[2], 
                     answerArray[0], answerArray[1], answerArray[2],
                     outcomeArrayOne[0], outcomeArrayOne[1]);
         }
    }
user229044
  • 232,980
  • 40
  • 330
  • 338
  • Thats as random as JavaScript's (or any other mathematically determined) random gets. If you need true randomness, check: http://www.random.org/ – techfoobar Jul 12 '14 at 14:47
  • 3
    [This](http://xkcd.com/221/) may be why. – AstroCB Jul 12 '14 at 14:48
  • 2
    Please, *please* never write code like this again: `function choice(a, b, c, x, y, z, aa, bb)`. That's some of the worst variable naming I've ever seen. How are other people (like the people you're asking for help from) supposed to understand what that function is doing and what inputs it expects? – user229044 Jul 12 '14 at 14:49
  • so its impsoibble to make it a random amount of damage each time I repeat the function – user3832004 Jul 12 '14 at 14:50
  • 1
    Yes, clearly it's "impsoibble", just because you haven't gotten an answer in the first 5 minutes. You're asking strangers for help. Rather than throw your hands up and cry "It's impossible", wait a while. Better yet, fix your code: Provide us with the **smallest** complete example that can reproduce your problem, rather than your entire program. Make it easy for us to help and you'll get better help, faster. – user229044 Jul 12 '14 at 14:53

2 Answers2

2

Instead of computing the random damage value only once at the beginning of your script, compute them whenever you need them. That would probably be in your attack function.

Maybe you are misunderstand something here: Math.random doesn't return some special magical value which changes every time it's read. It returns a number and that number doesn't change.

If you want multiple random values, you have to call Math.random multiple times. If you want a different random value whenever the goblin attacks, you have to call Math.random when it attacks.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

The problem here is that you're initializing your variables at the beginning as random.

That means that, on every move, it will use the same random number that was generated at the beginning.

If you want it to be random for each time, you have to call Math.random() on every move.

As it stands, dmgMultiply and dmgGoblin are computed randomly once and use the same value on each turn, so you end up getting the same amount of damage taken and dealt each time.

Declare your variables at the beginning:

var dmgMultiply;
var dmgGoblin;

Then, set them to a random number within your attack function so that it computes a random number each turn:

function goblinAttack(g)
    {
        dmgMultiply = Math.floor(Math.random() * 10 + 1);
        dmgGoblin = Math.floor(Math.random() * 10 + 1);
        ...
    }

Demo

AstroCB
  • 12,337
  • 20
  • 57
  • 73