My CheckGuessValidity code

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;
}

@DevFingers

I really like the way you have designed your code.

You check for many things such as looking to see if the input from the user is anything other than letters. I do enjoy the way you’ve coded it, however, I feel that you could make the code less redundant by checking if the hidden word and guess have different sizes just in one statement such as this:

if (Guess.length() != getHiddenWordLength())
{
return EGuessStatus::Not_Correct_Length;
}

Also you could create a function that checks every index of the guess to see if any characters are not letters, rather than checking if they are numbers and checking if there is punctuation.

A simple Vector filled with the alphabet would allow you to check quite easily with a for loop that all characters in guess are letters.

Aside from that you have some great ideas
Keep it up!

~Tim

To add to Tim’s reply, some of the checks are redundant, esspecially the whole GuessHasControlPunctuationOrRunes and GuessHasNumbers function as that could just use isalpha since std::iscntrl(), std::isdigit(), std::ispunct() and std::isspace() will return zero i.e isalpha('2'); isalpha(' '); etc will all return false.

if (GuessLength == 0)
{
    ResultStatus = EWordStatus::Empty_Guess;
}

Should probably be

if (Guess.empty())
{
    ResultStatus = EWordStatus::Empty_Guess;
}

Also is there really any point to having > and < checks? What’s wrong with != ?

1 Like

Using GuessLength enforces only a single call to Guess.Length() because of my need to test length twice.
The < & > were specifically chosen for more resolution on the error. I want to know if the user is guessing too long or too short. There’s nothing wrong with using length != guess length; it just didn’t suit what my requirements were.

1 Like

Tim - I did update to use the hash table as in the tutorial. It’s by far the better solution.

1 Like

Right okay, fair enough I guess. I would have just printed the length of both for the error message for an IncorrectLength result.

If you wanted to check specifically then that is absolutely fine, I assumed otherwise and just thought I’d inform you

using != is far more neater than what I showed
I don’t know why I didn’t just type that instead of a big if statement the way I did.
I will update my reply now, thanks for pointing it out

In general though you may need to use both > and < in an if statement rather than using != later down the track usually when comparing more than two variables

No, my point was that the consumer of this function, a logic or behavior block, could respond differently if the response of the function allows it to. If I want the rest of the application to be able to respond to different user scenarios that are important in its design, then these ‘medium level’ functions have to provide the required resolution for higher level actions to occur. If it’s just ‘!=’, then that cripples logic higher up. It’s not so important here - it’s just a word comparison - and I wanted that amount of resolution. But on a vector comparison, it really becomes important to give proper feedback to the caller (view point of the function) to be able to react correctly.

Privacy & Terms