Blog Archive

Friday, December 6, 2013

Validating Player Input: Loops

In this tutorial, we will learn how to give players more than one chance to input a response into our text games. When players enter a wrong number, our text games currently output an error message before quitting. Players may accidentally enter a wrong number while playing a text game. If we want players to be able to play through our text games even after they enter a wrong number, we should give them another opportunity to enter a correct response when they enter a wrong number. We can give players another opportunity to enter a correct response using a loop, which is a control structure that executes a series of statements until some condition is met. The loop we will use is called a while loop, and the condition will require that the player enters a correct response before the loop stops getting new responses from the player.

Part 1: Creating a While Loop That Gets Player Input


  1. Run Code::Blocks.
  2. Open the project file for your text game.
  3. Create an int variable called “validChoice”.
  4. int validChoice;
    
  5. Create a bool variable called “valid”.
  6. Set valid to false using an equals sign.
  7. bool valid = false;
    
  8. Add a line below the two variables and write
  9. while (valid == false)
    {
    
    }
    
  10. Use cout statements between the while loop's braces to give players a problem, two numbered choices, and a prompt for a response.
  11. {
        cout << "You encounter an unattended pot of gold." << endl
             << "1. Try to take the gold." << endl
             << "2. Leave the gold alone." << endl;
        cout << "Enter 1 or 2: ";
    }
    
  12. Use a cin statement between the while loop's braces to get the player’s response and read it into “validChoice”.
  13. cin >> validChoice;
    


We have a while loop now, but if we build and run our program we will encounter a problem called an infinite loop. An infinite loop happens when a loop’s condition is met and then never changes within the loop. Our while loop never changes “valid” to true, so our text game will never stop prompting the player for the a response, even if they input a correct response. The infinite loop in our text game can be fixed by creating a selection statement that changes “valid” to true if the player enters a correct response.

Part 2: Updating A While Loop’s Condition


  1. Create a selection statement with an if portion, and else if portion, and an else portion within the braces for the while loop. Make “validChoice” the condition for the selection statement.
  2. while (valid == false)
    {
        cout << "You encounter an unattended pot of gold." << endl
             << "1. Try to take the gold." << endl
             << "2. Leave the gold alone." << endl;
        cout << "Enter 1 or 2: ";
        cin >> validChoice;
        
        if (validChoice == 1)
        {
            
        }
        else if (validChoice == 2)
        {
            
        }
        else
        {
            
        }
    }
    
  3. Use a cout statement to output an outcome in the if portion of the selection statement.
  4. Set “valid” to true in the if portion of your selection statement.
  5. if (validChoice == 1)
    {
            cout << "A fairy appears!" << endl
                 << "She thanks you for guarding her treasure." << endl
                 << "You walk away feeling guilty." << endl;
            valid = true;
    }
    
  6. Use a cout statement to output an outcome in the else if portion of the selection statement.
  7. Set “valid” to true in the else if portion of your selection statement.
  8. else if (validChoice == 2)
    {
            cout << "You walk away, feeling righteous but " << endl
                 << "a bit foolish." << endl;
            valid = true;
    }
    
  9. Use a cout statement to output a message to the player in the else portion of the selection statement to let the player know that they entered a wrong response.
  10. else
    {
        cout << "You made a response in error. Try again." << endl;
    }
    
  11. Choose "Build" -> "Build."
  12. Choose "Build" -> "Run", if no errors were generated in Step 7.


Play your text game and try to input a few different numbers to see how the while loop works. The while loop should output an outcome for your choice if it’s correct, but it will repeat the prompt if you enter an incorrect response. You can place a while loop around any selection statement to give your players more than one chance to input a response.

I placed all of my text game’s selection statements inside of while loops that validate player input. My text game is in this Google Drive file, so you can check your code against mine if your text game generated any errors. In the next post, I will interview a student of game design who has advice about writing stories for games.

No comments:

Post a Comment