Different way to know the winning guess

When trying to solve the challenge I came up with the solution I used in the function below. Please let me know what issue will arise if I stick to it.

// receives VALID guess, increments turn, and return counts
FBullCowCount FBullCowGame::SubmitValidGuess(FString Guess)
{
	MyCurrentTry++;
	FBullCowCount BullCowCount;

	//loop through all letter in the hidden word
	int32 WordLenght = MyHiddenWord.length(); //assuming same length as guess
	for (int32 MHWChar = 0; MHWChar < WordLenght; MHWChar++) {
		//compare letters against the guess
		for (int32 GChar = 0; GChar < WordLenght; GChar++) {
			//if they match then
			if (Guess[GChar] == MyHiddenWord[MHWChar]) {
				//increment bulls if they're in the same place
				if (MHWChar == GChar) {
					BullCowCount.Bulls++;
					bIsGameWon = BullCowCount.Bulls == WordLenght;
				}
				//increment cows if not
				else {
					BullCowCount.Cows++;
				}
			}
		}
	}
	return BullCowCount;
}
1 Like

Hi Mario,

I believe the main issue here is that you’d be checking if the game is won EVERY time a letter between the isograms matches. This is inefficient logic due to the fact that we KNOW that we must check every letter in the word before we can check the sufficient winning condition (that the number of bulls == the hidden word length).

By only checking after all of the bulls/cows have been calculated, we save whatever minuscule amount of time it takes to check the winning condition on the checking of every letter.

Ultimately, I would say it’s not a logic error, but an efficiency error. The more complex programs become, the longer the calculation/processing time can get between user input/interaction, which is something we’ll definitely be concerned with as video game programmers.

1 Like

Hello @Mario_Lintang and @MJPIII,

according to the context of performance in this case:

You could set bIsGameWon = false; if there is at least one cow. You can end-up with a break but the game will not tell you how accurate the remaining letters in the guessed word are.

Kind regards
Kevin

Privacy & Terms