A more intuitive loop and if statement setup

Ben and other students,

I came up with a different solution that seems to return the correct number of cows and bulls. It depends on the Guess length being equal to the MyHiddenWord length, which we assumed. I didn’t test this too rigorously and it may turn out to be flawed. If so I will find out soon enough.

(Sorry, I thought the text would stay formatted. Here’s a screenshot for visual purposes: Imgur )

for (int32 i = 0; i < HiddenWordLength; i++)
{
    if (MyHiddenWord[i] == Guess[i]) { BullCowCount.Bulls++; }
    else 
    {
        for (int32 j = 0; j < HiddenWordLength; j++)
        {
            if (MyHiddenWord[i] == Guess[j]) { BullCowCount.Cows++;}
        }
    }
}

For formatting indent with four spaces or surround with 3 backticks like
```
code here
```

I don’t believe that will work because your second for loop will iterate through"Guess" and “MyHiddenWord” will stay fixed to the the i index and possible give false Bulls.

For example.

MyHiddenWord= planets
Guess= hip

Your first If statement will be false and jump to the next for loop because “p” and “h” do not match.
“p” and “i” will not match.
“p” and “p” suddenly do match. Hmm…but the "p"s are in different positions…that’s a logic error. At this point in the course the game is very simple but it should at least only give bulls when letters match in the correct position. Later on you could possibly give letters that match but in different position.

Hope this made sense.

1 Like

Privacy & Terms