Fixed my code here

So I had the problem with the Cows adding up too fast, and the solution in the video was correct, and I came to that conclusion before the challenge. However, my cow’s were still stacking up incorrectly (damn things always piling up…) and I couldn’t figure it out, until I looked at the placement of all of my closing brackets ‘}’ .

Here’s my code block now (exactly how it is in the video)

//receives a VALID guess, increments turn, and returns count
FBullCowCount FBullCowGame::SubmitGuess(FString Guess)
{
//increment the turn number
MyCurrentTry++;

//setup a return variable
FBullCowCount BullCowCount;

//loops through all letters in the guess
int32 HiddenWordLength = MyHiddenWord.length();
for (int32 MyHChar = 0; MyHChar < HiddenWordLength; MyHChar++) 
{
	for (int32 GChar = 0; GChar <HiddenWordLength; GChar++) //compares letters against the hidden word
	{
		if (MyHiddenWord[MyHChar] == Guess[GChar]) // if they match then
		{
			if (MyHChar == GChar) // if they're in the same place
			{
				BullCowCount.Bulls++; // increments bulls
			}
			else 
			{
				BullCowCount.Cows++; //increments cows 
			}
		}				
	}
}

return BullCowCount;

}

Here’s the block before, that was giving me trouble.

//receives a VALID guess, increments turn, and returns count
FBullCowCount FBullCowGame::SubmitGuess(FString Guess)
{
//increment the turn number
MyCurrentTry++;

//setup a return variable
FBullCowCount BullCowCount;

//loops through all letters in the guess
int32 HiddenWordLength = MyHiddenWord.length();
for (int32 MyHChar = 0; MyHChar < HiddenWordLength; MyHChar++) 
{
	for (int32 GChar = 0; GChar <HiddenWordLength; GChar++) //compares letters against the hidden word
	{
		if (MyHiddenWord[MyHChar] == Guess[GChar]) // if they match then
		{
			if (MyHChar == GChar) // if they're in the same place
			{
				BullCowCount.Bulls++; // increments bulls
			}
                    }       <----------------- this guy for the first IF statement was out of place and causing it to jump to counting the cow's too much
			else 
			{
				BullCowCount.Cows++; //increments cows 
			}				
	}
}

return BullCowCount;

}

The reason it was jumping to the cows so early looks to be that it was checking if MyHChar matched GChar, and when they did it would skip the next if statement to check if they were in the same place and instead increment the cows, THEN go back and check if they were in the same place and continue over and over.

I just simply dropped that closing bracket down to encapsulate the else statement as well, and viola that fixed it.
Then I felt a little dumb and almost didn’t put this up, but I figured “hey, if it helps someone else.”, and decided to do it anyway.

Hope that helped! Good Luck!

Privacy & Terms