Let me know what you think!
EWordStatus FBullCowGame::CheckGuessValidity(FString Guess) const
{
EWordStatus ResultStatus = EWordStatus::OK;
int32 GuessLength = (int32)Guess.length();
if (GuessLength == 0)
{
ResultStatus = EWordStatus::Empty_Guess;
}else if (GuessLength < GetHidenWordLength())
{
ResultStatus = EWordStatus::Length_Too_Short;
}else if (GuessLength > GetHidenWordLength())
{
ResultStatus = EWordStatus::Length_Too_Long;
}else if(std::find_if(Guess.begin(), Guess.end(), ::isdigit) != Guess.end())
{
ResultStatus = EWordStatus::Numbers_In_Guess;
}else if(GuessHasControlPunctuationOrRunes(Guess))
{
ResultStatus = EWordStatus::Non_Characters_In_Guess;
}else if(GuessIsIsogram(Guess) == false) // or if(!GuessIsIsogram(Guess))
{
ResultStatus = EWordStatus::Not_Isogram;
}
return ResultStatus;
}
And supporting functions:
// Check if numbers are in string
bool FBullCowGame::GuessHasNumbers(FString Guess) const
{
return Guess.find_first_of("0123456789") != std::string::npos;
}
// Check if any non characters are present
bool FBullCowGame::GuessHasControlPunctuationOrRunes(FString Guess) const
{
bool result = false;
result = (std::find_if(Guess.begin(), Guess.end(), ::iscntrl ) != Guess.end());
if(!result)
{
result = std::find_if(Guess.begin(), Guess.end(), ::ispunct ) != Guess.end();
}
if(!result)
{
result = std::find_if(Guess.begin(), Guess.end(), ::isrune ) != Guess.end();
}
if(!result)
{
result = std::find_if(Guess.begin(), Guess.end(), ::isspace ) != Guess.end();
}
return result;
}
// Check if guess is an isogram
// Sort the characters, and if any of the same characters
// are beside each other, then not isogram
bool FBullCowGame::GuessIsIsogram(FString Guess) const
{
std::string SortedGuess = Guess;
std::sort(SortedGuess.begin(), SortedGuess.end());
char LastChar = 32;
for(int32 i=0;i<SortedGuess.length();i++)
{
if(SortedGuess[i] == LastChar)
{
return false;
}
LastChar = SortedGuess[i];
}
return true;
}