Blog Archive

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.


No comments:

Post a Comment