My partial code code from early return implemented an isogram check as well

Just showing my partial code Process Guess;

the isogram was not shown yet in class #63 (early returns) but i went a head and implement a simple check of characters; as string is an array of characters , i used memberfunction GetCharArray() to access the array where string stores the characters and used a range base for loop to check the current character is equal to the next character by using pointer arithmetic. There is also alternative class for loop too instead of range based. I hope this is OK. I will see in the next sections. Great class :slight_smile:)

void UBullCowCartridge::ProcessGuess(const FString& Input)
{
if (Input == HiddenWord)
{
PrintLine(TEXT(“You win…”));
EndGame();
return;
}

if (IsIsogram(Input))
{
    PrintLine(TEXT("No repeating letters, guess again"));
    return;
}


if (Input.Len() != HiddenWord.Len())
{
    PrintLine(TEXT("The hidden word is %i letters long"), HiddenWord.Len());
    PrintLine(TEXT("Sorry try guessing again! \n You have %i lives"), Lives);
    // PrintLine(FString::Printf(TEXT("The hidden word is %2d characters long...\nTry Again.."), HiddenWord.Len()));
    return;
}

PrintLine(TEXT("Lost a life"));
--Lives;

if (Lives <=0)
{
ClearScreen();
PrintLine(TEXT(“Sorry No More Lives”));
PrintLine(TEXT(“The HiddenWord was : %s \n”), *HiddenWord);

   EndGame(); 
   return;

}

PrintLine(TEXT(“Try again, You have %i lives left”), Lives);
}

bool UBullCowCartridge::IsIsogram(const FString& Guess)
{
for (auto& c : Guess.GetCharArray())
if (c == *(&c + 1)) return true;

    //for(int i{0}; i < Guess.Len() - 1; ++i)
    //{
    //    if (Guess.GetCharArray()[i] == Guess.GetCharArray()[i + 1]) return true;
    //}
    return false;

}

ok . it looks like i did not have to use GetCharArray() member function instead simply use operator as regular c++ string to access any charcter so my Isogram() function got revised ;

bool UBullCowCartridge::IsIsogram(const FString& Guess)
{
// for (auto& c : Guess.GetCharArray())
// if (c == *(&c + 1)) return true;

    for(int i{0}; i < Guess.Len()-1; ++i)
    {
        if ( Guess[ i ] == Guess[ i + 1] ) return true;
    }
    return false;

}

just noticed my isogram check was notcorrect cause it only checks two adjacent characters but it has to check the whole string; so this one uses classic nested loop looks correct

bool UBullCowCartridge::IsIsogram(const FString& Guess)
{
for (int i{0}; i < Guess.Len(); ++i)
{
for (int j{i+1}; j < Guess.Len(); ++j)
{
if (Guess[i] == Guess[j])
return true;
}
}

return false;

}

I will try to USE a member function and will repost once i figure out
Probably use FindChar() function once i figure out how to do it =) that will be my challenge

Privacy & Terms