Pseudocode for a game

closed account (L6Rz8vqX)
Hi!

The game is a guessing game. Computer outputs a riddle, player inputs the answer.

My question is, should I use conditional statements or create a loop for this game?


This is what I have so far by using conditional statements:

1. Output riddle #1
2. Print the message "Type the answer and press enter"
3. Get input
4. Print the result
5. If input = answer #1
Print the message "Great job! You get to guess the next riddle."
Else
6. Print the message "You lose!"
7. Stop



My problem is, how to incorporate more than one riddle into this? If I just type the following steps out as many times as the amount of the riddles, then how do I make sure that each time the player guesses and loses, the game will end and not continue on with the next riddle?

I'll appreciate any hints or help!
Thank you!
Last edited on
I would use an array of strings and then a random number to selected a riddle. Pseudocode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream> //For input/output
#include <string> //To use strings for the riddles 
#include <random> //Header with lots of good random functions. 
#include <ctime> //To seed the random number with.
std::string riddles[5]; //Let's say there are five riddles. 
int i = 0; 

riddles[0] = "I become more and more wet as a dry, what am I?"; //Example riddle
//Rest of riddles. 

srand(time(0)); //Set the random number seed to the current time. Do this ONCE. 

while(player has not lost the game)
{
    i = rand() % 5; //Choose a random riddle. 
    //Display riddle
    cout << riddles[i];   
   //Get the player to guess and stuff. 
}
You can store each riddle in an array, then in a loop repeat the same steps for each index of the array. In very basic form it would look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string riddle [10];
string answear [10];
riddle[0] = "question 1";
riddle[1] = "question 2";
...
answear[0] = "answear 1";
answear[1] = "answear 2";
...

for(int i = 0; i < 10; i++)
{
    askQuestion (riddle[i]);
    cin >> myAnswear;
    if(myAnswear != answear[i]
    {
        doSomething();
    }
    else doSomethingElse();
}


You may also create a struct Riddle that would store a pair of question + answear and create a single array of say 10 Riddles instead of separate arrays of questions and answears.
Last edited on
closed account (L6Rz8vqX)
Thank you! That really helped!
Topic archived. No new replies allowed.