void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
const FString WordListPath = FPaths::ProjectContentDir() / TEXT("Wordlists/HiddenWordList.txt");
FFileHelper::LoadFileToStringArray(Words, *WordListPath);
Words = GetValidWords(Words);
PrintLine(TEXT("The library consists of %i valid isograms"), Words.Num());
SetupGame();
}
I implemented the runtime HiddenWordList parser from earlier.
I saw no reason to maintain 2 wordlists so I replaced the full wordlist array from the library with the new ValidWords array.
TArray<FString> UBullCowCartridge::GetValidWords(TArray<FString> WordList) const
{
TArray<FString> ValidWords;
for (int32 Index = 0; Index < WordList.Num(); Index++)
{
if (WordList[Index].Len() >= 4 && WordList[Index].Len() <= 8 && IsIsogram(WordList[Index])) // Check for Isograms between 4 and 8 Characters long
{
ValidWords.Emplace(WordList[Index]);
}
}
return ValidWords;
}
And I got 452 valid words at runtime. Yay!
2 Likes
Wow that is a lot of words! Great job!
Here is my code so far, everything is going fine to this point c:
// 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();
PrintLine(TEXT("El numero posible de palabras es: %i"), Words.Num());
PrintLine(TEXT("El numero de palabras validas es: %i."), GetValidWords(Words).Num());
PrintLine(TEXT("La palabra secreta es: %s."), *HiddenWord); // Debug Line
}
void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
if (bGameOver)
{
ClearScreen();
SetupGame();
}
else // Checking PlayerGuess
{
ProcessGuess(Input);
}
}
void UBullCowCartridge::SetupGame()
{
// Welcoming player
PrintLine (TEXT("Muuuuuy bienvenida sea usted!"));
HiddenWord = TEXT("cafe");
Lives = HiddenWord.Len();
bGameOver = false;
PrintLine (TEXT("Adivina la palabra de %i letras"), HiddenWord.Len());
PrintLine(TEXT("\nTe queda %i vidas"), Lives);
PrintLine (TEXT("Escribe una palabra y \npresiona ENTER para continuar")); // Prompt PlayerGuees
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
ClearScreen();
PrintLine(TEXT("\nPresiona Enter para jugar de nuevo"));
}
void UBullCowCartridge::ProcessGuess(FString Guess)
{
if (Guess == HiddenWord) // Order doesn't matter
{
PrintLine (TEXT("Correcto"));
EndGame();
return;
}
if (Guess.Len() != HiddenWord.Len())
{
PrintLine(TEXT("La palabra tiene %i letra/s"), HiddenWord.Len());
PrintLine(TEXT("Intenta de nuevo. \nTe quedan %i vida/s"), Lives);
return;
}
// Check if Isogram
if (!IsIsogram(Guess))
{
PrintLine(TEXT("No debe haber letras repetidas.\nIntenta de nuevo."));
return;
}
//Remove Life
PrintLine(TEXT("Perdiste una vida."));
--Lives;
if (Lives <= 0)
{
PrintLine(TEXT("Ya no te quedan vidas."));
PrintLine(TEXT("La palabra secreta era: %s"), *HiddenWord);
EndGame();
return;
}
// Show the player Bulls and Cows
PrintLine(TEXT("Intenta de nuevo.\nTe quedan %i vida/s"), Lives);
}
bool UBullCowCartridge::IsIsogram(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(TArray<FString> WordList) const
{
TArray<FString> ValidWords;
for (int32 Index = 0; Index < WordList.Num(); Index++)
{
if (WordList[Index].Len() >= 4 && Words[Index].Len() <= 8 && IsIsogram(WordList[Index]))
{
ValidWords.Emplace(WordList[Index]);
}
}
return ValidWords;
}