# Different method for determining Bulls and Cows

Greetings!

Just in the IF statements section here, I looked at it and thought there is likely to be a logic problem with the double loop, so I decided to use the std::string::find function instead and evaluate the results. The find function is very efficient, and this takes the place of the second loop and is more useful IMHO. I could be wrong - not necessarily the “world’s best programmer.” I do tend to be a bit brutish with coding.

My code:

[CODE]
void FBullCowGame::FindBullsAndCows()
{
BCCount.BullCount = 0;
BCCount.CowCount = 0;

``````for (int32 i = 0; i < CurrentGuess.length(); i++)
{
std::size_t Position = FString::npos;
Position = CurrentWord.find(CurrentGuess[i]);
//std::cout << "Char: " << CurrentGuess[i] << ", Position: " << Position << ", " << i << "\n";
if (Position == i)
BCCount.BullCount++;
else
if (Position != FString::npos)
BCCount.CowCount++;
}
return;
``````

}
[/CODE]

NOTE: the commented cout was just so I could verify I had it correct, just debugging there.

Jon

Having seen the later part of the double loop, “I see what he did there.”

Funny, I actually had the same thought and came here to see if anyone else had done it. The only difference in your implementation would be that it wouldn’t count repetitions of the same letter in the guess, where the function he showed would. I’m not far enough into the program to tell if that even matters (EDIT: just got far enough to see that the guess does need to be an isogram, making this irrelevant, but I’ll leave it up since it could be helpful in other situations), but I implemented mine with a loop that should be functionally identical to what he gave us:

``````FBullCowCount FBullCowGame::SubmitGuess( FString Guess )
{
PlayerCurrentTry++;

FBullCowCount BCCount;
int32 WordLength = HiddenWord.length();
for ( int32 i = 0; i < WordLength; i++ )
{
int32 GuessIndex = Guess.find( HiddenWord[i] );

while ( GuessIndex != -1 )
{
if ( GuessIndex == i ) BCCount.Bulls++;
else BCCount.Cows++;

GuessIndex = Guess.find( HiddenWord[i], GuessIndex + 1 );
}
}

return BCCount;
}``````

Yes, with your code, the assumption is a valid guess has been submitted. So, in a way, that assumption does matter - if the number of letters > 1, then it isn’t an isogram and the guess is invalid.

Also, for clarity, my function only locates bulls/cows… Additionally, I did this well before seeing what the instructor finally did.

Nice job btw.

1 Like