Added a level up feature with added difficulty up to level 5. Took a bit to figure out how it should work, but this seemed to turn out alright after a few changes.
// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
#include "HiddenWordList.h"
// #include "Math/UnrealMathUtility.h"
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
Isograms = GetValidWords(Words);
Level = 0;
SetupGame();
}
void UBullCowCartridge::OnInput(const FString& PlayerInput) // When the player hits enter
{
if (bGameOver)
{
ClearScreen();
SetupGame();
}
else // Checking PlayerGuess
{
ProcessGuess(PlayerInput);
}
}
void UBullCowCartridge::SetupGame()
{
// Welcoming The Player
PrintLine(TEXT("Welcome to Bull Cows!"));
HiddenWord = GetValidWords(Words)[FMath::RandRange(0, GetValidWords(Words).Num() - 1)];
Lives = HiddenWord.Len()*2;
bGameOver = false;
PrintLine(TEXT("Level: %i"), Level);
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)
{
PrintLine(TEXT("You have Won!"));
LevelUp();
EndGame();
return;
}
if (Guess.Len() != HiddenWord.Len())
{
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("Lost a life!"));
--Lives;
if (Lives <= 0)
{
ClearScreen();
PrintLine(TEXT("You have no lives left!"));
PrintLine(TEXT("The hidden word was: %s"), *HiddenWord);
Level = 0;
EndGame();
return;
}
// Show the player Bulls and Cows
FBullCowCount Score = GetBullCows(Guess);
PrintLine(TEXT("You have %i Bulls and %i Cows"), Score.Bulls, Score.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 Word : WordList)
{
if (Word.Len() == 4 + Level && IsIsogram(Word))
{
ValidWords.Emplace(Word);
}
}
return ValidWords;
}
FBullCowCount UBullCowCartridge::GetBullCows(const FString& Guess) const
{
FBullCowCount Count;
for (int32 GuessIndex = 0; GuessIndex < Guess.Len(); GuessIndex++)
{
if (Guess[GuessIndex] == HiddenWord[GuessIndex])
{
Count.Bulls++;
continue;
}
for (int32 HiddenWordIndex = 0; HiddenWordIndex < HiddenWord.Len(); HiddenWordIndex++)
{
if (Guess[GuessIndex] == HiddenWord[HiddenWordIndex])
{
Count.Cows++;
break;
}
}
}
return Count;
}
void UBullCowCartridge::LevelUp()
{
ClearScreen();
Level++;
if (Level >= 6)
{
PrintLine(TEXT("FINAL LEVEL COMPLETE! \nYou have beaten the game! \nAll the bulls and cows are yours now!"));
Level = 0;
return;
}
PrintLine(TEXT("Level Up! \nLevel: %i"),Level);
if (Level == 5)
{
PrintLine(TEXT("Final Level!"));
}
}
// 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"
struct FBullCowCount
{
int32 Bulls = 0;
int32 Cows = 0;
};
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(const FString& Guess);
bool IsIsogram(const FString& Word) const;
TArray<FString> GetValidWords(const TArray<FString>& WordList) const;
FBullCowCount GetBullCows(const FString& Guess) const;
void LevelUp();
// Your declarations go below!
private:
FString HiddenWord;
int32 Lives;
bool bGameOver;
TArray<FString> Isograms;
int32 Level;
};