Code Your Own Adventure 2!


#1

adventure game 2 : More Roboust code with try again function included.

please feel free to ask anything you did not understand

...

/* try again function, i felt this to delare this first bcoz i was going to use it in game function*/

var tryAgain = function()
{
var input = prompt("would you like to play again enter YES or NO?").toUpperCase();
if(input === "YES")
{
game();
}
else if(input === "NO")
{
console.log("Thank you for playing,please visit again!");
}
else
{
alert("You have entered invalid option please enter YES or NO");
tryAgain();
}
};

// game function

var game = function()
{
var user = prompt("You're walking through the forest, minding your own business, and you run into a troll! Do you FIGHT him, PAY him, or RUN?").toUpperCase();

switch(user)
{
case 'FIGHT':
var strong = prompt("How courageous! Are you strong(YES or NO)?").toUpperCase();
var smart = prompt("Are you smart?").toUpperCase();

     if(strong === 'YES' || smart === 'YES')
     {
      console.log("You only need one of the two! You beat the troll--nice work!");
     } 
    else 
     {
       alert("You're not strong OR smart? Well, if you were smarter, you probably wouldn't                     have tried to fight a troll. You lose!");
       tryAgain();
     }

     break;

case 'PAY':
   var money = prompt("All right, we'll pay the troll. Do you have any money (YES or NO)?"                          ).toUpperCase();

  var dollars = prompt("Is your money in Troll Dollars?").toUpperCase();    

  if(money === 'YES' && dollars === 'YES') 
  {
      console.log("Great! You pay the troll and continue on your merry way.");
  } 
  else
  {
      alert("Dang! This troll only takes Troll Dollars. You get whomped!");
      tryAgain();
  }
  break;

case 'RUN':
     var fast = prompt("Let's book it! Are you fast (YES or  NO)?").toUpperCase();
     var headStart = prompt("Did you get a head start?").toUpperCase();

    if(fast === 'YES' || headStart === 'YES') 
    {
        console.log("You got away--barely! You live to stroll through the forest another day.");
    } 
    else
    {
        alert("You're not fast and you didn't get a head start? You never had a chance! The                         troll eats you.");
        tryAgain();
    }

   break;

default:
   alert("I didn't understand your choice. Hit Run and try again, this time picking FIGHT,                         PAY, or RUN!");
  tryAgain();

}
};
game();

...

please do give it a try...Regards....Kcoder111


#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.