// 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();
}
void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
if (bGameOver == true)
{
ClearScreen();
SetupGame();
}
else
{
ProcessGuess(Input);
}
}
// Check if isogram
// Check right number of characters
// Remove life
// If all lives lost, prompt for guess again
// Show lives left
// Else print game over and HiddenWord
// Prompt to play again, Press enter to play again?
// If yes, set hidden word and lives
// Else quit game
void UBullCowCartridge::SetupGame()
{
HiddenWord = TEXT("machine");
Lives = HiddenWord.Len();
bGameOver = false;
PrintLine(TEXT("Welcome To Bull Cow Game")); //Prints Welcome Message
PrintLine(TEXT("The HiddenWord Is %i Charecters Long"), HiddenWord.Len()); //remove the hardcoded 4 later
PrintLine(TEXT("Type in your guess and \npress enter to continue..."));
PrintLine(TEXT("You Have %i Lives"), Lives);
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("Press Enter To Play Again"));
}
bool UBullCowCartridge::isIsogram(FString word)
{
for(int i = 0; i < word.Len() - 1; i++)
{
if (word.IsValidIndex(i + 1))
{
if (word[i] != word[i + 1])
{
return false;
}
}
}
return true;
}
void UBullCowCartridge::ProcessGuess(FString Guess)
{
// Check if the PlayerGuess is the HiddenWord
if (Guess == HiddenWord)
{
// The Player wins the game
ClearScreen();
PrintLine(TEXT("You guessed the word!"));
PrintLine(TEXT("The hidden word is: %s."), *HiddenWord);
PrintLine(TEXT("YOU WIN!"));
EndGame();
return;
}
if (Guess != HiddenWord)
{
// Clear terminal
ClearScreen();
// Print messages for the player telling about the mistake in the guess
PrintLine(TEXT("Ooh sorry your guess is not correct! Hint:The hidden word has %i letters."), HiddenWord.Len());
// Remove a life since the player missed in the guess
PrintLine(TEXT("You lost a life. Now you have %i lives."), --Lives);
if (Guess.Len() != HiddenWord.Len())
{
PrintLine(TEXT("Please remember that\nthe hidden word has %i letters."), HiddenWord.Len());
}
if (!isIsogram(Guess))
{
PrintLine(TEXT("This word has repeating letters!"));
}
if (Lives == 0)
{
PrintLine(TEXT("The Hiddenword was %s"), *HiddenWord);
PrintLine(TEXT("Press Enter To Play Again"));
EndGame();
}
}
}
This Is CPP File, The issue is that even though I enter an isogram
it is printing and treating it as it is not, I use UE5 beta. But from what I know this cannot be causing any issues though I have not tried it on UE4.
The Header File:
// 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 ProcessGuess(FString Guess);
bool isIsogram(FString Word);
// Your declarations go below!
private:
FString HiddenWord;
int32 Lives;
bool bGameOver;
};