Features I’ve added:
Start Menu - option to view instructions or just start the game
Levels - After beating level 5, play gets winning screen
Difficulty - length of hidden word increases with each level (starting at 4 letters)
Learned heaps with this project, thank you! My brain hurts!
#include "BullCowCartridge.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
FBullCowCount Count;
const FString WordListPath = FPaths::ProjectContentDir() / TEXT("Wordlists/HiddenWordList.txt");
FFileHelper::LoadFileToStringArray(Words, *WordListPath);
SetUpGame();
}
void UBullCowCartridge::OnInput(const FString& PlayerInput) // When the player hits enter
{
ClearScreen();
if (bGameOver)
{
ClearScreen();
SetUpGame();
}
else // Checking player input
{
ProcessGuess(PlayerInput);
}
}
void UBullCowCartridge::SetUpGame()
{
PrintLine(TEXT("Yowdy Baws!\nYou ever played this here Bull Cows game?"));
PrintLine(TEXT("\nLeft Click mouse\nPress TAB\nType \"Yup\" or \"Nope\"\nPress ENTER"));
Level = 0;
bGameOver = false;
bNextLevel = false;
}
void UBullCowCartridge::LevelClue()
{
if (bNextLevel == true && bGameOver == false)
{
Isograms = GetValidWords(Words);
HiddenWord = Isograms[FMath::RandRange(0, Isograms.Num() - 1)];
Lives = HiddenWord.Len();
bGameOver = false;
bNextLevel = false;
PrintLine(TEXT("\nNEXT LEVEL: %i"), Level);
PrintLine(TEXT("LIVES: %i"), Lives);
PrintLine(TEXT("The Hidden Word is: %s"), *HiddenWord); // Debug Line
}
else if (bNextLevel == false)
{
PrintLine(TEXT("\nLEVEL: %i"), Level);
PrintLine(TEXT("LIVES: %i"), Lives);
PrintLine(TEXT("The Hidden Word is: %s"), *HiddenWord); // Debug Line
return;
}
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
ClearScreen();
if (Lives == 0)
{
PrintLine(TEXT("Yuh lose.\nThe word was: %s\n\nLasso up the ENTER\nkey to play again."), *HiddenWord);
}
else if (Level >= 5)
{
PrintLine(TEXT("WINNER!\n\nLasso up the ENTER key\n to play again."));
}
else
{
SetUpGame();
}
}
void UBullCowCartridge::ProcessGuess(const FString& Guess)
{
// level == 0 is intro/instructions
if (Guess == "Yup" && Level == 0)
{
ClearScreen();
++Level;
bNextLevel = true;
LevelClue();
return;
}
if (Guess == "Nope" && Level == 0)
{
PrintLine(TEXT("Guess the correct isogram to win!\n"
" Isogram: a word in which the letters \n"
" occur an equal number of times.\n"
" TO PLAY: type your guess\n"
" and press ENTER.\n"
" BULL: correct letter, correct place.\n"
" COW: correct letter, wrong place.\n\n"
" Ready to play?\n"
" Type \"Yup\" to begin."));
return;
}
if (Level == 0) // if player input is not Yup or Nope at game start, does nothing
{
ClearScreen();
SetUpGame();
return;
}
// evaluate guesses
if (Lives == 0) // GAME OVER
{
EndGame();
return;
}
if (Guess == HiddenWord && Level < 5) // NEXT LEVEL
{
ClearScreen();
PrintLine(TEXT("Yup!"));
PrintLine(TEXT("You guessed: %s"), *Guess);
bNextLevel = true;
++Level;
LevelClue();
return;
}
if (Guess == HiddenWord && Level >= 5) //WINNER
{
EndGame();
return;
}
--Lives;
// show player bulls and cows
FBullCowCount Score = GetBullCows(Guess);
PrintLine(TEXT("You have %i Bulls and %i Cows"), Score.Bulls, Score.Cows);
if (Guess != HiddenWord && Guess.Len() == HiddenWord.Len())
{
ClearScreen();
bNextLevel = false;
PrintLine(TEXT("Nope."));
PrintLine(TEXT("You guessed: %s"), *Guess);
PrintLine(TEXT("Correct Word Length."));
PrintLine(TEXT("%i Bulls \n%i Cows"), Score.Bulls, Score.Cows);
LevelClue();
}
if (Guess != HiddenWord && Guess.Len() != HiddenWord.Len())
{
ClearScreen();
bNextLevel = false;
PrintLine(TEXT("Nope."));
PrintLine(TEXT("You guessed: %s"), *Guess);
PrintLine(TEXT("Incorrect word Length."));
PrintLine(TEXT("%i Bulls \n%i Cows"), Score.Bulls, Score.Cows);
LevelClue();
}
if (!IsIsogram(Guess))
{
ClearScreen();
bNextLevel = false;
PrintLine(TEXT("You guessed: %s"), *Guess);
PrintLine(TEXT("No repeating letters pal."));
PrintLine(TEXT("%i Bulls \n%i Cows"), Score.Bulls, Score.Cows);
LevelClue();
return;
}
}
//Fires after checking correct legth
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() == Level + 3 && 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 HiddenIndex = 0; HiddenIndex < HiddenWord.Len(); HiddenIndex++)
{
if (Guess[GuessIndex] == HiddenWord[HiddenIndex])
{
Count.Cows++;
break;
}
}
}
return Count;
}