-
-
Save codecademydev/a67f5652c8684d74d067a846c0d154af to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
int main() { | |
std::string choice1; | |
std::string choice2; | |
int choice3 = 0; | |
bool hungry = false; | |
int apples = 5; | |
int eat = 0; | |
std::cout << "You're walking down a path.\n"; | |
std::cout << "You come across a fork in the path. Do you turn [l]eft or [r]ight? \n"; | |
while (choice1 != "r" && choice1 != "l") { | |
std::cout << "[l] / [r]: "; | |
std::cin >> choice1; | |
} | |
if (choice1 == "r") { | |
std::cout << "You turn right."; | |
} else if (choice1 == "l") { | |
std::cout << "You turn left.\n"; | |
} | |
std::cout << "You continue walking down the path...\n"; | |
std::cout << "Walking...\n"; | |
std::cout << "Walking...\n"; | |
while (!hungry) { | |
std::cout << "Are you hungry yet?\n"; | |
std::cout << "[y] / [n]: "; | |
std::cin >> choice2; | |
if (choice2 == "y") { | |
hungry = true; | |
} | |
if (choice2 == "n") { | |
std::cout << "You walk down the path some more...\n"; | |
} | |
} | |
std::cout << "Luckily you have " << apples << " apples in your bag.\n"; | |
std::cout << "How many apples do you eat?.\n"; | |
std::cout << ""; | |
std::cin >> eat; | |
apples = apples - eat; | |
if (apples < 0) { | |
std::cout << "You've somehow eaten more apples that you have. Congrats, you've won the game!\n"; | |
return 0; | |
} else { | |
std::cout << "You eat " << eat << " apples. Yummy!\n"; | |
std::cout << "You have " << apples << " apples left.\n\n"; | |
} | |
std::cout << "You continue walking down the path...\n"; | |
std::cout << "Walking...\n"; | |
std::cout << "Walking...\n\n"; | |
std::cout << "A troll steps out in front of you. It looks ready to eat you. What do you do?\n"; | |
while (choice3 < 1 || choice3 > 3) { | |
std::cout << "[1] fight\n[2] flight\n[3] freeze\n"; | |
std::cout << "\n"; | |
std::cin >> choice3; | |
} | |
switch (choice3) { | |
case 1 : // fight | |
std::cout << "You decide the fight the troll. It beats you to a pulp and eats you.\n"; | |
std::cout << "You died."; | |
return 0; | |
case 2 : // flight | |
std::cout << "You turn tail like a coward and start running away back up the path.\n"; | |
std::cout << "You trip on a fallen branch and fall over. You hit your head on a rock.\n"; | |
std::cout << "You died."; | |
return 0; | |
case 3 : // freeze | |
std::cout << "You stand there, frozen in place.\n"; | |
std::cout << "The troll walks up to you, kills you, and eats you.\n"; | |
std::cout << "You died."; | |
return 0; | |
default : | |
std::cout << "You died."; | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment