.h 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;
// Basic Game Functions
void SetupGame();
void EndGame();
void ProcessGuess(const FString &Input);
// Create a list of words for game
TArray<FString> GetValidWords(TArray<FString> &Words)const;
//Word Checkers
bool is_palindrome(const FString& s)const;
bool is_isogram(const FString& s)const;
// Your declarations go below!
private:
FString HiddenWord;
int32 player_lives;
bool bGameOver;
};
.cpp file
// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
#include <deque>
#include <algorithm>
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"
TArray<FString> UBullCowCartridge::GetValidWords(TArray<FString>&word_list)const {
TArray<FString> valid_words;
for (int32 i = 0; i < word_list.Num(); i++) {
if (word_list[i].Len() >= 4 && word_list[i].Len() <= 8)
valid_words.Emplace(word_list[i]);
}
return valid_words;
}
bool UBullCowCartridge::is_palindrome(const FString& s)const{
std::deque<char>normal, reverse;
for (auto& c : s) {
if (isalpha(c)) {
normal.push_back(toupper(c));
reverse.push_front(toupper(c));
}
}
if (normal == reverse)
return true;
return false;
}
bool UBullCowCartridge::is_isogram(const FString& s)const {
for (int32 i = 0; i < s.Len()-1; i++)
if (s[i] == s[i + 1])
return true;
return false;
}
void UBullCowCartridge::BeginPlay() {
Super::BeginPlay();
// Get words from txt file
TArray<FString>Words;
const FString WordListPath = FPaths::ProjectContentDir() / TEXT("Wordlists/HiddenWordList.txt");
FFileHelper::LoadFileToStringArray(Words, *WordListPath);
Words = GetValidWords(Words);
PrintLine(TEXT("The library consists of %i valid words"), Words.Num());
SetupGame();
if (is_isogram(HiddenWord))
PrintLine(TEXT("Hidden word is a isogram"));
if (is_palindrome(HiddenWord))
PrintLine(TEXT("Hidden word is a palindrome"));
if (is_palindrome(HiddenWord) && is_isogram(HiddenWord))
PrintLine(TEXT("Hidden word is a palindrome and isogram"));
PrintLine(FString::Printf(TEXT("The length of the hidden word is: %i"), HiddenWord.Len()));
}
void UBullCowCartridge::OnInput(const FString& Input) {
if (bGameOver) {
ClearScreen();
SetupGame();
}
else {
ProcessGuess(Input);
}
}
void UBullCowCartridge::SetupGame() {
HiddenWord = TEXT("cakes");
player_lives = HiddenWord.Len();
bGameOver = false;
PrintLine(FString::Printf(TEXT("Total Lives: %i"), player_lives));
PrintLine(FString::Printf(TEXT("first two characters are %c %c\nlast character is: %c "), HiddenWord[0], HiddenWord[1],HiddenWord[HiddenWord.Len()-1]));
PrintLine(TEXT("Guess the hidden word!!\nPress enter to continue"));
// PrintLine(TEXT("The hidden word is: %s"), *HiddenWord); // debug
}
void UBullCowCartridge::EndGame() {
bGameOver = true;
PrintLine(TEXT("Press enter to play again"));
}
void UBullCowCartridge::ProcessGuess(const FString &Input) {
if (Input == HiddenWord)
{
PrintLine(TEXT("You have Won!"));
EndGame();
}
else
{
PrintLine(TEXT("You have Lost!"));
PrintLine(FString::Printf(TEXT("Remaining lives: %i"), --player_lives));
if(player_lives == 0) {
PrintLine(TEXT("The hidden word was: %s"), *HiddenWord);
PrintLine(TEXT("You have no lives left!\nGame Over."));
EndGame();
}
}