I didn’t have to change much as I wrote code that only printed to screen instead. When I was writing the pseudocode in comments I was finding it hard to organize where the code would be.
BullCowCartridge.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Console/Cartridge.h"
#include "BullCowCartridge.generated.h"
UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class BULLCOWGAME_API UBullCowCartridge : public UCartridge
{
GENERATED_BODY()
public:
virtual void BeginPlay() override;
virtual void OnInput(const FString& Input) override;
void SetupGame();
void EndGame();
void MinusLife();
void ProcessGuess(FString Guess);
bool CheckIsogram(FString Word2Check);
void BullCowculator(FString Word3Check);
// Your declarations go below!
private:
FString HiddenWord;
int32 Lives, AttemptWarning;
bool bGameOver;
};
BullCowCartridge.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
SetupGame();
PrintLine(FString::Printf(TEXT("Hidden word is: %s. "), *HiddenWord)); // Debug Line
}
void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
if (bGameOver)
{
ClearScreen();
SetupGame();
}
else
{
ProcessGuess(Input);
}
}
void UBullCowCartridge::SetupGame()
{
PrintLine(TEXT("***Bull Cows Game*** "));
HiddenWord = TEXT("aspired");
Lives = HiddenWord.Len();
AttemptWarning = 0;
bGameOver = false;
PrintLine(TEXT("Guess the %i letter isogram: "), HiddenWord.Len()); //Prompt player for guess
PrintLine(TEXT("You have %i lives. "), Lives);
PrintLine(TEXT("Type your guess \nPress enter to continue... "));
// const TCHAR HW[] = TEXT("cakes");
// PrintLine(TEXT("Character 1 of the hidden word is: %c"), HiddenWord[0]);
// PrintLine(TEXT("Character 4 of HW is: %c"), HW[3]);
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("\n*** Game Over ***. "));
PrintLine(TEXT("Press enter to play again. "));
// PrintLine(TEXT("Press something to quit. "));
}
void UBullCowCartridge::MinusLife()
{
if (Lives > 0)
{
PrintLine(TEXT("You have %i lives remaining. "), --Lives);
if (Lives == 0)
{
PrintLine(TEXT("Last try! "));
}
AttemptWarning = 0;
}
else
{
ClearScreen();
PrintLine(TEXT("You have no lives remaining. "));
AttemptWarning = 0;
PrintLine(TEXT("\nYou Lose!"));
EndGame(); // Losing EndGame
}
}
void UBullCowCartridge::ProcessGuess(FString Guess)
{
if (Guess == HiddenWord)
{
ClearScreen();
PrintLine(TEXT("You win!"));
EndGame(); // Winning EndGame
return;
}
if (Guess.Len() != HiddenWord.Len()) //Checking if guess is not valid length
{
if (AttemptWarning++ < 2) // Tracking and advising player's invalid attempts
{
PrintLine(TEXT("Attempt Warning # %i"), AttemptWarning);
PrintLine(TEXT("The hidden word is %i characters long. "), HiddenWord.Len());
if (Guess.Len() < HiddenWord.Len())
{
PrintLine(TEXT("Your guess needs to be longer. "));
return;
}
else
{
PrintLine(TEXT("Your guess needs to be shorter. "));
return;
}
}
// Invalid attempt punishment
if (Lives > 0)
{
PrintLine(TEXT("Guessing carelessly lost you a life. "));
MinusLife();
return;
}
else
{
PrintLine(TEXT("Your hidden word was: %s "), *HiddenWord);
MinusLife();
return;
}
}
if (!CheckIsogram(Guess)) //Checking if guess is not an isogram
{
if (AttemptWarning++ < 2) // Tracking and advising player's invalid attempts
{
PrintLine(TEXT("Attempt Warning # %i"), AttemptWarning);
PrintLine(TEXT("Your guess was not an isogram."));
return;
}
if (Lives > 0)
{
PrintLine(TEXT("*Note* An isogram is a word with no repeating letters."));
MinusLife();
return;
}
else
{
PrintLine(TEXT("Your hidden word was: %s "), *HiddenWord);
MinusLife();
return;
}
BullCowculator(Guess);
}
}
bool UBullCowCartridge::CheckIsogram(FString Word2Check)
{
PrintLine(TEXT("checking if %s is an isogram. "), *Word2Check); //Debug ling
// For each ketter
// Start at element [0]
// Compare against the letter
// until we reach [Word2Check.Len() - 1]
// if any are the same return false
return true; // Implement isogram checking code and return appropriate true / false
}
void UBullCowCartridge::BullCowculator(FString Word3Check)
{
PrintLine(TEXT("The bulls and cows for %s are: "), *Word3Check); // Debug line
// implement bull cow checking based on argument
}