Conditional solution

void UBullCowCartridge::CalculateBulls(FString const& Guess, int32& Bulls, int32& Cows) const
{
	Bulls = 0;
	Cows = 0;
	for (int32 Index = 0; Index < Guess.Len(); ++Index)
	{
		Guess[Index] == HiddenWord[Index] ? ++Bulls : ++Cows;
	}
}

so what’s going on here?
IsConditionTrue ? IfTrueDoThis : IfFalseDoThat
quite useful if you want to shorten code or give conditional argument for example

PrintLine(bGameWon ? TEXT("You WON!") : TEXT("YOU LOST!"));

more info here:
https://en.cppreference.com/w/cpp/language/operator_other

1 Like

Nice solution

Unfortunately your logic is wrong. You’re always incrementing Cows when the letter is not a bull.
So if the HiddenWord is “own” and the Guess is “car” your logic will still get us 3 Cows which is wrong.

Ah I didn’t understand what’s wrong at first. But after looking at wiki to remember the game rules I see you’re right, if we look at wikipedia cows is when number is right but different spot.

My first connection was fallout’s hacking game and i went with that logic. But clearly I was wrong with my understanding of the problem.

Privacy & Terms