Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include "typing.h"
/**This bool function is for testing if the
* user's input matches the given word. */
void typing_input(int &wordCorrect)
{
while(wordCorrect == 0) //This is a problem with multithreading that I can't really figure out.
{ //When one thread is waiting for input, the while loop will not reach it's end and check if the time is up. The current solution is
std::string wordAttack; //to ask the player for any input for the while loop to continue and check that the time is up, thus ending the function and the thread.
std::string word;
word = "example";
std::cout<<"Please type in this word before time is up: "<<word<<std::endl;
wordAttack = inputTest<std::string>();
if ( word == wordAttack)
{
wordCorrect = 1;
return;
}
}
return;
}
/**This function is for testing if the user
* inputted the word before time is up. */
void typing_countdown( int &countdownEnd, int &wordCorrect )
{
for(int i=0; i<5; i++)
{
if( wordCorrect == 0) //If the player has not yet inputted the correct word
{
Sleep(1000); //The loop will wait a second
}
if (wordCorrect == 1) //If the player has already inputted the correct word
return; //The function will end before changing the countdownEnd variable
}
countdownEnd = 1; //After the for loop sets countdownEnd to 1, meaning that time is up.
wordCorrect = 2; //Sets the wordCorrect to 2, meaning it was not guessed in the allocated time, making the typing_input() loop terminate.
std::cout<<"Time's up. Input anything to continue."<<std::endl;
}
/** This function uses multithreading to run typing_input()
* and typing_countdown() simultanously. */
bool typing_result()
{
int wordCorrect = 0; //Checks if the correct word was inputted. 0 means no, 1 means yes, 2 means time for changing has ran out
int countdownEnd = 0; //Checks if the time is up.
std::thread input_thread(typing_input, std::ref(wordCorrect)); // This thread is for the typing_input function, which takes no arguments.
std::thread countdown_thread(typing_countdown, std::ref(countdownEnd), std::ref(wordCorrect)); //std::ref is used as a wrapper to pass a reference to the thread, as per https://stackoverflow.com/questions/61985888/why-the-compiler-complains-that-stdthread-arguments-must-be-invocable-after-co
countdown_thread.join(); //The function will wait for the typing_countdown() to end.
if(countdownEnd == 1) //If countdown has reached it's end.
{
input_thread.join(); //Deallocates memory from the input_thread, as it's no longer needed.
return false; //typing_result returns that the time is up.
}
else
{
input_thread.join(); //Waits for the input_thread to finish
std::cout<<"Correct word!"<<std::endl;
return true; //typing_result returns that the word was typed in correctly.
}
}