Looking good. One little bug to fix but not a biggy.
// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
#include "HiddenWordsList.h"
#include "Math/UnrealMathUtility.h"
void UBullCowCartridge::BeginPlay() // When the game
{
Super::BeginPlay();
GetValidWords(Words);
IntiateGame();
}
void UBullCowCartridge::OnInput(const FString& PlayerInput) // When the player hits enter
{
ClearScreen();
ProcessInput(PlayerInput);
}
void UBullCowCartridge::DisplayWelcomeMessage()
{
PrintLine(TEXT("Velcro's Bulls and Cows"));
PrintLine(TEXT("Please click on board and press 'Tab' to start"));
//PrintLine(TEXT("Enter a ") + FString::FromInt(GameLevel + 3) + TEXT(" letter word...")); // what you would normally do if it wasn't defined elsewhere
PrintLine(TEXT("Enter a %i letter word."), HiddenWord.Len());
}
void UBullCowCartridge::ProcessInput(const FString& Input)
{
if (bEndPrompt) {
if (Input.Len() == 1 && Input == "P" && bEndPrompt) {
bResetGame = true;
bEndPrompt = false;
}
if (Input.Len() == 1 && Input == "Q" && bEndPrompt) {
bResetGame = false;
bEndPrompt = false;
PrintLine(TEXT("Bye."));
}
}
if (!IsAnIsogram(Input))
{
PrintLine(TEXT("%s is not an Isogram"), *Input);
}
if (bResetGame)
{
ClearScreen();
IntiateGame();
}
else {
if (HiddenWord == Input)
{
PrintLine(TEXT("Nailed it."));
PrintLine(TEXT("The Hidden word was %s"), *HiddenWord);
EndGame();
}
else
{
if (Input.Len() != HiddenWord.Len())
{
PrintLine(TEXT("Enter a %i letter word."), HiddenWord.Len());
}
Lives--;
if (Lives <= 0) {
PrintLine(TEXT("You have lost dude."), Lives);
EndGame();
}
else
{
PrintLine(TEXT("Try again. You have %i lives left."), Lives);
}
}
}
}
void UBullCowCartridge::IntiateGame()
{
//Intiate game(lives Levels hidden word)
bResetGame = false;
bEndPrompt = false;
GameLevel = 1;
Lives = GameLevel * LivesMultiplier;
HiddenWord = GetHiddenWord();
DisplayWelcomeMessage();
if (bIsItDebugTime) { PrintLine(TEXT("The Hidden word is %s"), *HiddenWord); }
}
FString UBullCowCartridge::GetHiddenWord()
{
HiddenWord = GetValidWords(Words)[FMath::RandRange(0, GetValidWords(Words).Num() -1 )];
return HiddenWord;
}
void UBullCowCartridge::EndGame()
{
ContinueGamePrompt();
}
void UBullCowCartridge::ContinueGamePrompt()
{
bEndPrompt = true;
PrintLine(TEXT("'P'lay Again? or 'Q'uit?, then Enter"));
}
bool UBullCowCartridge::IsAnIsogram(const FString& str) const
{
for (int i = 0; i < str.Len(); i++)
{
for (int j = 0; j < str.Len(); j++)
{
if (i != j)
{
if (str[i] == str[j])
{
return false;
}
}
}
}
return true;
}
TArray<FString> UBullCowCartridge::GetValidWords(const TArray<FString>& Wordlist ) const
{
PrintLine(TEXT("Number of words are: %i"), Wordlist.Num());
TArray<FString> ValidWords;
for (FString Words : Wordlist)
{
if (Words.Len() <= 8 && Words.Len() >= 4 && IsAnIsogram(Words))
{
ValidWords.Emplace(Words);
}
}
return ValidWords;
}