My Game with RandRange() using a .txt file for HiddenWords

I tried using the different method of importing hidden words, and have succeeded after some trial and error with calling functions :slight_smile:

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

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

    // Load in words in runtime, this makes compiling significantly quicker
    const FString WordListPath = FPaths::ProjectContentDir() / TEXT("WordLists/HiddenWordList.txt");
    // Loads text into strings in Words array
    FFileHelper::LoadFileToStringArray(Words, *WordListPath);
    
    // Sends words from file to validate, sets Words to ValidWords from GetValidWords()
    Words = GetValidWords(Words);

    SetupGame();    
}

void UBullCowCartridge::OnInput(const FString& PlayerInput) // When the player hits enter
{
    if (bGameOver) // == true (like Python)
    {
        ClearScreen();
        SetupGame();
    }
    else // Checking PlayerGuess
    {
        ProcessGuess(PlayerInput);
    }
}

void UBullCowCartridge::SetupGame()
{
    // Welcoming The Player
    PrintLine(TEXT("Welcome to Bull Cows!"));
    //PrintLine(TEXT("%i"), Words.Num()); // Debug Line
    
    int32 RandNum = FMath::RandRange(0, Words.Num() - 1);
    HiddenWord = Words[RandNum]; 
    Lives = HiddenWord.Len(); 
    bGameOver = false;

    PrintLine(TEXT("Guess the %i letter word!"), HiddenWord.Len()); 
    PrintLine(TEXT("You have %i lives."), Lives);
    PrintLine(TEXT("Type in your guess and \npress enter to continue...")); // Prompt Player for Guess
    PrintLine(TEXT("The HiddenWord is: %s."), *HiddenWord); //Debug Line

}

void UBullCowCartridge::EndGame()
{
    bGameOver = true;
    PrintLine(TEXT("\nPress enter to play again..."));
}

void UBullCowCartridge::ProcessGuess(const FString& Guess) 
{
    if (Guess == HiddenWord) // Check validity
        {
            PrintLine(TEXT("Correct!"));
            EndGame();
            return;
        }

    if (Guess.Len() != HiddenWord.Len()) // Check number of characters
        {
            PrintLine(TEXT("The hidden word is %i letters long"), HiddenWord.Len());
            PrintLine(TEXT("Sorry, try guessing again! \nYou have %i lives remaining"), Lives);
            return;
        } 

    // Check if isogram
    if (!IsIsogram(Guess)) 
    {
        /* code */
        PrintLine(TEXT("No repeating letters, guess again"));
        return;
    }

    // Remove life
    PrintLine(TEXT("You have lost a life!"));
    --Lives; 
    
    if (Lives <= 0)
        {
            ClearScreen();
            PrintLine(TEXT("You have 0 lives remaining"));
            PrintLine(TEXT("The hidden word was: %s"), *HiddenWord);
            EndGame();      
            return;
        }
   
    // Show the player Bulls and Cows
    PrintLine(TEXT("Guess again, you have %i lives left"), Lives);
}

bool UBullCowCartridge::IsIsogram(const FString& Word) const
{
    
    for (int32 Index = 0; Index < Word.Len(); Index++)
    {
        for (int32 Comparison = Index + 1; Comparison < Word.Len(); Comparison++)
        {
            if (Word[Index] == Word[Comparison])
            {
                return false;
            }
        }  
    }
    
    return true;
}
 
 TArray<FString> UBullCowCartridge::GetValidWords(const TArray<FString>& WordList) const
 {
    TArray<FString> ValidWords;

    for (FString TempWord : WordList)
    {
        if (TempWord.Len() >= 4 && TempWord.Len() <=8 && IsIsogram(TempWord))
        {
            ValidWords.Emplace(TempWord);
        }
    }

    //PrintLine(TEXT("%i"), ValidWords.Num()); // Debug Line
    return ValidWords;
 }
1 Like

Good :+1:

1 Like

Privacy & Terms