An odd result


#1

When I type this code, the result of it is very strange. It says that I killed the dragon and then the dragon killed me. Why is that and how can I fix it?

This is my code below:

var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 0;

while (slaying)
{
if (youHit)
{
console.log("You killed the dragon " );
totalDamage += damageThisRound;

    if (totalDamage >= 4) { 
        console.log("The dragon is dead and you did" + damageThisRound + "damage") ; 
        slaying = false;
    }
    else 
    {
        youHit  = Math.floor(Math.random() * 2);
    }

}
else
{
    console.log("The dragon killed you");  
      slaying = false;
}

};

Also why does it log "false" inside the result box too?


#2

Hey @dragonflare,

To the second: It simply logs it. you can't do anything against it.

To the first, you got a little "fault" in it:
Hitting != Killing.

So the first console.log(); should say something like console.log("You dealt " + damageThisRound + " dmg to the dragon!");

The rest should work. The "wrong" console.log(); doesn't make your code bad, it's just a bit incorrect, so there is a chance that you "slayed" the dragon (It says you "killed" it), and then the dragon "kills" you. :slight_smile:


The next part of this exercise is better. You can improve the "game".

A few ideas:
- what if the dragon has to hit you 10x until you die?
- you also have a chance to do a crit-strike, which doubles the damage?

Therefore my code below:

var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var critStrike = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 0;
var miss = 0;

while (slaying) {
    if (youHit) {
        if (critStrike) {
            damageThisRound *= 2.5;
            console.log("Gz, you hittet the Dragon with a " + (damageThisRound) + "dmg Crit-Strike!");
            totalDamage += damageThisRound;
        } else {
            console.log("Gz, you hittet the Dragon with a " + (damageThisRound) + "dmg Strike!");
            totalDamage += damageThisRound;
        }
        if (totalDamage >= 50) {
            console.log("Gz, you slayed the Dragon with a total of " + totalDamage + "dmg!"); 
            slaying = false;
        } else {
            damageThisRound = Math.floor(Math.random() * 5 + 1);
            youHit = Math.floor(Math.random() * 2);
            critStrike = Math.floor(Math.random() * 2);
        }
    } else {
        if (miss < 10) {
            console.log("You missed!");
            damageThisRound = Math.floor(Math.random() * 5 + 1);
            youHit = Math.floor(Math.random() * 2);
            critStrike = Math.floor(Math.random() * 2);
            miss++;
        } else {
            console.log("You missed! The Dragon defeated you!");
            slaying = false;
        }
    }
}

#3

Thank you VERY MUCH. I loved the way you improved the game! I have also found out what was the cause of the dragon killing me even though I hit the dragon. For some reason if you add (damageThisRound ) to the second (if)'s console.log() it will sometimes result that I hit the dragon, did an (x) amount of damage, and then the dragon kills me. Another result was that I hit the dragon an (x) amount of times but only did 1 damage each time and then it killed me. Can you tell me why the (damageThisRound) variable on the second (if) was the cause of all this?


#6

Hey @dragonflare,
below my code again - but with explanations in the comments.


/* --- Head-Part / Define variables --- */

/* Variable to check if the either the dragon killed you, or you killed the dragon. */
var slaying = true; 

/* Variable to check if you hitted the dragon, or missed it. */
var youHit = Math.floor(Math.random() * 2);

/* Variable to set the Crit-Chance (in this case it's pretty high - 50%) */
var critStrike = Math.floor(Math.random() * 2);

/* Variable to check the amount of dmg for THIS round. */
var damageThisRound = Math.floor(Math.random() * 5 + 1);

/* Variable to sum up all the dmg per round. */
var totalDamage = 0;

/* Variable to log how many times you already missed. */
var miss = 0;



/* --- Main-Part / The game itself --- */

/* While the dragon didn't defeat you or you defeated the dragon,
you are still fighting against it, so the code inside the while() will run */
while (slaying) {

    /* If youHit results in 1, then  you hitted the dragon */
    if (youHit) {

        /* --- Hit-Part / This code runs, if you hitted the dragon --- */

        /* Here it checks, if you gonna do a normal or a critical-strike to the dragon */

        /* If you get lucky and deal a crit-strike, this part of the code runs */
        if (critStrike) {

            /* Since you deal a crit strike, the damage gets multiplied by 2.5 */
            damageThisRound *= 2.5;

            /* A info to the user that he made a X dmg CRIT-strike */
            console.log("Gz, you hittet the Dragon with a " + (damageThisRound) + "dmg Crit-Strike!");

            /* Sum up the damage */
            totalDamage += damageThisRound;

        /* If you have just a lil bit luck, it will do a base-strike - and run this part of the code */
        } else {

            /* Another info for the user - but this time just a X dmg BASE-strike */
            console.log("Gz, you hittet the Dragon with a " + (damageThisRound) + "dmg Strike!");

            /* Aaaand sum the damage up again */
            totalDamage += damageThisRound;
        }

        /* ... but what if you dealt already more than 50 dmg to the dragon?
        the poor beast will die of his injuries. */
        if (totalDamage >= 50) {

            /* Again a info for the used, that he mastered to kill the dragon */
            console.log("Gz, you slayed the Dragon with a total of " + totalDamage + "dmg!"); 

            /* And obviously you have to set slaying to false, since the dragon is already dead */
            slaying = false;

        } else {
            /* otherwise, if the dragon has more or equal 1 HP left (out of 50),
            you keep fighting against it.  */

            /* re-calculating the dmg for the next round ... */
            damageThisRound = Math.floor(Math.random() * 5 + 1);

            /* ... aswell as the check if you gonna miss or hit the dragon ... */
            youHit = Math.floor(Math.random() * 2);
     
            /* ... and last but not least - re-calculate the chance to do a crit-strike */
            critStrike = Math.floor(Math.random() * 2);
        }
    } 

     /* --- Miss-Part / This code runs, if you missed the dragon --- */

    /* ... otherwise, if you missed, it will this part of the while-loop  */
    else {

        /* Here it checks, if you missed the dragon more than 10 times. */

        /* If you didn't miss the dragon 10 times yet, it will run this part of the code */
        if (miss < 10) {
            
            /* Basic log as information for the user, that he missed */
            console.log("You missed!");

            /* The BASE damage for the NEXT round gets calculated */
            damageThisRound = Math.floor(Math.random() * 5 + 1);

            /* Again the variable to check, if the user hitted the dragon or missed it. */
            youHit = Math.floor(Math.random() * 2);

            /* Then it kinda "flips the coin" to check, if the next strike is a normal,
            or a critical strike (as reminder: BASE-dmg * 2.5) */
            critStrike = Math.floor(Math.random() * 2);

            /* Increase the "miss"-counter by one */
            miss++;
        } else {

            /* Otherwise, this was your last miss - the dragon crushes you ... */
            console.log("You missed! The Dragon defeated you!");

            /* ... and so the slaying ends - you've been defeated */
            slaying = false;
        }
    }
}

That's pretty much all that you have to know about it :smiley:
Happy slaying :slight_smile:


#7