Alternative solution using 'find'

// Recieves a valid guess, increments turn and returns count
FBullCowCount FBullCowGame::SubmitGuess(FString Guess)
{
	int32 HiddenWordLength = static_cast<int32>(MyHiddenWord.length());
	// increments the turn no.
	MyCurrentTry++;
	// set up return  variable
	FBullCowCount BullCowCount;
	// loop through all letters in the guess
	for (int32 i = 0; i < HiddenWordLength; i++)
	{
		// charactars match at given positon
		if(Guess[i] == MyHiddenWord[i])
		{
			BullCowCount.Bulls++;
		}
		// otherwise, character is found somewhere within the hidden word
		// also works with: MyHiddenWord.find(Guess[i]) != -1)
		else if (MyHiddenWord.find(Guess[i]) != std::string::npos)
		{
			BullCowCount.Cows++;
		}
		// else character does not match anything in hidden word,
		// don't increment either bulls or cows
	}
	return BullCowCount;
}
1 Like

Privacy & Terms