A different solution to this Submit challenge

As a new programmer; I struggled with this challenge for over an hour on deciding how this was going to be accomplished.

The first issue I had was figuring out how i could get the user input into our Submitguess function. (I had no idea I could just write ‘guess’ after the Fstring to create it as an internal variable of the function.

My solution? First I went into out Main.cpp’s main function and completely removed the guess input from the player; parts used in the loop for input and the rest incorporated it in the header file under a new private variable set by a new public function.

void LoopingGuess()
{
	BCGame.Reset();
	int32 CurrentTry = BCGame.GetCurrentTry();
	int32 MaxTries = BCGame.GetMaxTries();

	//Loop for user input

	for (int32 count = 1; count <= MaxTries; count++)
	{
		// Set Try number
		int32 CurrentTry = BCGame.GetCurrentTry();
		std::cout << "\nTry " << CurrentTry << " : ";
		
		//Submit Player Guess
		FText Guess = "";
		Guess = BCGame.CurrentGuess();

		//TODO make a loop checking valid Guess

		//Check for Bulls and Cows
		FBullCowCount BCCount = BCGame.SubmitGuess(Guess);
		// Print Bulls and Cows
		std::cout << "Bulls : " << BCCount.Bulls;
		std::cout << "   Cows : " << BCCount.Cows << "\n";
	
	}

So then the new class function is simple enough:

Fstring FBullCowGame::CurrentGuess()
{
	std::getline(std::cin, pGuess);
	return pGuess;
}

As for how i built the final submit guess :

for (int32 i = 0; i < HiddenWordLength; i++)
	{
		// compare letters against hidden word 
		//If they match in the right place; +1 to Bulls
		if (pGuess[i] == HiddenWord[i])
		{
			BullCowCount.Bulls++;
		}
		//If they match in the wrong place; +1 to cows
		else
		{
			for (int32 j = 0; j < HiddenWordLength; j++)
			{	//if they match in the right place

				if (pGuess[i] == HiddenWord[j])
				{
					BullCowCount.Cows++;
				};
			}
		};
	}

So the biggest difference here is that in my version; the “J” variable is not created during a ‘Bull’

Anyway; just super proud that my solution worked; and didn’t suffer the bugs that was in the ending of this tutorial.

Privacy & Terms