My logic :)

This is the logic I came up with before I move onto the next lesson. I tested it and it seems to work as intended. Does anyone have anything different outside of what is done in the video?

// receives a valid guess, increments turn, and returns count
FBullCowCount FBullCowGame::SubmitGuess(FString guess)
{
	// increment the return #
	MyCurrentTry++;

	// setup a return variable
	FBullCowCount BullCowCount;
	int32 HiddenWordLength = MyHiddenWord.length();
	int32 GuessingWorldLength = guess.length();

	// loop through all letters in the guess
	for (int32 i = 0; i < HiddenWordLength; i++)
	{
		// compared letters against the hidden word
		for (int32 j = 0; j < HiddenWordLength; j++)
		{

			// if they match then
			if (GuessingWorldLength == HiddenWordLength)
			{
				// if they're in the same place
				if ((guess[i] == MyHiddenWord[j]) && (i == j))
				{
					// increment bulls
					BullCowCount.Bulls++;
				}
				// else
				else if (guess[i] == MyHiddenWord[j])
				{
					// increment cows
					BullCowCount.Cows++;
				}
			}
		}
	}

	return BullCowCount;
}

Makes sense to me, and it’s actually easier to follow in my opinion. Despite what they may say about it being “clever” code.

I am having a hangup I want to discuss though. The third statement: if (GuessingWorldLength == HiddenWordLength) is only comparing the length of the strings, right?

Could we see if the lengths of the hidden word, and the submitted guess are the same length, then run our 2 for loops to get our result?

Correct about those two comparing length of strings. Also, the only way would know if the two are the same length is if the if statement is executed resulting in it being true.

if (true)
{
// run this code…
}

Yea I find Your loop more readable.
i went with an even more clean solution (for my eyes):

for (int32 i = 0; i < HiddenWordLength; i++)
{	
	// increment bulls if they're in same place
	if (Guess[i] == MyHiddenWord[i])
	{
		BullCowCount.Bulls = BullCowCount.Bulls++;
	}
	for (int32 j = 0; j < HiddenWordLength; j++)
	{
		// increment cows if they're same but not in same place
		if (i != j && Guess[j] == MyHiddenWord[i])
		{
			BullCowCount.Cows = BullCowCount.Cows++;
		}

	}
}

Privacy & Terms