Decided to do the newly released Bulls and Cows module and so far so good. Great introduction to the Unreal Editor Michael, I’ve learnt a lot!
Regarding the code, I decided to make the input interface a bit more robust by adding some user input checks and ignoring inappropriate input under certain conditions. I also added a ‘help’ request that can be called at any point and simply displays a basic description of the game.
Probably jumping the gun but it’s been a fun learning curve setting my own challenge and attempting to code a solution.
My additional instance scoped variables and a function definition added to BullCowCartridge.h shown below:
// Your declarations go below!
private:
FString HiddenWord;
FString CurrentWordGuess;
bool bHelpCalled;
bool bNewWord;
bool bHasGuessed;
void DisplayHelp();
and my modifications to the BullCowCartridge.cpp file:
#include "BullCowCartridge.h"
void UBullCowCartridge::DisplayHelp()
{
ClearScreen();
PrintLine(TEXT("The game is a guess the hidden word game."));
PrintLine(TEXT("The hidden word will always be an ISOGRAM,"));
PrintLine(TEXT("that is a word with no repeating letters."));
PrintLine(TEXT("Any correct letters in the right position"));
PrintLine(TEXT("will score you BULLS, any correct letters"));
PrintLine(TEXT("in the wrong position will score you COWS"));
PrintLine(TEXT("and any wrong letters score nothing."));
PrintLine(TEXT("You will have so many tries in order to "));
PrintLine(TEXT("guess the word correctly and win the game."));
PrintLine(TEXT("Press Enter to return to the game."));
}
void UBullCowCartridge::BeginPlay() // Called by Unreal Engine when the game starts
{
// Initialise variables
HiddenWord = TEXT("Place"); // Always encose literal strings in a TEXT macro to ensure native Unicode encoding.
bHelpCalled = false;
bNewWord = true;
bHasGuessed = false;
Super::BeginPlay();
PrintLine(TEXT("Welcome to the Bulls and Cows game\n"));
PrintLine(TEXT("Press Enter to continue"));
}
void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
ClearScreen();
PrintLine(TEXT("Type help and press Enter for a basic"));
PrintLine(TEXT("description of the game and rules.\n"));
PrintLine(TEXT("Otherwise guess the 5 letter word ..."));
// If user previously called help then display the previously stored word guess
if (bHelpCalled)
{
if (CurrentWordGuess != "")
{
PrintLine(CurrentWordGuess);
}
// Reset our global help called flag
bHelpCalled = false;
return;
}
else
{
// Has returned following Enter from guessing word correctly so just return. Negates any other kbd entry at this point being evaluated
if (bNewWord)
{
bNewWord = false;
return;
}
}
// If no characters input then ignore
if (Input != "")
{
// Diplay the users latest guess
PrintLine(Input);
// If user requested help
if (Input == TEXT("help"))
{
DisplayHelp();
// Set our global help called flag
bHelpCalled = true;
}
else
{
// Store the current word guess incase the user subsequently requests help or just hits enter and we need to restore the guess
CurrentWordGuess = Input;
bHasGuessed = true;
// Test to see if the guess is correct or not
if (Input == HiddenWord)
{
PrintLine(TEXT("Correct. You Win!\n"));
PrintLine(TEXT("Press Enter to play again"));
CurrentWordGuess = "";
bNewWord = true;
bHasGuessed = false;
}
else
{
PrintLine(TEXT("Incorrect. Try again"));
}
}
}
else if (bHasGuessed)
{
// If the user has previously guessed but no current guess then print previous guess
PrintLine(CurrentWordGuess);
}
}