I expanded the game a bit


#1

I worked way too long and hard on this to not have it played by at least one other person.

//adjusted coinflipper
var coinflip = function() {
    var random = Math.random()
    if ( random < 0.33 ) {
        return random = false;
    }
    else {
        return random = true;
    }
}  
//console.log(coinflip());

//variables
var yourTurn = true
var slaying = true
var youHit = coinflip()
var damageThisRound = Math.floor(Math.random() * 5 + 4 )
var yourHP = 15
var yourMP = 15
var magicDamage = Math.floor(Math.random() * 6 + 10 )

var dragonHit = coinflip()
var dragonRoundDamage = Math.floor(Math.random() * 7 + 3 )
var dragonHP = 30
var dragonMana = 20
var dragonDeath = false

var potionPurseHP = 3
var potionPurseMP = 1
var moneyPouch = 95

//start setup
var runChoice = false
var start = prompt("You encounter a dragon! Will you attack or attempt to run? (attack/run/magic)")

while ( start !== "run" && start !== "attack" && start !== "magic") {
    start = prompt("That is not a valid choice. What will you do? (attack/run/magic)")
}

if ( start === "run" ) {
    runChoice = true;
    var runChance = Math.floor(Math.random() * 2 )
    if (runChance) {
        console.log("You got away safely!");
        slaying = false;
    }
    else {
        console.log("You couldn't escape, and the dragon starts to attack!");
    } 
}
else if ( start === "magic" ) {
    magic = true;
}

//fighting
while(slaying) {
    if (runChoice === false) {
//your hit
//magic
        if (magic) {
            console.log("You launch a firebolt at the dragon!");
            console.log("It burns the dragon for " + magicDamage + " damage!");
            dragonHP -= magicDamage
            yourMP -= 10
            console.log("You have " + yourMP + " mana left.")
            magic = false
            if ( dragonHP > 0 ) {
                console.log("The dragon has " + dragonHP + " hitpoints left!");
            }
            else {
                console.log("The dragon burnt to death!");
                console.log("_____________")
                slaying = false;
                yourTurn = false;
                dragonDeath = true;
            }
        }
//attack hits
        else if (youHit) {
            console.log("You hit the dragon for " + damageThisRound + " damage!");
            dragonHP -= damageThisRound;
            if ( dragonHP > 0 ) {
                console.log("The dragon has " + dragonHP + " hitpoints left.");
            }
            else {
                console.log("The dragon has no hitpoints left!");
                console.log("You defeated the dragon!");
                console.log("_____________")
                slaying = false;
                yourTurn = false;
                dragonDeath = true;
            }
        }
//attack misses        
        else {
            console.log("You missed!");
        }
        youHit = coinflip()
        damageThisRound = Math.floor(Math.random() * 4 + 4 );
        magicDamage = Math.floor(Math.random() * 6 + 10 );
        
    }
    else {
        runChoice = false;
    }
    
//dragon hit
    if (slaying) {
        if (dragonHit) {
            console.log("The dragon hits you for " + dragonRoundDamage + " damage!");
            yourHP -= dragonRoundDamage;
            if ( yourHP > 0 ) {
                console.log("You have " + yourHP + " hitpoints left.");
                console.log("_____________")
            }
            else {
                console.log("You have no hitpoints left!");
                console.log("The dragon has defeated you!");
                console.log("_____________")
                slaying = false;
                yourTurn = false;
            }
        }
        else {
            console.log("The dragon missed!")
            console.log("_____________")
        }
        dragonHit = coinflip()
        dragonRoundDamage = Math.floor(Math.random() * 6 + 3 );
    }
//next turn setup
    if ( yourHP >= 1 && dragonHP >= 1 ) {
        yourTurn = true;
    }
    while (yourTurn) {
            if (slaying) {
                var choice = prompt("What will you do? (attack/run/potion/magic)")
                while (choice !== "attack" && choice !== "run" && choice !== "potion" && choice !== "magic") {
                choice = prompt("That is not a valid choice. What will you do? (attack/run/potion/magic)")
            }            
//attacking option           
            if ( choice === "attack" ) {
                console.log("You strike again!");
                yourTurn = false
            }
//running option            
            else if ( choice === "run" ) {
                runChoice = true
                var runChance = Math.floor(Math.random() * 2 )
                if (runChance) {
                    console.log("You got away safely!");
                    slaying = false;
                }
                else {
                    console.log("You couldn't escape, and the dragon continues its attack!");
                }
                yourTurn = false;
            }
//healing option
            else if ( choice === "potion" ) {
                potionChoice = prompt("Which potion do you take? (hp/mp)")
                if ( potionChoice === "hp" ) {
                    if ( potionPurseHP > 0 ) {
                        yourHP += 5;
                        if ( yourHP > 15 ) {
                            yourHP = 15;
                        }
                    console.log("You drink a healing potion and restore your hitpoints to " + yourHP + ".");
                    potionPurseHP -= 1;
                    console.log(potionPurseHP + " health potions remain in your purse.");
                    }
                    else {
                        console.log("You don't have any more potions!");
                    }
                }
                else if ( potionChoice === "mp" ) {
                    if ( potionPurseMP > 0 ) {
                        yourMP += 5;
                        if ( yourMP > 15 ) {
                            yourMP = 15;
                        }
                    console.log("Your drink a mana potion and restore your mana to " + yourMP + ".")
                    potionPurseMP -= 1;
                    console.log(potionPurseMP + " mana potions remain in your purse.");
                    }
                    else {
                        console.log("You don't have any more potions!");
                    }
                }
            }                        
//magic option
            else if ( choice === "magic" ) {
                if ( yourMP > 9 ) {
                    magic = true;
                    yourTurn = false;
                }
                else {
                    console.log("You don't have enough mana to do that!")
                }
            }
        }
    }
}
//after
if (dragonDeath && runChoice === false) {
    var coinDrop = Math.floor(Math.random() * 50 + 100 )
    console.log("The dragon dropped " + coinDrop + " gold coins!")
    moneyPouch += coinDrop
}
else if (dragonDeath === false && runChoice === false) {
    console.log("Your journey is over.")
}

If anyone has some general critique on how to improve the code that would be nice, like things that could've been written down in a shorter or more efficient way (which is most likely everything). It's built around the original code of the assignment, so it might not be very logically structured at some points.


#2

I applaud this effort; it puts my code to shame. Bravo!


#3

I played your game, and I really liked it! It's very good.

P.S.
I think you should add a way to spend your money at the end of the game


#4

I enjoyed that.. like for real.. I'm still at 2/6 so your code was overwhelming, but it was fun.


#5

Very Nice although some Minor Issues

Minor Issues:

  1. If you go attack at first you will have a refrence error. Check your code again.
  2. We Should have more Mana Potions :stuck_out_tongue_winking_eye:

#6

I had the same problem, if I attacked first I got a reference error.
All you need to do is state that magic = false where it lists all the other variables. That made it work just fine.


#7

I tried to make some changes to this game, and I can't figure out what's wrong with it.

//hero variables  *****
var heroTurn = true;
var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 4 );
var heroHP = 20;

//dragon variables *******
var dragonHit = Math.floor(Math.random() * 2 - 0.1);
var dragonRoundDamage = Math.floor(Math.random() * 7 + 3 );
var dragonHP = 30;
var dragonDeath = false;



//setup *******

var runChoice = false
var start = prompt("You enter the dark chamber and are met with the reason for your journey.  A dragon!  What will you do? (attack/run")

while ( start !== "attack" && start !== "run" {
    start = prompt("Quit messing around!  Choose your action: (attack/run)");
}

if ( start === "run" ) {
    runChoice = true;
    var choice = prompt("Are you sure you want to leave this battle and end the game?  (yes/no)");
    if ( choice === "yes") {
        console.log("You coward! Go home to your momma and cry!");
        slaying = false;
    else {
        console.log("That's the spirit!");
        yourTurn = true;
        }
    }
} 



//slaying  ******
while(slaying) {  
    if (youHit) {
        console.log("You've made contact!");
        totalDamage += damageThisRound;
        dragonHP -= damageThisRound;
        if ( dragonHP > 20 ) {
            console.log("The blade runs along the surface of the dragon's tough hide but fails to break through.");
            console.log("Dragon HP: " + dragonHP);
        }
        else if (20 > dragonHP > 10) {
            console.log("The axe sinks into the thick dragon scales but barely scathes the fiery core.");
            console.log("Dragon HP: " + dragonHP);
        }
        else if (dragonHP < 10) {
            console.log("The axe pierces through the thick dragon hide and slices the beast's fiery core.");
            console.log("Dragon HP: " + dragonHP);
        }
    else {
        console.log("Your final blow crushes the dragon's hide and completely severs its fire core!  The dragon tumbles to the ground as fire erupts from its eyes, throat, and other unheavenly orifices.");
        console.log("You did it! You've slain the dragon!");
        console.log("________________________")
        slaying = false;
        heroTurn = false;
        dragonDeath = true;
        }
    }
    
//attack misses  *******       
    else {
        console.log("Your attack missed!");
        youHit = Math.floor(Math.random() * 2);
        damageThisRound = Math.floor(Math.random() * 4 + 4 );  
    }
       
    
//dragon hit   *********
    if (slaying) {
        if (dragonHit) {
            console.log("The dragon sends razor sharp talons your way, leaving gashes in your mortal body.");
            console.log("It dealt a good" + dragonRoundDamage + " damage.");
            heroHP -= dragonRoundDamage;
            if ( heroHP > 0 ) {
                console.log("Hero HP: " + heroHP);
                console.log("________________________")
            }
            else {
                console.log("The blood gushes from your wounds without ceasing!");
                console.log("You challenged a God and emergered inferior.  As you bleed to death in agony, you cry out for your mother, who is currently being bedded by the local baker - who knew!");
                console.log("________________________");
                console.log("GAME OVER");
                slaying = false;
                heroTurn = false;
            }
        }
//dragon miss *********      
        else {
            console.log("The dragon lunges at you, and misses!")
            console.log("________________________")
        }
    dragonHit = coinflip()Math.floor(Math.random() * 2 - 0.1);
    dragonRoundDamage = Math.floor(Math.random() * 6 + 3 );
}

//next turn setup *******
if ( heroHP >= 1 && dragonHP >= 1 ) {
    heroTurn = true;
}

while (heroTurn) {
    if (slaying) {
        var choice = prompt("What will you do? (attack/run)")
        while (choice !== "attack" && choice !== "run") {
            choice = prompt("Quit messing around!  Choose your action: (attack/run)")
        }            
//attack           
if ( choice === "attack" ) {
    console.log("You swing your Hildebard Axe!");
    heroTurn = false
}
//run            
else if ( choice === "run" ) {
    choice = prompt("Are you sure you want to leave this battle and end the game?");
    if ( choice === "yes") {
        runChoice = true
        console.log("You coward! Go home to your momma and cry!");
        slaying = false;
    }
    else {
        console.log("That's the spirit!");
        }
        yourTurn = true;
    }
}
}

#8

After reading your interest in spending the money, I made this from scratch. https://www.dropbox.com/sh/yoxn3tis03qc7vk/AAASIKShMQYBLoeZfoH5TPP7a?dl=0