Code is not working correctly

.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

	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!
		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)
    return valid_words;

bool UBullCowCartridge::is_palindrome(const FString& s)const{
    std::deque<char>normal, reverse;

    for (auto& c : s) {
        if (isalpha(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() {

    // Get words from txt file
    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());

    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) {
    else {
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!"));
        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."));


Ekran görüntüsü 2021-05-22 182901

I presume you’re talking about the 0 valid words?

Could you ensure that you have named the file correctly? Could you show it in File Explorer?

On a side note I do have some comments about your code.

  1. You have inconsistent naming. Ideally you should prefer Unreal’s coding standard as that would make your code more consistent. Right now you are using a mix e.g. valid_words vs Words, SetupGame vs is_palindrome.
  2. is_palindrome:
    • It is not platform independent as it is using char instead of TCHAR
    •  if (normal == reverse)
         return true;
       return false;
      This can just be
      return normal == reverse;
    • The function can actually be further simplified to just
      return Str == Str.Reverse();
    • With that said, that is performing a dynamic allocation and a copy of the string for something that just throws it away after doing the comparison. Doing the checks via iteration of each character from either end allows you to return early e.g. if FirstChar != LastChar would immediately return false. It would be saving a bunch of work vs your current implementation. I’ll leave implementing that for yourself :slight_smile: .
    • More of a side note, std::deque has horrendous performance in Microsoft’s implementation due to a ridiculously small block size. (see microsoft/STL/issues/147 for more info). I believe libc++/clang has similar issues but not as drastic as Microsoft’s.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms