Skip to content

Instantly share code, notes, and snippets.

@nethmunson
Created March 1, 2017 04:27
Show Gist options
  • Save nethmunson/6f4986e5b4336a4094739e5624a7a4a5 to your computer and use it in GitHub Desktop.
Save nethmunson/6f4986e5b4336a4094739e5624a7a4a5 to your computer and use it in GitHub Desktop.
Section 2 Chapter 23 Neth Munson
#include <iostream>
#include <string>
using namespace std;
void PrintIntro();
void PlayGame();
string GetGuess();
bool AskToPlayAgain();
int main()
{
PrintIntro();
PlayGame();
AskToPlayAgain();
cout << endl;
return 0; // Exit the application
}
// Introdue the game
void PrintIntro()
{
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";
cout << endl;
return;
}
// Loop for the number of turns asking for guesses
void PlayGame()
{
constexpr int NUMBER_OF_TURNS = 3;
for (int count = 1; count <= NUMBER_OF_TURNS; count++)
{
string strGuess = GetGuess();
cout << "Your guess was: " << strGuess << endl;
cout << endl;
}
}
// Get a guess from the player
string GetGuess()
{
cout << "Enter your guess: ";
string strGuess = "";
getline(cin, strGuess);
return strGuess;
}
// Asks if the player wants to play again
bool AskToPlayAgain()
{
cout << "Do you want to play again (y/n)? ";
string Response = "";
getline(cin, Response);
// Desides if the first char is a Y or N
if (Response[0] == 'Y' || Response[0] == 'y')
{
cout << endl;
PlayGame();
AskToPlayAgain();
}
else if (Response[0] == 'N' || Response[0] == 'n')
{
cout << "Thanks for playing!\n";
}
else
{
do{
// Validation Message
cout << "Incorrect parameter, please type YES or NO\n\n";
cout << "Do you want to play again? ";
getline(cin, Response);
} while (Response[0] != 'Y' && Response[0] != 'y' && Response[0] != 'N' && Response[0] != 'n');
}
cout << endl;
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment