Sharing my ProcessGuess function

Sharing my code snippet here.

void UBullCowCartridge::ProcessGuess(FString Guess)
{
    if (Guess == HiddenWord) 
    {
        PrintLine(TEXT("You have Won!"));
        EndGame();
        return;
    }
    
    else
    {    
        if (Guess.Len() != HiddenWord.Len()) 
        {
            PrintLine(TEXT("Sorry, try again. %i lives remaining"),Lives);
            PrintLine(TEXT("The hidden word is %i letter long"),HiddenWord.Len());
            return;
        }

        // else if (!IsIsogram)
        // {
        //     /* code */
        //     PrintLine(TEXT("No repeating letters, guess again"));
        //     return
        // }
        else 
        {
            PrintLine(TEXT("You have lost 1 life"));
            --Lives;
        
            if (Lives <= 0)
            {
                ClearScreen();
                PrintLine(TEXT("You have no life left"));
                PrintLine(TEXT("The hiddenword is: %s."), *HiddenWord);
                EndGame();
            }
            else
            {
                PrintLine(TEXT("Guess again, you have %i lives left"),Lives);
            }
        }
        
    }
}
1 Like

Awesome job!!

Hi, I want to use this topic to avoid useless and numerous duplications. In the challenge of easy returns I tried to implement the code like the teacher did but the logic of my game is a little different. In order to apply a code compliant to my game logic I modified EndGame and ProcessGuess, simplifying the nested ifs. Very few returns were used because otherwise I could show in realtime the lives and the input errors. The game runs smoothly.

Here is the code:

void UBullCowCartridge::ProcessGuess(FString Guess)
{
    if (Guess == HiddenWord)
    {
        bVictory = true;        // new variable declared in the header
        EndGame(bVictory);  // modified version to pass a bool argument
        return;
    }
    else
    {
        --Lives;

        if (Lives > 0)
        {
            if ((Guess.Len() != HiddenWord.Len() || Guess == "") && !Guess.IsNumeric())
            {
                PrintLine(TEXT("The word you wrote has a wrong length."));                
            }

            if ((Guess.Len() == HiddenWord.Len()) && !Guess.IsNumeric())
            {
                PrintLine(TEXT("Wrong answer."));
            }

            if (Guess.IsNumeric())
            {
                PrintLine(TEXT("Don't insert numbers."));               
            }            

            //if (/*not an isogram*/)
            //{
            //    // Input is not isogram. Please insert an isogram
            //}

            PrintLine(TEXT("You lost a life!"));
            PrintLine(TEXT("Insert a % i characters long word.\nYou have % i lives remaining.\n"), HiddenWord.Len(), Lives);
        }
        else
        {
            bVictory = false;
            EndGame(bVictory);
            return;
        }
    }
}

Privacy & Terms