Something cool (I hope!)

Something I added that I thought was quite cool, let me know if it’s stupid or wrong!

So I wanted to let the user know how many tries it took them to win, so I added this code:

void PrintGameSummary()
{
	if (BCGame.IsGameWon())
	{
		if (BCGame.GetCurrentTry() == 1)
		{
			std::cout << "Congratulations, you got it! It only took you " << BCGame.GetCurrentTry() << " try." << std::endl;
			std::cout << std::endl;
		}
		else
		{
			std::cout << "Congratulations, you got it! It only took you " << BCGame.GetCurrentTry() << " tries." << std::endl;
			std::cout << std::endl;
		}
	}
	else
	{
		std::cout << "Bad luck, you've exceeded your tries. Maybe have another go!" << std::endl;
		std::cout << std::endl;
	}
	return;
}

Is it better to do that compare in a switch statement maybe?

As a result of this I noticed an issue in that MyCurrentTry was incrementing at the very start of the SubmitValidGuess method, which meant that the game thought you were on turn 2 even if you won on the first go. To resolve this I move the MyCurrentTry++ down to the if statement at the end:

if (BullCowCount.Bulls == WordLength)
	{
		bGameIsWon = true;
	}
	else
	{
		bGameIsWon = false;
		// increment the try number
		MyCurrentTry++;
	}

Hi Jack, I’ve taken the liberty to edit your post using code formatting. I hope you don’t mind, it makes it much easier to read.

Your code looks fine. You could argue the "Congratulations, you got it! It only took you " bit is repeated, so just put the code that separates try from tries in a separate conditional, but it’s fine.

Thanks very much for taking the time to respond :slight_smile:

void PrintGameSummary()
{
    if (BCGame.IsGameWon())
    {
	    std::cout << "Congratulations, you got it! It only took you " << BCGame.GetCurrentTry();
	    if (BCGame.GetCurrentTry() == 1)
	    {
		    std::cout << " try!" << std::endl;
	    }
	    else
	    {
		    std::cout << " tries!" << std::endl;
	    }
    }
    else
    {
	    std::cout << "Bad luck, you've exceeded your tries. Maybe have another go!" << std::endl;
	    std::cout << std::endl;
    }
    return;
}

Awesome, well done

Privacy & Terms