For some reason the number of bulls and cows in unreal always adds up to 4. All my words are 5-digit isograms so I find it mysterious why I am wrong?
Below is my code:
#include “BullCowCartridge.h”
#include “HiddenWordList.h”
#include “Math/UnrealMathUtility.h”
void UBullCowCartridge::BeginPlay()
{
Super::BeginPlay();
Isograms = GetValidWords(Words);
//PrintLine(TEXT("%i"),FMath::RandRange(0, 10));
SetupGame();
//PrintLine(TEXT(“The number of possible words is: %i”), Words.Num());
//PrintLine(TEXT(“The number of valid words is: %i”), GetValidWords(Words).Num());}
void UBullCowCartridge::OnInput(const FString& PlayerInput) // When the player hits enter
{if(bGameOver == true)
{ClearScreen(); SetupGame();
}
else
{
ProcessGuess(PlayerInput);
}
}void UBullCowCartridge::SetupGame()
{
PrintLine(TEXT(“Welcome to New Bull Cows.\n”));
HiddenWord = Isograms[FMath::RandRange(0, Isograms.Num()- 1)];
lives = HiddenWord.Len();
bGameOver = false;
PrintLine(TEXT(“Guess the %i letter word”),HiddenWord.Len());
PrintLine(TEXT(“You have %i lives in the game.”), lives);
PrintLine(TEXT(“Type in your guess and then press enter.\n”));
PrintLine(TEXT("%s"), *HiddenWord);}
void UBullCowCartridge::EndGame()// Hi
{bGameOver = true;
PrintLine(TEXT(“Press enter to play again.”));}
void UBullCowCartridge::ProcessGuess(const FString& Guess)
{
if (Guess == HiddenWord)
{PrintLine(TEXT("You have won")); EndGame(); return; } if(Guess.Len() != HiddenWord.Len()) { PrintLine(TEXT("Sorry try guessing again! \n You have %i lives remaining."),lives); PrintLine(TEXT("The HiddenWord is %i long."), HiddenWord.Len()); return; } if(!IsIsogram(Guess)) { PrintLine(TEXT("There is no repeating characters. Guess again")); return; } PrintLine(TEXT("You lost a life!")); --lives; if(lives <= 0 ) { ClearScreen(); PrintLine(TEXT("You have no lives left!")); PrintLine(TEXT("The hidden word was %s"), *HiddenWord); EndGame(); return; } int32 Bulls,Cows; GetBullCows(Guess,Bulls, Cows); PrintLine(TEXT("You have %i Bulls and %i Cows."), Bulls, Cows); PrintLine(TEXT("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;} }
}
/* //for(int32 Index = 0; Index < Word.Len(); Index++)
{PrintLine(TEXT("%c"), Word[Index]);
} */
// For each individual letter
//Start at element[0]
//Compare against the next letter
//Until we reach [Word.Len()-1]
//if any are the same return falsereturn true;
}
TArray UBullCowCartridge::GetValidWords(const TArray& WordList) const
{
TArray ValidWords;
for (FString Word : WordList)
{
if(Word.Len()>= 4 && Word.Len()<= 8 && IsIsogram(Word) )
{
ValidWords.Emplace(Word);}
}
return ValidWords;
}
void UBullCowCartridge::GetBullCows(const FString& Guess, int32& BullCount, int32& CowCount) const
{
BullCount = 0;
CowCount = 0;// for every index Guess is same as index Hidden, BullCount ++ // if not a bull was it a cow? if yes CowCount ++ for (int32 GuessIndex = 0; GuessIndex < Guess.Len(); GuessIndex++) { if (Guess[GuessIndex] == HiddenWord[GuessIndex]) { BullCount ++; continue; } for (int32 HiddenIndex = 0; HiddenIndex < HiddenWord.Len(); HiddenIndex++) { if (Guess[GuessIndex] == HiddenWord[HiddenIndex]) { CowCount ++; } } }
}