Please help!I modified the code but it does not work(sloved)

I modified the code to hint the hidden word by changng the array of characters but it does not work! Please help.

(1.bullcowcartridge.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include “BullCowCartridge.h”

#include “HiddenWordList.h”

#include “Math/UnrealMathUtility.h”

void UBullCowCartridge::BeginPlay() // When the game starts

{

Super::BeginPlay();

Isograms = GetValidWord(Words);

SetupGame();

}

void UBullCowCartridge::OnInput(const FString& PlayerInput) // When the player hits enter

{

if (bGameOver) //If the game is over then ClearScreen() and Setupgame().

{

    ClearScreen();

    SetupGame();

}

else//If the game isn't over then check the player guess 

{   

    ProcessGuess(PlayerInput);

}

}

void UBullCowCartridge::SetupGame()

{

//Welcoming the player.

PrintLine(TEXT("Welcome to Bulls and Cows!"));

HiddenWord = Isograms[FMath::RandRange(0, Isograms.Num() - 1)];//Set the hidden word.

Lives = HiddenWord.Len();

bGameOver = false;

PrintLine(TEXT("Guess the %i letter word!"), HiddenWord.Len());

PrintLine(TEXT("You have only %i lives."), Lives);

PrintLine(TEXT("Type in your guess and\npress enter to continue....."));

Random = FMath::RandRange(MixingLettersone, MixingLetterstwo, MixingLettersthree, MixingLettersfour);

PrintLine(TEXT("This is a hint for the hidden word(Letters have been mixed.):%s"), *Random);

}

void UBullCowCartridge::EndGame()

{

bGameOver = true;

PrintLine(TEXT("Press enter to play again."));

}

void UBullCowCartridge::ProcessGuess(const FString& Guess)

{

if (HiddenWord == Guess)//Cheking the player guess.

{

    PrintLine(TEXT("Your answer is correct!"));

    EndGame();

    return;

}

if (Guess.Len() != HiddenWord.Len())

         {

             PrintLine(TEXT("Sorry,the word has %i letters."), HiddenWord.Len());

             PrintLine(TEXT("You have %i lives remaining!"), Lives);

             EndGame();

             return;

         }

    //Cheking if it is a isogrm.

  if (!IsIsogram(Guess))

{

    PrintLine(TEXT("No repeating letters,Guess again!"));

    return;

}

PrintLine(TEXT("You lost a live!"));

--Lives;

 if(Lives <= 0)

        {

          EndGame();

          PrintLine(TEXT("You have no lives!"));

          PrintLine(TEXT("The hidden word was '%s'!"), *HiddenWord);

          ClearScreen();

          return;

        }

        //show the player bulls and cows.

        FBullCowCount Score = GetBullCows(Guess);

        PrintLine(TEXT("You have %i bulls and %i cows"), Score.Bulls, Score.Cows);

        PrintLine(TEXT("Guess again,you have %i lives left."), Lives);

}

bool UBullCowCartridge::IsIsogram(const FString& Word) const

{

for (int32 Index = 0; Index < Word.Len(); Index++)

{

    for (int32 Comparison = Index + 1; Comparison < Word.Len(); Comparison++)

    {

        if (Word[Index] == Word[Comparison])

        {

            return false;

        }

        

    }

    

}

return true;

}

TArray UBullCowCartridge::GetValidWord(const TArray& WordList)

{

  TArray<FString> ValidWords;

for (FString Word : WordList)

{

    if (Word.Len() = 5 && IsIsogram(Word))

    {   

            ValidWords.Emplace(Word);

    }

}

return ValidWords;

}

FBullCowCount UBullCowCartridge::GetBullCows(const FString& Guess) const

{

FBullCowCount Count;

//For every index Guess and the index hidden word are same.then Bullcount ++.

//If not a bull is it a cow?if yes Cowcount ++.

for (int32 GuessIndex = 0; GuessIndex < Guess.Len(); GuessIndex++)

{

    if (Guess[GuessIndex] == HiddenWord[GuessIndex])

    {

        Count.Bulls++;

        continue;

    }

    for (int32 HiddenIndex = 0; HiddenIndex < HiddenWord.Len(); HiddenIndex++)

    {

        if (Guess[GuessIndex] == HiddenWord[HiddenIndex])

        {

            Count.Cows++;

            break;

        }

        

    }

    

}

    return Count;            

}

TArray UBullCowCartridge::MixingLettersone(TArray& LetterMixerone) const

{

LetterMixerone = Isograms[FMath::RandRange(0, Isograms.Num() - 1)];//Being ready to mix letters.

TCHAR HW[] = TEXT("%S"), *Random;

PrintLine(TEXT("%C%C%C%C%C"), Random[4], Random[2], Random[0], Random[1], Random[3]);

}

TArray UBullCowCartridge::MixingLetterstwo(TArray& LetterMixertwo) const

{

LetterMixertwo = Isograms[FMath::RandRange(0, Isograms.Num() - 1)];//Being ready to mix letters.

TCHAR HW[] = TEXT("%S"), *Random;

PrintLine(TEXT("%C%C%C%C%C"), Random[3], Random[2], Random[0], Random[4], Random[1]);

}

TArray UBullCowCartridge::MixingLettersthree(TArray& LetterMixerthree) const

{

LetterMixerthree = Isograms[FMath::RandRange(0, Isograms.Num() - 1)];//Being ready to mix letters.

TCHAR HW[] = TEXT("%S"), *Random;

PrintLine(TEXT("%C%C%C%C%C"), Random[3], Random[0], Random[4], Random[2], Random[1]);

}

TArray UBullCowCartridge::MixingLettersfour(TArray& LetterMixerfour) const

{

LetterMixerfour = Isograms[FMath::RandRange(0, Isograms.Num() - 1)];//Being ready to mix letters.

TCHAR HW[] = TEXT("%S"), *Random;

PrintLine(TEXT("%C%C%C%C%C"), Random[2], Random[0], Random[4], Random[3], Random[1]);

}

(2.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”

struct FBullCowCount

{

int32 Bulls = 0;

int32 Cows = 0;

};

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(const FString& Guess);

bool IsIsogram(const FString& Word) const;

TArray<FString> GetValidWord(const TArray<FString>& WordList);

FBullCowCount GetBullCows(const FString& Guess) const;

TArray<FString> MixingLettersone(TArray<FString>& LetterMixerone, TArray<FString>& Random);

TArray<FString> MixingLetterstwo(TArray<FString>& LetterMixertwo, TArray<FString>& Random);

TArray<FString> MixingLettersthree(TArray<FString>& LetterMixerthree, TArray<FString>& Random);

TArray<FString> MixingLettersfour(TArray<FString>& LetterMixerfour, TArray<FString>& Random);

// Your declarations go below!

private:

FString HiddenWord;

FString Random;

int32 Lives;

bool bGameOver;

TArray<FString> Isograms;

};

You haven’t said how it doesn’t work. Please use the </> button to create a code block.

As a cursory glance

What are you trying to do here? That code is the same as

TCHAR HW[] = *Random;

There are so many errors.I want to print the hidden word(letters) to the terminal( Not to the arrange, Only as a hint.)
So I created “MixingLettersone,MixingLetterstwo,etc.” and created a “randrange” function that randomly chooses a “MixingLetters”. But it didn’t compile.I want to know why and how can I fix it.Sorry for my English I’m Sri Lankan.

Here is my project log

Creating makefile for BullCowGameEditor (ini files are newer than makefile)
Using Visual Studio 2017 14.16.27023 toolchain (C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023) and Windows 10.0.16299.0 SDK (C:\Program Files (x86)\Windows Kits\10).
Building 5 actions with 4 processes…
[1/5] BullCowCartridge.gen.cpp
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame/BullCowCartridge.h(32) : error C2086: ‘FString UBullCowCartridge::HiddenWord’: redefinition
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame/BullCowCartridge.h(31): note: see declaration of ‘UBullCowCartridge::HiddenWord’
[2/5] BullCowCartridge.cpp
d:\unrealengine\my projects\bullcow_1\bullcowgame-starter-kit\source\bullcowgame\BullCowCartridge.h(32) : error C2086: ‘FString UBullCowCartridge::HiddenWord’: redefinition
d:\unrealengine\my projects\bullcow_1\bullcowgame-starter-kit\source\bullcowgame\BullCowCartridge.h(31): note: see declaration of ‘UBullCowCartridge::HiddenWord’
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(42) : error C2065: ‘Random’: undeclared identifier
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(42) : error C2065: ‘MixingLettersone’: undeclared identifier
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(42) : error C2065: ‘MixingLetterstwo’: undeclared identifier
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(42) : error C2065: ‘MixingLettersthree’: undeclared identifier
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(42) : error C2065: ‘MixingLettersfour’: undeclared identifier
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(43) : error C2065: ‘Random’: undeclared identifier
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(114) : error C2106: ‘=’: left operand must be l-value
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(146) : error C2039: ‘MixingLettersone’: is not a member of ‘UBullCowCartridge’
d:\unrealengine\my projects\bullcow_1\bullcowgame-starter-kit\source\bullcowgame\BullCowCartridge.h(16): note: see declaration of ‘UBullCowCartridge’
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(147) : error C2270: ‘MixingLettersone’: modifiers not allowed on nonmember functions
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(148) : error C2065: ‘Isograms’: undeclared identifier
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(148) : error C2661: ‘FMath::RandRange’: no overloaded function takes 1 arguments
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(150) : error C3861: ‘PrintLine’: identifier not found
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(152) : error C2039: ‘MixingLetterstwo’: is not a member of ‘UBullCowCartridge’
d:\unrealengine\my projects\bullcow_1\bullcowgame-starter-kit\source\bullcowgame\BullCowCartridge.h(16): note: see declaration of ‘UBullCowCartridge’
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(153) : error C2270: ‘MixingLetterstwo’: modifiers not allowed on nonmember functions
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(154) : error C2065: ‘Isograms’: undeclared identifier
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(154) : error C2661: ‘FMath::RandRange’: no overloaded function takes 1 arguments
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(156) : error C3861: ‘PrintLine’: identifier not found
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(158) : error C2039: ‘MixingLettersthree’: is not a member of ‘UBullCowCartridge’
d:\unrealengine\my projects\bullcow_1\bullcowgame-starter-kit\source\bullcowgame\BullCowCartridge.h(16): note: see declaration of ‘UBullCowCartridge’
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(159) : error C2270: ‘MixingLettersthree’: modifiers not allowed on nonmember functions
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(160) : error C2065: ‘Isograms’: undeclared identifier
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(160) : error C2661: ‘FMath::RandRange’: no overloaded function takes 1 arguments
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(162) : error C3861: ‘PrintLine’: identifier not found
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(164) : error C2039: ‘MixingLettersfour’: is not a member of ‘UBullCowCartridge’
d:\unrealengine\my projects\bullcow_1\bullcowgame-starter-kit\source\bullcowgame\BullCowCartridge.h(16): note: see declaration of ‘UBullCowCartridge’
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(165) : error C2270: ‘MixingLettersfour’: modifiers not allowed on nonmember functions
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(166) : error C2065: ‘Isograms’: undeclared identifier
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(166) : error C2661: ‘FMath::RandRange’: no overloaded function takes 1 arguments
D:\unrealengine\my projects\BullCow_1\BullCowGame-starter-kit\Source\BullCowGame\BullCowCartridge.cpp(168) : error C3861: ‘PrintLine’: identifier not found

Could you provide your whole file please?

https://drive.google.com/file/d/1gkCaDC2DOdOMpNrH3SGCN4sZa-bfBzuX/view?usp=sharing

https://drive.google.com/file/d/1JGzi35NejCWx8rqn5MXqMplvxAJCRsyd/view?usp=sharing
*Here are the google drive links for the project . One link is for cpp files and I cannot realise what is for the 2nd link.I just uploaded them as one file but after uploading there are two files.

I meant the code.

  • I just uploaded Bull cow game source file to the google drive.Here is the link.There were some c# and c++ files.

https://drive.google.com/drive/folders/1V2RdaHfKvYdepOrPSuYzFpdzmtezm7fv?usp=sharing

There only seems to be the issue I originally pointed out. So to be clear you just want to print a random character in the word?

Yes,I want to print characters of the word in to a random array to the terminal.

It’s not quite clear to me what you mean. If you want to print a random character from HiddenWord then you would just need

const int32 RandIndex = FMath::RandRange(0, HiddenWord.Len() - 1);
PrintLine(TEXT("Random character: %c"), HiddenWord[RandIndex]);

Thank you,that is what I quiet want but I want a for loop wich checks wheather the letter has gone through or not in order to do my modification(beacuse it will return the same letter, won’t it).I can’t acctually imagine the code.If you can provide a hint or the for loop, it’ll be a great help.

I have another problem also.What “continue” does.I can’t quiet understand what it does in a for loop.Is it iterate the for loop?

This is much clearer, thank you. Please state this kind of information to begin with in the future as your original post did not indicate this at all.

Just to be extra clear, you don’t want the same random character to be printed? So you can only go through all characters once? In that case you can just create a member variable that’s a copy of HiddenWord ands call RemoveAt on it after printing the character e.g.

// In SetupGame
HintCharacters = HiddenWord;

// When printing the hint
const int32 RandIndex = FMath::RandRange(0, HiddenWord.Len() - 1);
PrintLine(TEXT("Random character: %c"), HiddenWord[RandIndex]);
HintCharacters.RemoveAt(RandIndex);

It goes directly to the ending } of the loop.

for (int i = 0; i < 10; ++i)
{
    if (i == 2)
    {
       continue;
    }
    std::cout << i << '\n'
/* continue  comes here */}

So that would print

0
1
3
4
5
6
7
8
9

As when i == 2 it will go directly to the end skipping the print statement, then the loop will continue by incrementing i and checking the condition.

Thank you for the great help.

1 Like

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

Privacy & Terms