Main.cpp
//Trying to figure out why the bulls and cows are printed off multiple times…
/*This is the console executable, that makes use of the BullCow Class
This acts as the view in a MVC pattern and is responsible for all
user interaction for game logic see the FBullCowGame Class.
*/
#include
#include
#include “FBullCowGame.h”
using int32 = int;
using FText = std::string; //substitutes std::string with FText
void PrintIntro();
void PlayGame();
FText GetValidGuess();
void PrintGameSummary();
bool AskPlayAgain();
FBullCowGame BCGame; //initiate a new game, looks for a constructor and runs any code in the constructor
int main()
{
bool bPlayAgain = false;
do
{
PrintIntro();
PlayGame();
bPlayAgain = AskPlayAgain();
}
while (bPlayAgain);
return 0; //exit the application
}
void PrintIntro()
{
//Starts the game
//constexpr is a constant expression that cannot be changed when used in the console but can be used when coding.
std::cout << “Welome to Bulls and Cows, a fun word game!!” << std::endl;
std::cout << "Can you guess the " << BCGame.GetHiddenWordLength();
std::cout << " letter isogram? " << std::endl;
std::cout << std::endl;
return;
}
void PlayGame()
//loops a constant number of turns wand returns the results of the players guess
{
BCGame.Reset();
int32 MaxTries = BCGame.GetMaxTries();
//loop the game for guess while the game is NOT won and the
//and there are still guesses left
while (!BCGame.IsGameWon() && BCGame.GetCurrentTry() <= MaxTries)
{
FText Guess = GetValidGuess();
for (int32 count = 1; count <= MaxTries; count++)
{
//Submit valid guess to the game and recieve counts
FBullCowCount BullCowCount = BCGame.SubmitValidGuess(Guess);
// Print the number of bulls (successes) and cows (misses)
std::cout << "Bulls = " << BullCowCount.Bulls;
std::cout << " Cows = " << BullCowCount.Cows;
std::cout << std::endl;
std::cout << std::endl;
}
}
PrintGameSummary();
}
//loop continously until the user gets a valid guess
FText GetValidGuess()
{
FText Guess = " ";
GetGuessStatus Status = GetGuessStatus::Ivalid_Status;
do {
int32 CurrentTry = BCGame.GetCurrentTry();
//get a guess from the player
std::cout << "Try " << CurrentTry << “. What is your guess?” << std::endl;
std::getline(std::cin, Guess);
Status = BCGame.CheckGuessValidiaty(Guess);
switch (Status)
{
case GetGuessStatus::Wrong_Length:
std::cout << "Please enter a " << BCGame.GetHiddenWordLength() << " letter word.\n";
break;
case GetGuessStatus::Not_Lowercase:
std::cout << "Must be lower case! Try again " << std::endl;
break;
case GetGuessStatus::Not_Isogramm:
std::cout << "Letters must not repeat! Try again " << std::endl;
break;
default:
//assume the guess is valid after all checks valid
Guess;
}
std::cout << std::endl;
} while (Status != GetGuessStatus::OK); //keep looping until you get valid input or no errors
return Guess;
}
bool AskPlayAgain()
{
//gives player the option to play again
std::cout << "Would you like to play again with the same hidden word (y/n) " << std::endl;
FText Response = “”;
std::getline(std::cin, Response);
return (Response[0] == 'y') || (Response[0] == 'Y');
}
void PrintGameSummary()
{
if (BCGame.IsGameWon())
{
std::cout << “Well Done, You Win!” << std::endl;
}
else
{
std::cout << “Better Luck Next Time!” << std::endl;
}
}