I did the challenge completely different isn't mine more encapsulated?

During this section for fixing GetGuessAndPrintBack();
i moved string Guess = “”; to the top with the rest of the prototypes.
then i moved the Printback code into its own string and put that string in Play Game Loop.
im not really sure why this isnt the prefered solution as it is more encapsulated. my question is am i misunderstanding something?

#include
#include

using namespace std;
void PrintIntro();
void PlayGame();
string Guess = “”;
string GetGuess();
string PrintBack();

//Entry point for the game
int main()
{
PrintIntro();
PlayGame();
return 0;
}

void PrintIntro()
{
// introduce the game
constexpr int WORD_LENGTH = 5;
cout << “Welcome to Bulls and Cows, a fun word game.\n”;
cout << “Can you guess the " << WORD_LENGTH;
cout << " letter isogram I’m thinking of?\n\n”;
return;
}
void PlayGame()
{
//Loop for turns asking guess
constexpr int NUMBER_OF_TURNS = 5;
for (int count = 1; count <= NUMBER_OF_TURNS; count++)
{
GetGuess();
PrintBack();
cout << endl;
}
}
string GetGuess()
{
// get a guess from the player
cout << "Enter your guess: ";
getline(cin, Guess);
return Guess;
}
string PrintBack()
{
// print the guess back to player
cout << "You Guessed: " << Guess << endl;
return Guess;
}

This is just my opinion, but I think it isn’t neccesary to create a function for that line because print back the guess isn’t a ‘side effect’ in the PlayGame method in that format of the code. But I also create a method for it at first, haha. :smiley:

Privacy & Terms