// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
#include "Containers/UnrealString.h"
#include "HiddenWordList.h"
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
SetupGame();//Setting up game
PrintLine(TEXT("The number of possible words are: %i."), Words.Num());
PrintLine(TEXT("The Hidden Word is: %s."), *HiddenWord);
PrintLine(TEXT("\nIts length is: %i"), HiddenWord.Len());
}
void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
//Main loop on entry by Player
if (bGameOver)//is Game Over?
{
ClearScreen();
SetupGame();
} else if(!bGameOver)//checking guess
{
ProcessGuess(Input);
}
}
void UBullCowCartridge::SetupGame()
{
//Initialize Member Variable(s)
HiddenWord = TEXT("ounces");
Lives = HiddenWord.Len();
bGameOver = false;
//Welcome message
PrintLine(TEXT("Welcome Player to Bull Cows!"));
PrintLine(TEXT("Guess the %i letter magic word..."), HiddenWord.Len());
PrintLine(TEXT("You have %i Lives to guess the word..."), Lives);
PrintLine(TEXT("Type in your guess and\nPress <ENTER> to continue..."));
/* const TCHAR HW[] = TEXT("peaches");
//const TCHAR HW = {TEXT('o'), TEXT('u'), TEXT('n'), TEXT('c'), TEXT('e'), TEXT('s'), TEXT('\0')};
PrintLine(TEXT("Character 1 of the hidden word is: %c"), HiddenWord[0]);//should print first char of the word "o"
PrintLine(TEXT("Character 4 of HW array is: %c"), HW[3]); */
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("\nThe Game is Over\nPress <ENTER> to play again..."));
}
void UBullCowCartridge::ProcessGuess(FString Guess)
{
if (Guess == HiddenWord)
{
PrintLine(TEXT("You Won!"));
EndGame();
return;
}
//Check length of guess to length of Hidden Word
if (Guess.Len() != HiddenWord.Len())
{
PrintLine(TEXT("The HiddenWord is %i letters long"), HiddenWord.Len());
PrintLine(TEXT("Sorry, guess not same length as word\nYou have %i lives left..."), Lives);
return;
}
//Check if it's an isogram
if (!IsIsogram(Guess))
{
//IsIsogram return a bool on whether it's an Isogram or not, if not
PrintLine(TEXT("There are no repeating letters,\nPlease guess again..."));
//remove Life
--Lives;
return;
}
//Remove a Life
PrintLine(TEXT("You have lost a life...\n"));
--Lives;
//check if any Lives left
if (Lives <= 0)
{
ClearScreen();
PrintLine(TEXT("You have no lives left, the cows rejoice"));
PrintLine(TEXT("The Hidden Word was %s.\nGood job for doing as well as you did..."), *HiddenWord);
EndGame();
return;
}
//Show player the Bulls and Cows
PrintLine(TEXT("Guess again, you have %i Lives left..."), Lives);
}//end ProcessGuess
bool UBullCowCartridge::IsIsogram(FString Word) const
{
//get length of Guess, so we know our outer bounds
int32 GuessLength = Word.Len()-1;
//check number of characters
//set a value to number of chars
//step through each value and see if there is another char like it in the array
//if no doubles return true as it is an isogram
return true;
}![EmptyArrayError|690x401](upload://n3qAgNAWGMxTYaso57Q9GvJEusr.jpeg)