713 possible words

cpp:

// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
#include "HiddenWordList.h"

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


}

void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
    if (bGameOver == true)
	{
		ClearScreen();
		SetupGame();
	}

	else
	{
		ProcessGuess(Input);
	}
}

// Check if isogram
// Check right number of characters
// Remove life
// If all lives lost, prompt for guess again
	// Show lives left
// Else print game over and HiddenWord
	// Prompt to play again, Press enter to play again?
	// If  yes, set hidden word and lives
	// Else quit game

void UBullCowCartridge::SetupGame()
{
    /*for (int32 Index = 0; Index <= GetValidWords(Words).Num(); Index++)
    {
        HiddenWord = GetValidWords(Words)[Index];
    } */
    HiddenWord = TEXT("Cakes");
	Lives = HiddenWord.Len();
	bGameOver = false;
    FilterWords(Words);
	PrintLine(TEXT("Welcome To Bull Cow Game")); //Prints Welcome Message
	PrintLine(TEXT("The HiddenWord Is %i Charecters Long"), HiddenWord.Len()); //remove the hardcoded 4 later
	PrintLine(TEXT("Type in your guess and \npress enter to continue..."));
	PrintLine(TEXT("You Have %i Lives"), Lives);

    

    //const TCHAR HW[] = TEXT("plums");
    //PrintLine(TEXT(%c), HW[0]); p should be printed out
    //PrintLine(TEXT(%c), HW[3]); m should be the one appearing.

    
}

void UBullCowCartridge::EndGame()
{
	bGameOver = true;
    PrintLine(TEXT("Press Enter To Play Again"));
}


bool UBullCowCartridge::isIsogram(FString word)
{
    for (int i = 0, comparing_letter = i+1; comparing_letter < word.Len() - 1; comparing_letter++)
    {
        if (word.IsValidIndex(comparing_letter))
        {
            if (word[i] == word[comparing_letter])
            {
                return false;
            }
        }
    }

    return true;
}

void UBullCowCartridge::ProcessGuess(FString Guess)
{

    // Check if the PlayerGuess is the HiddenWord

    if (Guess == HiddenWord)
    {
        // The Player wins the game

        ClearScreen();

        PrintLine(TEXT("You guessed the word!"));

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

        PrintLine(TEXT("YOU WIN!"));

        EndGame();

        return;

    }

    if (Guess != HiddenWord)
    {

        // Clear terminal

        ClearScreen();

        // Print messages for the player telling about the mistake in the guess

        PrintLine(TEXT("Ooh sorry your guess is not correct! Hint:The hidden word has %i letters."), HiddenWord.Len());

        // Remove a life since the player missed in the guess

        PrintLine(TEXT("You lost a life. Now you have %i lives."), --Lives);

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

            PrintLine(TEXT("Please remember that\nthe hidden word has %i letters."), HiddenWord.Len());

        }

        if (!isIsogram(Guess))
        {
            PrintLine(TEXT("This word has repeating letters!"));
        }

        if (Lives == 0)
        {
            PrintLine(TEXT("The Hiddenword was %s"), *HiddenWord);
            PrintLine(TEXT("Press Enter To Play Again"));
            EndGame();


        }
        

    }

}

TArray<FString> UBullCowCartridge::FilterWords(TArray<FString> WordList)
{
    TArray<FString> ValidWords;

    for (int32 Index = 0; Index < WordList.Num(); Index++)
    {
        if (WordList[Index].Len() >= 4 && WordList[Index].Len() <= 8 && isIsogram(WordList[Index]))
        {
            ValidWords.Emplace(WordList[Index]);
        }
    }
     
    PrintLine(TEXT("The right amount of words possible are: %i"), ValidWords.Num());
    return ValidWords;
}

header:

// Fill out your copyright notice in the Description page of Project Settings.

#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);
	bool isIsogram(FString Word);
	TArray<FString> FilterWords(TArray<FString> WordList);
	

	// Your declarations go below!
	private:
	
	FString HiddenWord;
	int32 Lives;
	bool bGameOver;
	
};

as for the hiddenword list.h I got it from github, yes I skipped the advance cursor usage lesson and as for why my course progress is at 1% is because I recently migrated from udemy.

1 Like

Your IsIsogram function is incorrect. Your code says “radar” is an isogram when it’s not as you’re only comparing adjacent characters

r == a
a == d
d == a
a == r

Which are all false so execution reaches the end of the function and returns true.
Whereas the courses code does the following comparisons

r == a
r == d
r == a
r == r # stops here, returns false

# would continue like so
# outer loop increments
a == d
a == a
a == r
# outer loop increments
d == a
d == r
# outer loop increments
a == r
1 Like

Thanks for solving my doubt :smiley:

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

Privacy & Terms