Here is my partial code. I used a struct to pass GetBullCow() function; also using FStringView and used FindChar instead of second loop; this is the part from ProcessGuess();
FBullCowCount Count;
GetBullCows(Input, Count);
PrintLine(TEXT("Bulls: %i, Cows: %i"), Count.Bulls, Count.Cows);
FBullCowCount is a struct defined in header file and i create an instance of it then pass it to function GetBullCows() . Here is my header file ;
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;
virtual void OnInput(FStringView Input) override;
void SetupGame();
void EndGame();
void ProcessGuess(FStringView Guess); // Guess=Input
void GetValidWords();
bool IsIsogram(FStringView Guess) const;
void GetBullCows(FStringView Guess, FBullCowCount& count);
private:
FString HiddenWord;
int32 Lives{0};
bool bGameOver{false};
TArray<FString>WordList;
};
And here is the GetBullCow() function ;
void UBullCowCartridge::GetBullCows(FStringView Guess, FBullCowCount& Count)
{
int32 Position{ 0 };
for (int32 Index = 0; Index < Guess.Len(); ++Index)
{
HiddenWord.FindChar(Guess[Index], Position);
if (Position == Index)
++Count.Bulls;
else if (Position != INDEX_NONE)
++Count.Cows;
}
}
FindChar() is a member function for FStringView or FString that takes a char; Guess[Index]
Position is a local int32 variable passed to FindChar(). This function looks for the character and the index of character is stored in the passed Position .
So if Position ==Index that means we have a cow;
if the character is not found Position == INDEX_NONE which means it does not exit
So in the else condition if Position != INDEX_NONE ; it means it exist but in a different position so we have cow.