Here is my code.
#include "BullCowCartridge.h"
#include "HiddenWordList.h"
void UBullCowCartridge::BeginPlay()
{
Super::BeginPlay();
SetupGame();// makes sense to set up game first: Lives and Word ready to go
PrintLine(TEXT("The number of possible words is %i"), Words.Num());
PrintLine(TEXT("The Number of valid words is: %i."), GetValidWords(Words).Num());
PrintLine(TEXT("The Hidden word is: %s."), *HiddenWord);// debug line
}
void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
if (bGameOver == true)
{
ClearScreen();
SetupGame();
return;
}
else // Check Player guess
{
ProcessGuess(Input);
return;
}
}
void UBullCowCartridge::SetupGame()
{
// Welcomeing The Player
PrintLine(TEXT("Welcome to Bull Cow!"));
HiddenWord = TEXT("words");
Lives = HiddenWord.Len();
bGameOver = false;
PrintLine(TEXT("The Hidden word is: %s."), *HiddenWord);// debug line
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 moove on..."));
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("\nPress Enter to play again..."));
}
void UBullCowCartridge::ProcessGuess(FString Guess)
{
if (Guess == HiddenWord)
{
ClearScreen();
PrintLine(TEXT("You Win!"));
EndGame();
return;
}
if (Guess.Len() != HiddenWord.Len())
{
PrintLine(TEXT("The hidden word is %i letters long."), HiddenWord.Len());
PrintLine(TEXT("Sorry! Guess again, you have %i \nlives left."), Lives);
return;
}
// Isogram?
if (!IsIsogram(Guess))
{
PrintLine(TEXT("No repeatingletters, guess again!"));
return;
}
PrintLine(TEXT("Lost a Life!"));
--Lives;
if (Lives <= 0)
{
ClearScreen();
PrintLine(TEXT("You have no lives left!"));
PrintLine(TEXT("The hidden world was: %s."), *HiddenWord);
EndGame();
return;
}
// Show the player BUlls and Cows
PrintLine(TEXT("Guess again, you have %i lives left."), Lives);
}
bool UBullCowCartridge::IsIsogram(FString Word) const
{
// Instructors Code
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 && WordList[Index].Len() <= 8 && IsIsogram(WordList[Index]) )
{
ValidWords.Emplace(WordList[Index]);
}
}
return ValidWords;
}
I completely misinterpreted the challenge requirements, so I used a bool (for some reason) instead of a TArray. I knew I was wrong somehow.
Mistakes aside, I think I understand what is going on. When I am done this section, I may take a quick scan over it again before continuing on to the next section.
Also, I found that previous to this, when we were creating a new list of words in ValidWords, my list did something strange: have a look:
Here is the code:
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
SetupGame();
PrintLine(TEXT("The number of possible words is %i"), Words.Num());
PrintLine(TEXT("The Hidden word is: %s."), *HiddenWord);// debug line
TArray<FString> ValidWords;
for (int32 Index = 0; Index < 10; Index++)
{
if (Words[Index].Len() >= 4 && Words[Index].Len() <= 8 )
{
ValidWords.Emplace(Words[Index]);
}
for (int32 Index = 0; Index < ValidWords.Num(); Index++)
{
PrintLine(TEXT("%s."), *ValidWords[Index]);
}
}