Let’s begin.
Part 1: Coding the Outer Selection Statement
- Run Code::Blocks
- Open the project file for your text game.
- Create two int variables named “firstChoice” and “secondChoice”.
- Use a cout statement to present the player with a problem.
- Use a cout statement to present the player with two numbered choices.
- Use a cout statement to prompt the player for their choice.
- Use a cin statement to get the player’s choice and read it into “firstChoice”.
- Add an if portion to the selection statement that checks whether “firstChoice” is equal to 1.
- Add an else if portion to the selection statement that checks whether “firstChoice” is equal to 2.
- Add an else portion to the selection statement.
- Use a cout statement between the braces for the else portion of the selection statement to present a default outcome. This cout statement will only output this outcome if the player enters a choice other than 1 or 2.
int firstChoice, secondChoice;
cout << "After you deal with the wolf, you set off to your grandmother's house." << endl << "You travel the path until you come to a fork in the road." << endl;
cout << "1. Take the left fork." << endl << "2. Take the right fork." << endl;
cout << "Enter 1 or 2: ";
cin >> firstChoice;
if (firstChoice == 1) { }
else if (firstChoice == 2) { }
else { }
else { cout << "You're not sure what to do." << endl << "You turn around and head back home." << endl; }
Next, we will nest inner selection statements within our outer selection statement. The inner selection statements will give our players different choices to make, depending on which options they chose for the outer selection statement.
Part 2: Coding the Inner Selection Statements
- Use a cout statement inside of the braces for the if portion of your selection statement to present your player with a problem.
- Use a cout statement to present your player with two numbered choices.
- Use a cout statement to prompt your player for their choice.
- Use a cin statement to get the player’s choice and read it into “secondChoice”.
- Add an if portion to the inner selection statement that checks whether “secondChoice” is equal to 1.
- Use a cout statement between the braces of the if portion to output the outcome for a choice of 1.
- Add an else portion to the inner selection statement.
- Use a cout statement between the braces of the else portion to output the outcome of a choice of 2.
- Repeat steps 1 through 8 inside of the braces for the else if portion of the outer selection statement. Use a different problem, a different set of choices, and different outcomes.
- Choose "Build" -> "Build."
- Choose "Build" -> "Run", if no errors were generated in Step 10.
cout << "You come upon a beautiful spring. You suddenly feel very thirsty." << endl << "Will you drink?" << endl;
cout << "1. Drink." << endl << "2. Resist the urge to drink." << endl;
cout << "Enter 1 or 2: ";
cin >> secondChoice;
if (secondChoice == 1) { }
{ cout << "You drink and feel refreshed." << endl << "You continue on your way, picking up your pace." << endl; }
else { }
{ cout << "Reluctantly, you leave without drinking." << endl << "You continue on your way, thirsty and cranky." << endl; }
else if (firstChoice == 2) //player chose to take the right fork { cout << "There's a cute little bunny frolicking in a clearing." << endl << "Will you pet it?" << endl; cout << "1. Pet the bunny." << endl << "2. Leave the bunny alone." << endl; cout << "Enter 1 or 2: "; cin >> secondChoice; if (secondChoice == 1) //player chose to pet the rabbit { cout << "The bunny you're petting turns out to be a wizard, " << endl << "who finds your petting offensive. He turns you into " << endl << "a newt." << endl; } else //player did not choose to pet the rabbit { cout << "You go on your way without petting the rabbit, but " << endl << "you know, somewhere in the back of your mind, that " << endl << "you will always wonder about what could have been." << endl; } }
Now, your text game’s story change as players make different choices. To make your text game’s story longer, you can place as many nested if statements inside of nested if statements as you need; experiment until you find something that works well for your text game.
If your code generated any errors, I've included my text game’s code in this Google Drive file, so you can check your text game’s code against mine. In our next tutorial, we will learn about how to give players more than one chance to input a valid choice with loops.
No comments:
Post a Comment