Blog Archive

Thursday, December 5, 2013

Giving Players Choices: Nested If Statements

In this tutorial, we will learn how to create branching storylines that reflect your players' choices. We can create branching storylines by placing selection statements inside of other selection statements; this allows the story to change in ways that depend on the choices that players made earlier in the game. Selection statements placed within other selection statements are called nested if statements.

Let’s begin.

Part 1: Coding the Outer Selection Statement

  1. Run Code::Blocks
  2. Open the project file for your text game.
  3. Create two int variables named “firstChoice” and “secondChoice”.
  4. int firstChoice, secondChoice;
    
  5. Use a cout statement to present the player with a problem.
  6. 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;
    
  7. Use a cout statement to present the player with two numbered choices.
  8. cout << "1. Take the left fork." << endl
         << "2. Take the right fork." << endl;
    
  9. Use a cout statement to prompt the player for their choice.
  10. cout << "Enter 1 or 2: ";
    
  11. Use a cin statement to get the player’s choice and read it into “firstChoice”.
  12. cin >> firstChoice;
    
  13. Add an if portion to the selection statement that checks whether “firstChoice” is equal to 1.
  14. if (firstChoice == 1)
    {
        
    }
    
  15. Add an else if portion to the selection statement that checks whether “firstChoice” is equal to 2.
  16. else if (firstChoice == 2)
    {
        
    }
    
  17. Add an else portion to the selection statement.
  18. else
    {
        
    }
    
  19. 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.
  20. 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

  1. Use a cout statement inside of the braces for the if portion of your selection statement to present your player with a problem.
  2. cout << "You come upon a beautiful spring. You suddenly feel very thirsty." << endl
             << "Will you drink?" << endl;
    
  3. Use a cout statement to present your player with two numbered choices.
  4. 
    cout << "1. Drink." << endl
             << "2. Resist the urge to drink." << endl;
    
  5. Use a cout statement to prompt your player for their choice.
  6. cout << "Enter 1 or 2: ";
    
  7. Use a cin statement to get the player’s choice and read it into “secondChoice”.
  8. cin >> secondChoice;
    
  9. Add an if portion to the inner selection statement that checks whether “secondChoice” is equal to 1.
  10. 
    if (secondChoice == 1)
    {
            
    }
    
  11. Use a cout statement between the braces of the if portion to output the outcome for a choice of 1.
  12. {
           cout << "You drink and feel refreshed." << endl
                << "You continue on your way, picking up your pace." << endl;
    }
    
  13. Add an else portion to the inner selection statement.
  14. else
    {
    
    }
    
  15. Use a cout statement between the braces of the else portion to output the outcome of a choice of 2.
  16. {
       cout << "Reluctantly, you leave without drinking." << endl
            << "You continue on your way, thirsty and cranky." << endl;
    }
    
  17. 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.
  18. 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;
        }
    }
    
  19. Choose "Build" -> "Build."
  20. Choose "Build" -> "Run", if no errors were generated in Step 10.

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