Part 1: Creating a While Loop That Gets Player Input
- Run Code::Blocks.
- Open the project file for your text game.
- Create an int variable called “validChoice”.
- Create a bool variable called “valid”.
- Set valid to false using an equals sign.
- Add a line below the two variables and write
- Use cout statements between the while loop's braces to give players a problem, two numbered choices, and a prompt for a response.
- Use a cin statement between the while loop's braces to get the player’s response and read it into “validChoice”.
int validChoice;
bool valid = false;
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;
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
- 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.
- Use a cout statement to output an outcome in the if portion of the selection statement.
- Set “valid” to true in the if portion of your selection statement.
- Use a cout statement to output an outcome in the else if portion of the selection statement.
- Set “valid” to true in the else if portion of your selection statement.
- 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.
- Choose "Build" -> "Build."
- Choose "Build" -> "Run", if no errors were generated in Step 7.
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 { } }
if (validChoice == 1) { cout << "A fairy appears!" << endl << "She thanks you for guarding her treasure." << endl << "You walk away feeling guilty." << endl; valid = true; }
else if (validChoice == 2) { cout << "You walk away, feeling righteous but " << endl << "a bit foolish." << endl; valid = true; }
else { cout << "You made a response in error. Try again." << endl; }
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