Isogram Checker

I managed to get my Isogram check to work. I could probably make the code more efficient, but for now this works as intended.

bool UBullCowCartridge::IsIsogram(FString Word)
{
    int32 Checker = Word.Len() - 1;
    int32 Counter = Checker - 1;
    const TCHAR* IsoCheck = *Word;
    while (Checker >= 0)
    {
        while (Counter >= 0)
        {
            if (IsoCheck[Checker] == IsoCheck[Counter])
            {
                return false;
            }
            --Counter;
        }
        --Checker;
        Counter = Checker - 1;
    }
    return true;
}

To show my progress for the full CPP file

#include "BullCowCartridge.h"

void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();

    SetupGame();

    PrintLine(TEXT("The HiddenWord is: %s"), *HiddenWord); //Debug Line
}

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

    if (bGameOver)
    {
        ClearScreen();
        SetupGame();
    }
    else
    {
        CheckGuess(Input);
        if (Lives == 0)
        {
            PrintLine(TEXT("You have lost, the hidden word was %s"), *HiddenWord);
            EndGame();
            return;
        }
        if (!bGameWon)
        {
            PrintLine(TEXT("Your guess was wrong"));
            PrintLine(TEXT("Guess again, %i guess(es) remaining"), Lives);
        }
    }
}

void UBullCowCartridge::SetupGame()
{
    HiddenWord = TEXT("market");
    Lives = HiddenWord.Len();
    bGameOver = false;
    bGameWon = false;
    bMulligan = false;
    PrintLine(TEXT("Welcome to the Bull Cow Game!"));
    PrintLine(TEXT("You have %i guesses"), HiddenWord.Len());
    PrintLine(TEXT("Guess a %i letter isogram and press enter:"), HiddenWord.Len());
}

void UBullCowCartridge::EndGame()
{
    bGameOver = true;
    if (bGameWon)
    {
        PrintLine(TEXT("You guessed correctly!!!"));
    }
    PrintLine(TEXT("\nPress enter to play again"));
}

void UBullCowCartridge::CheckGuess(FString Guess)
{
    if (Guess == HiddenWord)
    {
        bGameWon = true;
        EndGame();
        return;
    }

    if (Guess == TEXT(""))
    {
        PrintLine(TEXT("Please type a guess")); 
        return;
    }

    if (bMulligan)
    {
        --Lives;
    }

    if (Guess.Len() != HiddenWord.Len())
    {
        PrintLine(TEXT("Guess was not %i characters long"), HiddenWord.Len());
        Mulligan();
        return;
    }

    //Check Isogram
    if (!IsIsogram(Guess))
    {
        PrintLine(TEXT("Guess has repeating characters"));
        Mulligan();
    }
}

void UBullCowCartridge::Mulligan()
{
    if (!bMulligan)
    {
        PrintLine(TEXT("This is your one mulligan")); 
    }
    bMulligan = true;
}

bool UBullCowCartridge::IsIsogram(FString Word)
{
    int32 Checker = Word.Len() - 1;
    int32 Counter = Checker - 1;
    const TCHAR* IsoCheck = *Word;
    while (Checker >= 0)
    {
        while (Counter >= 0)
        {
            if (IsoCheck[Checker] == IsoCheck[Counter])
            {
                return false;
            }
            --Counter;
        }
        --Checker;
        Counter = Checker - 1;
    }
    return true;
}

After watching the next video, I went and optimized the loops, swapping out the “while loops” for “for loops” and realizing I didn’t need to convert the string to a TCHAR but I’m still happy that I figured that out.

bool UBullCowCartridge::IsIsogram(FString Word) const
{
    for (int32 Index = 0; Index < Word.Len() - 1; Index++)
    {
        for (int32 Compare = Index + 1; Compare < Word.Len(); Compare++)
        {
            if (Word[Index] == Word[Compare])
            {
                return false;
            }
        }
    }
    return true;
}

I added the -1 to the first “for loop” because there is no reason to run the loop for the final character.

My IsIsogram Function after 2 hours thinking and scribbling on paper.
What should the IsIsogram () function be able to do: Compare each character with the following until the penultimate one has been reached. The last one cannot be compared any further. So you need 2 for loops. The first loop from the first to the penultimate character, the second from the next character to the last.
this is my code so far (it works)

bool UBullCowCartridge::IsIsogram(FString Word)
{
    for (int32 i = 0; i < Word.Len()-1; i++)
    {
        for (int32 j = i+1; j < Word.Len(); j++)
        {
            if (Word[i] == Word[j])
            {
                return false;
            }
        }
    }
    return true;
}

Cheers

Privacy & Terms