// Fill out your copyright notice in the Description page of Project Settings.
#include “BullCowCartridge.h”
#include “HiddenWordList.h”
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
SetupGame();
PrintLine(TEXT("The number of possible words is %i"), Words.Num());
TArray<FString> ValidWords;
for (int32 Index = 0; Index != 10; Index++)
{
if (Words[Index].Len() >= 4 && Words[Index].Len() <= 8)
{
PrintLine(TEXT("%s"), *Words[Index]);
ValidWords.Emplace(Words[Index]);
}
}
for (int32 Index = 0; Index < ValidWords.Num(); Index++)
{
PrintLine(TEXT("%s."), *ValidWords[Index]);
}
}
void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
if (bGameOver)
{
ClearScreen();
SetupGame();
}
else // Checking PlayerGuess
{
ProcessGuess(Input);
}
}
void UBullCowCartridge::SetupGame()
{
// Welcome the player
PrintLine(TEXT(“Welcome to Bulls & Cows!”));
HiddenWord = TEXT("cakes");
Lives = HiddenWord.Len();
bGameOver = false;
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."));
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT(“Press enter to play again.”));
}
void UBullCowCartridge::ProcessGuess(FString Guess)
{
if (Guess == HiddenWord)
{
PrintLine(TEXT(“You Have Won!”));
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))
{
PrintLine(TEXT("No repeating letters, guess again"));
return;
}
// remove a 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);
EndGame();
return;
}
PrintLine(TEXT("Guess again, you have %i lives left"), Lives);
}
bool UBullCowCartridge::IsIsogram(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;