Blog Archive

Showing posts with label if else statement. Show all posts
Showing posts with label if else statement. Show all posts

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.

Tuesday, December 3, 2013

Giving Players Choices: Control Structures

In this post, we will learn how to use control structures to let our players choose what happens in our text game. Our text game will present players with two options to choose from, and will let them know the outcome of their choices.

Let’s begin.


Part 1: Using a Variable to Hold Players' Choices

  1. Run Code::Blocks.
  2. Choose “Open An Existing Project.”
  3. Find the project file for your text game (this file will have the name you chose for your project last time with a .cbp extension) and open it.
    This image shows the listing for a text game's project file. It is named "RedRidingHood.cbp"
    My text game's project file is named "RedRidingHood.cbp."
  4. Add a line after the end of the last cout statement in your text game.
  5. Create an int variable named "choice."
  6. int choice;
    
  7. Use a cout statement to present the player with a problem.
  8. cout << "A wild wolf appears! What will you do?" << endl;
    
  9. Use a cout statement to present the player with two numbered choices.
  10. cout << "1. Run from the wolf." << endl 
         << "2. Attempt to engage the wolf in meaningful dialogue." << endl;
    
  11. Use a cout statement to prompt the player for a choice.
  12. cout << "Enter 1 or 2: ";
    
  13. Use a cin statement to get the player's choice.
  14. cin >> choice;
    
  15. Choose "Build" -> "Build."
  16. Choose "Build" -> "Run", if no errors were generated in Step 10.

Part 2: Using a Selection Statement to Determine a Choice's Outcome


A control structure in C++ determines if a series of statements should be executed based on whether a condition is true or false. The control structure we will use is called a selection statement. If the condition is true, then the program executes the statements associated with the if portion of the selection statement (which means that the program will execute any statements between the brackets below the line with the condition). If the condition is false, then the program executes the statements below the else portion of the selection statement.The condition we will use for our control structure is the “choice” variable. If the “choice” variable holds 1, the program will execute statements that output the outcome of the first choice; if the “choice” variable holds 2, the program will execute statements that outputs the outcome of the second choice.


  1. Add a new line and write
  2. if (choice == 1)
    
  3. Add an opening brace on the next line. Code::Blocks will automatically add a closing brace for you two lines down.
  4. Use a cout statement to output the outcome of the first choice between the two braces.
  5. {
        cout << "You flee from the wolf." << endl
             << "The wolf wanders off dejectedly into the forest." << endl;
    }
    
  6. Add a new line below the closing brace and write
  7. else
    
  8. Add another opening brace and closing brace.
  9. Use a cout statement between the two braces to output the outcome of the second choice.
  10. {
        cout << "You speak with the wolf, who got lost in the forest looking for his friend, the huntsman." << endl
             << "You give the wolf directions and he thanks you profusely before continuing on his way." << endl;
    }
    
  11. Choose "Build" -> "Build."
  12. Choose "Build" -> "Run", if no errors were generated in Step 7.
We wrote an if portion of a selection statement in Step 1. The if portion of the selection statement contained the condition inside of a set of parentheses. The condition for our selection statement checked whether our “choice” variable held a value of 1 using a pair of equal signs. This pair of equal signs is called the equal to operator in C++. The equal to operator checks whether the value on the left is equal to the value on the right. If the value on the left is equal to the value on the right, then the program evaluates the condition as true. If the value on the left is not equal to the value on the right, then the program evaluates the condition to false. In the example from my Red Riding Hood game, if the player enters 1 then the condition will evaluate to true and the player will flee from the wolf. If the player enters 2 then the condition will evaluate to false and the player will speak with the wolf.


Part 3: Giving Players More Choices with Else If


Unfortunately, if players enter any number but 1 for their choice, then the condition will evaluate to false and the game will execute the statements associated with the else portion of the selection statement. If players choose another number, we want our game to output an outcome that makes sense. We can use an else if portion to the selection statement to provide players with more than two possible outcomes.
  1. Find your else statement and change it to
  2. else if (choice == 2)
    
  3. Add another line below the brackets for the else if statement, and write
  4. else
    
  5. Add another opening brace and closing brace.
  6. Use a cout statement between the two braces to output the outcome of an invalid choice.
  7. {
        cout << "You're not sure what to do, so you hide behind a tree until the wolf leaves." << endl;
    }
    
  8. Choose "Build" -> "Build."
  9. Choose "Build" -> "Run", if no errors were generated in Step 5.

In this control structure, the program checks the condition for the if portion of the selection statement first. If the condition for the if portion of the selection statement evaluates to false, then the program checks the condition for the else if statement. If the condition for the else if portion of the selection statement evaluates to false, then the program executes the statements associated with the else portion of the selection statement.


You now have an interactive text game that lets players make choices. If your text game generated any errors, I have the code from today’s text game in this Google Drive file so you can check your code against mine. In our next post, we will learn more about control structures, and how to use them to create branching storylines affected by players' choices.