Here is my code for the BullCowCartridge.cpp
#include "BullCowCartridge.h"
#include "HiddenWordList.h"
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
SetupGame();
Isograms = GetValidWords(Words);
PrintLine(TEXT("The Word is: %s"), *HiddenWord);
PrintLine(TEXT("There are %i possible words"), Words.Num());
}
void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
if (bGameOver)
{
ClearScreen();
SetupGame();
}
ProcessGuess(Input);
}
void UBullCowCartridge::SetupGame()
{
//Welcome Player
PrintLine(TEXT("Welcome to the game of BullCow!"));
HiddenWord = Isograms[FMath::RandRange(0, Isograms.Num() -1)];
Lives = HiddenWord.Len();
bGameOver = false;
//Instructions
PrintLine(TEXT("Guess the %i letter word!"), HiddenWord.Len());
PrintLine(TEXT("You have %i lives, or chances to guess"), Lives);
PrintLine(TEXT("Please write your guess and \nthen hit the \"Enter\" Key to continue...\n"));
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("\nThe game is over! Hit ENTER to play again"));
}
void UBullCowCartridge::ProcessGuess(const FString& Guess)
{
if (Guess == HiddenWord)
{
ClearScreen();
PrintLine(TEXT("Wonderful Job! You have guessed the correct word!"));
EndGame();
return;
}
//Checking Player Guess
if (Guess.Len() != HiddenWord.Len()) //Check if Guess is right length
{
PrintLine(TEXT("Your guess is not even the right length!! \nThe Word is %i characters long"), HiddenWord.Len());
PrintLine(TEXT("Guess Again"));
return;
}
--Lives;
PrintLine(TEXT("You have lost a life"));
if (!IsIsogram(Guess)) //Check if Isogram
{
PrintLine(TEXT("No Repeating Letters"));
PrintLine(TEXT("Guess Again"));
return;
}
//Show Player Bulls and Cows
FBullCowCount Score = GetBullCows(Guess);
PrintLine(TEXT("You have %i Bulls and %i Cows"), Score.Bulls, Score.Cows);
if (Lives <= 0)//Check if Lives < 0
{
ClearScreen();
PrintLine(TEXT("You are out of Lives"));
PrintLine(TEXT("The hidden word was: %s"), *HiddenWord);
EndGame();
}
else
{
PrintLine(TEXT("Sorry, that is incorrect, try again."));
PrintLine(TEXT("You have %i Lives left"), Lives);//Show lives left
}
}
bool UBullCowCartridge::IsIsogram(const FString& WordGuess) const
{
int CharCheckNum = 0;
while (CharCheckNum != WordGuess.Len() - 1) //will repeat until the CharCheckNum == to Length of WordGuess
{
if(WordGuess[CharCheckNum] == WordGuess[CharCheckNum + 1]) //will check if two letters are equal
{
return false;
}
++CharCheckNum;
}
return true;
}
TArray<FString> UBullCowCartridge::GetValidWords(const TArray<FString>& WordPossibilities) const
{
TArray<FString> ValidWords;
for (FString Word : WordPossibilities)
{
if (Word.Len() >= 4 && Word.Len() <=8 && IsIsogram(Word))
{
ValidWords.Emplace(Word);
}
}
return ValidWords;
}
FBullCowCount UBullCowCartridge::GetBullCows(const FString& Guess) const
{
FBullCowCount Count {0, 0};
for (int32 GuessIndex = 0; GuessIndex < Guess.Len(); GuessIndex++)
{
if(Guess[GuessIndex] == HiddenWord[GuessIndex])
{
Count.Bulls++;
continue;
}
for(int32 CheckIndex = 0; CheckIndex < HiddenWord.Len(); CheckIndex++)
{
if(HiddenWord[CheckIndex] == Guess[GuessIndex])
{
Count.Cows++;
break;
}
}Preformatted text
}
return Count;
}