Different way of if statments

I made it in different way and it is working. I hope it will work in future. :slight_smile:
codeif

Your just writing extra code…in your if statement your comparing the same thing…let me explain. In your if statement you are comparing to see if the letters between your guess and hidden word are the same and in the same location…twice. And then in your else if you are first doing it a third time and then comparing to see if they are not equal, or not the same letters in the same location. It’s rather redundant.

Try to avoid repeating actions.
You can get the same result with

if (Guess[j] == MyHiddenWord[i])
{
if (i == j)//if they’re in the same place
{
BullCowCount.Bulls++;
}else{
BullCowCount.Cows++;
}
}

Hi! I also came up with a different form of logic that (so far at least) seems to work. I’ve commented-out what was in the video lecture to highlight the difference. This to me seems to be a simpler solution that achieves the same results faster.

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

Hi Christopher,
your code is not working as intended; e.g. if the hidden word is “planet” and the user is guessing “planxy” you get 4 exact matches (char & position equals -> bulls -> “plan”), but still 2 cow matches (e t and x y don’t match at all -> no cows). Your algorithm handles anything which is not an exact match as a cow - to solve the programs requirements you need to test any chars of both strings with each other, therefore the 2nd for (nested) loop.

Hi ErnestHoro,

Yea I realised a few days after I posted this that I had misunderstood the game and therefore got that logic wrong. I just forgot to change my post to reflect that! Thanks for pointing out the actual flaw :slight_smile:

Privacy & Terms