chrono and time
<chrono> is a replacement for <ctime>.
Don't worry about it. Use <ctime> and seed things with
srand(time(0));
.
Then get random numbers using one of the methods in the FAQ article I linked for you.
set
A 'set' is a "has a" container. You can answer questions like: does animals "have a" penguin?
This "has a" functionality is necessary for your program to function correctly. Without it you cannot determine whether or not the user entered the correct collection of animal names.
For example, suppose the user gave you the list of animals: dog, cat, snake, zebra, and tiger.
But the scrambled word uses only two of those animals.
For example, suppose that dog and cat were chosen from the list and scrambled together to form the word "codgta".
If the user thinks that the two animals represented are dog and zebra he would be wrong: Zebra is
not one of the animal names used to create the scramble.
So every iteration of your program must do several things:
1 - select N animals from the list
-
remember which animals were selected!
2 - scramble their names together
3 - ask the user to try to unscramble them
4 - get the user's response(s)
5 - compare the user's response to the names selected in step 1
Notice that there
multiple lists of animal names:
- the list obtained at the beginning of the program
- the sublist the program chooses randomly from the first list
- the sublist obtained from the user when he guesses what animals are in the second list
The trick, of course, is that the second list may be (dog, cat) and the user may enter (cat, dog). The two list the same animals, of course; hence the choice of using a set.
You don't really need the std::set to do this.
You could write a function that compares two lists to see if they contain the same animals in any order. A set is just a convenient Standard Library object.
1 2 3 4 5 6 7
|
bool is_subset( const vector<string>& set, const vector<string>& subset )
{
for each element in subset:
if element not found in set:
return false
return true
}
|
clearing previous loops
What would happen if you didn't start with an empty set of randomly chosen animals (and user responses) each time you play the game? Consider:
The first game: computer chooses (cat, dog) the first time. User correctly answers (cat, dog).
Then the computer chooses (zebra, dog) the second time. User correctly answers (zebra, dog), but computer is trying to look for (cat, dog, zebra).
Similar logical issues occur with the user response.
Hope this helps.