I am making a hangman game. I have a vector<string> words
that consists of 2455 words and a function called valid_word()
that verifies that the randomly selected word matches the level of difficulty that the player has selected. Like for easy level the word is <=6, for a hard level the word is <= 8 and so on.
Sometimes this takes a long time. First a random_number
is generated, then a words.at(random_number)
is picked, and then it is verified for the words.at(random_number).length()
. Depending upon the level and if it doesn't qualify to be a valid word then this process is repeated.
This process most of the time takes at least 15-20 seconds. So I created a function that displays a loading animation at the console as below:
void print_loading() {
std::cout << '-' << std::flush;
for (;;) {
sleep(1);
std::cout << "\b\\" << std::flush;
sleep(1);
std::cout << "\b|" << std::flush;
sleep(1);
std::cout << "\b/" << std::flush;
sleep(1);
std::cout << "\b-" << std::flush;
}
}
How can I make this function to run only until my valid_word
has been generated?