If Guess == Y or yes not working

Hey there!
So I was checking if (Guess == “yes” || “Yes”) and than an else if comparing it to no and no matter what I did bPlayAgain would always be set to true and the game would reset. I just googled comparing 2 strings c++ and found the compare which is working correctly. I was wondering is this due to how you set up the terminal?

(I tried returning Guess with a print line to debug and it would reprint a line that was in EndGame() along with the players input.)

I guess my real question is why wasn’t it working when I was trying to compare Guess to “y” “Y” “yes” “Yes” or the no’s? every condition was returning true. I had made sure that in setup game the bool bPlayagain was set to false and it was only supposed to be true if the Guess == the different forms of yes I tried.

Could it be because I was comparing a FString and a regular string? Or perhaps its comparing types and it says yes they are both strings instead of comparing the strings themselves?

void UBullCowCartridge::ProcessGuess(FString Guess)
{
    if (bGameOver)
    {
        if (Guess.Compare("yes") == 0)
        {
            bPlayAgain = true;
            EndGame();
            return;
        }
        else if (Guess.Compare("no") == 0)
        {
            bPlayAgain = false;
            return;
        }
        return;
    }
//****Full cpp file****
#include "BullCowCartridge.h"

void UBullCowCartridge::BeginPlay() // When the game starts

{

    Super::BeginPlay();

    SetupGame();

    PrintLine(TEXT("The hidden word is: %s."), *HiddenWord);

}

void UBullCowCartridge::OnInput(const FString &Input) // When the player hits enter

{

    ClearScreen();

    ProcessGuess(Input);

}

void UBullCowCartridge::SetupGame()

{

    bGameOver = false;

    bPlayAgain = false;

    HiddenWord = TEXT("platform");

    Lives = HiddenWord.Len();

    PrintLine(TEXT("Welcome to the Bull Cow Game!"));

    PrintLine(TEXT("The word is %i letters long."), HiddenWord.Len());

    PrintLine(TEXT("You have %i guesses. Good luck!"), Lives);

    PrintLine(TEXT("Guess and press enter to start"));

}

void UBullCowCartridge::EndGame()

{

    bGameOver = true;

    PrintLine(TEXT("Would you like to play again? Enter yes or no."));

    if (bPlayAgain == true)

    {

        ClearScreen();

        SetupGame();

    }

}

void UBullCowCartridge::ProcessGuess(FString Guess)

{

    if (bGameOver)

    {

        if (Guess.Compare("yes") == 0)

        {

            bPlayAgain = true;

            EndGame();

            return;

        }

        else if (Guess.Compare("no") == 0)

        {

            bPlayAgain = false;

            return;

        }

        return;

    }

    if (Guess == HiddenWord)

    {

        PrintLine(TEXT("You suck! I mean.. you won.. but you still suck!"));

        EndGame();

        return;

    }

    else

    {

        if (Guess.Len() != HiddenWord.Len())

        {

            PrintLine(TEXT("Dummy that's not even the correct amount of letters."));

            PrintLine(TEXT("The hidden word is %i characters long."), HiddenWord.Len());

            PrintLine(TEXT("Your guess was %i characters long."), Guess.Len());

            PrintLine(TEXT("Figger it out!"));

        }

        else if (Lives > 0)

        {

            --Lives;

            PrintLine(TEXT("Incorrect! You have %i guesses remaining."), Lives);

        }

        else

        {

            PrintLine(TEXT("You lost. Loserrrr!"));

            EndGame();

        }

    }

}

//*****Header****

#pragma once

#include "CoreMinimal.h"

#include "Console/Cartridge.h"

#include "BullCowCartridge.generated.h"

UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))

class BULLCOWGAME_API UBullCowCartridge : public UCartridge

{

    GENERATED_BODY()

public:

    virtual void BeginPlay() override;

    virtual void OnInput(const FString &Input) override;

    void SetupGame();

    void EndGame();

    void ProcessGuess(FString Guess);

    // Your declarations go below!

private:

    FString HiddenWord;

    FString Guess;

    int32 Lives;

    bool bGameOver;

    bool bPlayAgain;

};

Context isn’t carried over, either side of an or are independent expressions.

Guess == "yes" || "Yes"

means the right hand side "Yes" decays into a pointer which is then implicitly covered to bool, as only nullptr evaluates to false that would mean it’s true. So that expression is equivalent to

Guess == "yes" || true

also FString == is already case insensitive.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms