Interactive Text Adventure
Result
Method
The first step in my porject was to get the user input using prompts and store them in variables ready for use throughout the story. When I first wrote this I used cin << which only reas the first word of the input. I kept writing 'ice cream' for food and wondered why it only used ice and then used cream for my next answer. After a little research and asking my Codecademy classmates for help, I found out about getline(cin, variable) which reads the whole line including spaces.
//Collecting variables from user
//colour
std::string colour;
std::cout << "Input a colour: ";
std::getline(std::cin, colour);
//weather
std::string weather;
std::cout << "Input a description of a day (cold, windy, etc.): ";
std::getline(std::cin, weather);
//animals and names
std::string animal1;
std::cout << "Input your first animal: ";
std::getline(std::cin, animal1);
std::string name1;
std::cout << "Name your first animal: ";
std::getline(std::cin, name1);
std::string animal2;
std::cout << "Input your second animal: ";
std::getline(std::cin, animal2);
The next part was to write the story itself, I knew at certain points in the story I had to give the user options which allowed for multiple outcomes.
//First fork in the story
int option1;
std::cout << "What does " << name1 << " decide to do? ";
std::cin >> option1;
//check for valid response
while(option1 != 1 && option1 !=2){
std::cout << "Invalid response. Try again: ";
std::cin >> option1;
}
Then each option needed its own outcome, so I used if and else if conditionals to create the different paths the story could take.
if (option1 == 1) { //bed
std::cout << "\n"<< name1 << " decided to go back to bed. \n";
std::cout << "While " << name1 << " slept, they had the most unusual dream. They were in a world where everything was made out of " << food << ".\n";
std::cout << "The first thing " << name1 << " did was take a massive bite out of the " << item << ".\n";
std::cout << name1 << " hears footsteps coming from behind. " << name1 << " turns around and screams as a " << size << " " << animal2 << " is running towards them trying to escape from the " << food << " monster! \n";
std::cout << "They both hide under a bridge and hear the monster stomp away.\n";
std::cout << name1 << " turns to the " << animal2 << " and introduces themself, the " << animal2 << " then introduces themself as " << name2 << ".\n";
std::cout << "Suddenly, " << name1 << "jolts awake and smile while thinking about their new friend " << name2 << " the " << animal2 <<".\nThe End.\n";
return 0;
} else { //outside
std::cout << "\n" << name1 << " puts on their favourite " << colour << " coat and visits a nature park.\n";
std::cout << "The nature park is very busy today and " << name1 << " doesn't like crowds so decided to go find a quiet place to read.\n";
std::cout << name1 << " sees two possible locations, the treehouse (1) or rent a boat and go on the lake (2).\n";
//fork number two in story
int option2;
std::cout << "Where does " << name1 << " decide to go? ";
std::cin >> option2;
This process of giving options and creating different outcomes was repeated throughout the story to make it as interactive as possible.
Another problem I encountered when getting my mother to run through the story was that she kept entering invalid options such as Y or N instead of 1 or 2. To fix this I added a while loop to check if the input was valid and if not, it would keep asking until a valid option was given.
//check if valid response
while(option2 != 1 && option2 !=2){
std::cout << "Invalid response. Try again: ";
std::cin >> option2;
}
After this everything worked perfectly and I was happy with how the story turned out.