Dragon Slayer Story Game


#1

So I got a little ambitious with the dragon game, and after hours of trying to figure out how get it where I wanted it, it's finally here!

var youHit = Math.floor(Math.random() * 2);
var dragHit = Math.floor(Math.random() * 2);
var yourHealth = 8;
var dragHealth = 8;
var newYouHealth = 0;
var newDragHealth = 0;

var slay = function(a, b, c, d) {
    var slaying = true;
    var youDam = Math.floor(Math.random() * 5 + 1);
    var dragDam = Math.floor(Math.random() * 5 + 1);
    var yourName = prompt("What is your name, proud champion?");
    while(slaying) {
        if (a + b === 2) {
            console.log("A simultaneous blow! You hit the dragon for " + youDam + " damage, but is struck at the same moment for " + dragDam + " damage");
            c -= dragDam;
            d -= youDam;
            console.log(c, d);
            if (c + d === 0) {
                console.log("You have slain the dragon, but your wounds were too great. The king and all his courtiers will toast to " + yourName + "'s memory");
                slaying = false;
            } else if (c > d === 0) {
                console.log("You have slain the dragon and survived! Prepare yourself for a mighty feast, befitting your heroism");
                slaying = false;
            } else if (c > d === 0) {
                console.log(yourName + " came to its lair seeking glory, but all you found was blood and ashes. Perhaps your successor can finish the job.");
                slaying = false;
            } else {
                console.log("You both yet live! The battle continues...");
                a = Math.floor(Math.random() * 2);
                b = Math.floor(Math.random() * 2);
            }
        } else if (a > b) {
            alert("You struck the dragon for " + youDam + " damage while avoiding its searing roar. Bravo!");
            d -= youDam;
            console.log(c, d);
            if (d >= 1) {
                console.log("The foul beast yet lives! Remain vigilant!");
                a = Math.floor(Math.random() * 2);
                b = Math.floor(Math.random() * 2);
            } else {
                console.log("The vile creature is slain, and nary a scratch on you! Your rewards will be great, and your glory everlasting!");
                slaying = false;
            }
        } else if (a < b) {
            console.log("The dragon's flame engulfs you before you can lift your sword! A searing pain brings you to your knees as it deals you " +  dragDam + " damage!");
            c -= dragDam;
            console.log(c, d);
            if (c >= 1) {
                console.log("Your armor fuses to your skin and your blood boils, but you stray not from your purpose! Advance! Advance!");
                a = Math.floor(Math.random() * 2);
                b = Math.floor(Math.random() * 2);
            } else {
                console.log("You came for glory, but all you found was blood and ashes. Perhaps another poor fool will come to collect your bones");
                slaying = false
            }
        } else {
            console.log("You avoid the dragon's fiery breath, but as you close in to strike, it rears up on its hind legs and tears at you with its razor claws. You barely manage to deflect it with your shield as you grip your sword tightly and move to re-engage");
            a = Math.floor(Math.random() * 2);
            b = Math.floor(Math.random() * 2);
        }
    }
    console.log(c, d);
    if (c > 0) {
        alert("Three cheers for " + yourName + "!");
    } else {
        alert("An unmarked grave in a dragon's den. Hardly a fitting end for one of your potential");
    }
};

slay(youHit, dragHit, yourHealth, dragHealth)

Don't mind the grammatical errors in the strings, I started changing it up and then decided not to, but I'm too tired to perfect it.
Does anyone have improvements or suggestions? I'm very new to this, and I'd appreciate any input on how to make my code neater or more intuitive.


#2

Hi, I don't see how you incorporated var newYouHealth or newDragHealth


#3

Nice! I like the simultaneous hits factor. Very cool.

I'm new to this as well, but when I get a chance, I'll look through your code and try to give you some feedback.

I just finished up a similar, yet not quite as ambitious version. Check it out!

var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var dragonAttack = Math.floor(Math.random() * 5 + 1);
var yourHealth = 10;
var dragonHealth = 10;

while(slaying) {
    if (youHit) {
        dragonHealth -= damageThisRound;
        console.log("You hit the dragon and did " + damageThisRound + " damage.");
        if (dragonHealth <= 0) {
            console.log("The dragon has no more HP left! You win!");
            slaying = false;
            console.log(" ");
        } else {
            console.log("The dragon has " + dragonHealth + "HP left.");
            console.log("You strike again!");
            var youHit = Math.floor(Math.random() * 2);
            var damageThisRound = Math.floor(Math.random() * 5 + 1);
        }
    } else {
        yourHealth -= dragonAttack;
        console.log("You missed! The dragon countered and did " + dragonAttack + " damage.");
        if (yourHealth <= 0) {
            console.log("You have no more HP left! You are dead.");
            slaying = false;
            console.log(" ");
        } else {
            console.log("You have " + yourHealth + "HP left.");
            console.log("Keep slaying!");
            var youHit = Math.floor(Math.random() * 2);
            var dragonAttack = Math.floor(Math.random() * 5 + 1);
        }
    }
}

Let me know if you have any feedback for mine as well!

--Blaine


#4

missing ; after false

missing ; after the )

nice scripting :slight_smile: