My attempt at Lecture 25

// ...

#include <iostream>

using namespace std;

void GameLoop();
void PrintIntro();
void PlayGame();
bool AskToPlayAgain();
string GetGuess();
void PrintGuess(string Guess);

int main(int argc, const char * argv[])
{
	GameLoop();
	// exit application
    return 0;
}

void GameLoop()
{
	bool bPlayAgain = false;
	
	do
	{
		PrintIntro();
		PlayGame();
		bPlayAgain = AskToPlayAgain();
	}
	while(bPlayAgain);
}

void PrintIntro()
{
	constexpr int WORD_LENGTH = 9;
	
	cout << "Welcome to Bulls and Cows, a fun word game!\n\n";
	cout << "Note: isogram is a word without a repeating letter.\n";
	cout << "Can you guess the " << WORD_LENGTH <<" letter isogram I'm thinking of?\n";
	cout << endl << endl;
	return;
}

void PlayGame() {
	constexpr int NUMBER_OF_TURNS = 5;
	
	// loop for number of turns asking for guesses
	for (int count = 0; count < NUMBER_OF_TURNS; count++)
	{
		string Guess = GetGuess();
		PrintGuess(Guess);
	}
	cout << endl;
	
	return;

}

bool AskToPlayAgain()
{
	cout << "Game over!\n\n";
	cout << "Would you like to play again? (y/n): ";
	string Response = "";
	getline(cin, Response);
	bool bPlayAgain = (Response[0] == 'y' || Response[0] == 'Y');
	cout << endl << endl;
	return bPlayAgain;
}

string GetGuess() {
	// returns guess made by player
	string Guess = "";
	cout << "Enter your guess: ";
	getline(cin, Guess);
	return Guess;
}

void PrintGuess(string Guess)
{
	// repeat the guess back to player
	cout << "Your guess was: " << Guess << endl << endl;

}
1 Like

Hello @carleneconner,

I suggest to use this form:

int main()
{
	do {
		printIntro();
		PlayGame();
		
	} while (AsktoPlayAgain());
	return 0;
}

“==true” can be avoided because it’s implicit.

Kind regards
Kevin

Privacy & Terms