Once I type in a guess in my console, it outputs # bulls and # cows, but the amount of those remains the same for any further guesses

I have written all the code correctly, checked it several times

// receives a VALID guess, incriments turn, and returns count
FBullCowCount FBullCowGame::SubmitGuess(FString Guess)
{
    // incriment the turn number
    MyCurrentTry++;

    // setup a return variable
    FBullCowCount BullCowCount;
    // loop through all letters in the guess
    int32 HiddenWordLength = MyHiddenWord.length();
    for (int32 MHWChar = 0; MHWChar < HiddenWordLength; MHWChar++) {
	    // compare letters against the hidden word
	    for (int32 GChar = 0; GChar < HiddenWordLength; GChar++) {
		    // if they match then
		    if (Guess[GChar] == MyHiddenWord[MHWChar]) {
			    if (MHWChar == GChar) { // if they're in the same place
				    BullCowCount.Bulls++; // incriment bulls
			    }
			    else {
				    BullCowCount.Cows++; // must be a cow
			    }
		    }
	    }
    }
    return BullCowCount;
}

I’ve tried to reset the bulls and cows to 0 before the first for loop, but it’s not working. Ben’s code is exactly the same as mine, but his code is running well and mine isn’t. I was using the 2017 version of VS. There are no errors or warnings popping up in the bottom tab. Please help me, I’m clueless.

Other than a bug in the first loop (we really should be looping over the length of Guess) the code looks fine to me, <- You can ignore this bug, they discuss a solution later :wink:
I suspect your problem lies elsewhere…
Can you link your PlayGame() function?

Regards

Keith

Hey, thanks for replying so quickly. Here it is.

    void PlayGame()
{
    // TODO add a game summary.
    BCGame.Reset();
    int32 MaxTries = BCGame.GetMaxTries();
    std::cout << "The number of guesses you have is " << MaxTries << std::endl << std::endl;
    FText Guess = GetGuess(); // TODO change from for to while loop once we are validating tries.
    for (int32 i = 0; i <= MaxTries; i++)		// TODO make loop check valid guesses
    {
	    // Submit valid guess to the game.
	    FBullCowCount BullCowCount = BCGame.SubmitGuess(Guess);
	    // Print32 number of bulls/cows
	    std::cout << "Bulls = " << BullCowCount.Bulls;
	    std::cout << " . Cows = " << BullCowCount.Cows << "." << std::endl;
	    FText guess = GetGuess();
	    std::cout << "Your guess was: " << guess << std::endl << std::endl;
    }
}

What do you think?

OK I see whats happening
Remove the Ftext Guess = GetGuess(); statement above the for loop

FText Guess = GetGuess(); should be the first line your for loop, remove it from its current position and move it up to the start of the loop.

void PlayGame()
{
// TODO add a game summary.
BCGame.Reset();
int32 MaxTries = BCGame.GetMaxTries();
std::cout << "The number of guesses you have is " << MaxTries << std::endl << std::endl;
for (int32 i = 0; i <= MaxTries; i++) // TODO make loop check valid guesses
{
FText Guess = GetGuess(); // <-----------------
// Submit valid guess to the game.
FBullCowCount BullCowCount = BCGame.SubmitGuess(Guess);
// Print32 number of bulls/cows
std::cout << "Bulls = " << BullCowCount.Bulls;
std::cout << " . Cows = " << BullCowCount.Cows << “.” << std::endl;
std::cout << "Your guess was: " << guess << std::endl << std::endl;
}
}

Try this and let me know what happens (note the second string guess you declared is not the same as Guess (case sensitve, so its never used as you pass Guess string not guess string as a parameter to SubmitGuess(args))
Regards

Keith

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms