For a school project, I am creating a text-based adventure game in C++. I have shown my code to my teacher and he is stating my code is not entirely logical in sequence and that I should make my if
statements inside functions because there are so many. I need help as to know how to make these changes.
Note that this is one function of the code, and not it's entirety.
void road() {
int choice2;
int choice3;
int event1;
system("cls");
cout << "You are on a road that heads west and east of your position." << endl;
cout << "Which way will you go?\n" << endl;
cout << "1. Go West" << endl;
cout << "2. Go East" << endl;
cout << "3. Wait for something to happen\n" << endl;
cin >> choice2;
cin.ignore();
system("cls");
if (choice2 == 1) {
int decision1;
cout << "You travel down the road, about only 100 metres and you encounter " << endl;
cout << "a giant spider with vicious poison coated fangs." << endl;
cout << "its hideous appearance causes your throat to dry and your knees to shake!" << endl;
cout << "What on earth will you do?\n\n" << endl;
cout << "1. Attempt to attack the spider with you sword." << endl;
cout << "2. Throw your sword in the off chance it might kill it." << endl;
cout << "3. RUN FOR YOUR LIFE!" << endl;
cin >> decision1;
cin.ignore();
system("cls");
if (decision1 == 1) {
cout << "You viscously swing your sword at the spiders general direction." << endl;
cout << "The swing was so great, your arms jolts out of place, \ncreating a surge of pain." << endl;
cout << "Your arm is now broken, and you fall to the ground in pain...." << endl;
cout << "The spider launches 3 metres straight into your body... \nWhat on earth is it doing?" << endl;
cout << "Oh My God! The spider is devouring everything...." << endl;
cout << "All that remained was bones of the once mobile adventurer " << name << endl;
system("pause");
gameover();
}
else if (decision1 == 2) {}
else if (decision1 == 3) {}
}
else if (choice2 == 2) {
cout << "After a mile walk, you arrive at an old brick house. " << endl;
cout << "You walk slowly inside." << endl;
cout << "The door slams behind you and the room lightens up." << endl;
cout << "What on earth is going on...?\n\n" << endl;
system("pause");
cout << "\nUnable to open the door, you look around for anything of use." << endl;
cout << "Nothing, not a single piece of furniture." << endl;
cout << "What will you do?\n" << endl;
cout << "1. Wait for someone to save you." << endl;
cout << "2. Or Wait for someone to save you." << endl;
cin >> choice3;
cin.ignore();
system("cls");
if (choice3 == 1) {
cout << "As you character relaxes his legs and begins to sit" << endl;
cout << "he leans against the wall, only to hit a hidden switch." << endl;
system("pause");
cout << "The floor beneath you disappears, and you start to plummet." << endl;
cout << "What a trap! You bloody stupid fool!\n\n?" << endl;
cout << "somehow you never reach the bottom....." << endl;
cout << "You die of water deprivation, after only 2 weeks of falling...." << endl;
system("pause");
gameover();
}
else if (choice3 == 2) {
system("cls");
cout << "After only 5 hours..." << endl;
cout << "The doors suddenly swoosh open." << endl;
cout << "You are free! " << endl;
system("pause");
cout << "You take your first steps into what is now moonlight," << endl;
cout << "and then suddenly pass out on the ground...." << endl;
cout << "For some peculiar reason or another, you are now /nback in the creepy dark and gloomy library. You swiftly escape its nostalgia. " << endl;
road();
}
}
else if (choice2 == 3) {
int random1;
cout << "After at least was seemed to be 4 hours " << endl;
cout << "a dragon swoops down from the heavens...." << endl;
cout << "and lands right in front of you!\n" << endl;
system("pause");
system("cls");
random1 = rand() % 2; // pseudo random generator
if (random1 == 0) {
int choice4;
cout << "Your character screams at the top of his lungs, " << endl;
cout << "this causes the dragon to immediately to bow in fear..." << endl;
cout << "It turns out dragons are very sensitive to hearing....." << endl;
system("pause");
cout << "\nIt seems the dragon is requesting you ride it!\n" << endl;
cout << "Will you ride it?\n" << endl;
cout << "1. Ride it" << endl;
cout << "2. Or Wait here." << endl;
cin >> choice4;
cin.ignore();
system("cls");
if (choice4 == 1){
Ending();
}
else if (choice4 == 2){
road();
}
}
else if (random1 == 1) {
cout << "Your character screams at the top of his lungs, " << endl;
cout << "eventually your breath gives out and you die because of lack of oxygen." << endl;
system("pause");
gameover();
}
}
}
Inform7
if you're interested in writing your own text adventures. \$\endgroup\$ – Eric Lippert May 13 '14 at 16:07