Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- #include <string>
- #include "FBullCowGame.h"
- using FText = std::string;
- using int32 = int;
- void PrintIntro();
- void PrintGameSummary();
- void PlayGame();
- FText GetGuess();
- bool IsPlayingAgain();
- FBullCowGame BCGame; //instantiate a new game
- int main()
- {
- bool bPlayingAgain = false;
- do //dowhile loop as main game loop
- {
- PrintIntro();
- PlayGame();
- bPlayingAgain = IsPlayingAgain();
- } while (bPlayingAgain);
- return 0;
- }
- //introduce the game
- void PrintIntro()
- {
- int32 WordLength = BCGame.GetHiddenWordLength();
- std::cout << "Welcome to Bulls and Cows, a word guessing game." << std::endl;
- std::cout << "Can you guess the " << WordLength << " letter isogram within " << BCGame.GetMaxTries() << " tries." << std::endl;
- return;
- }
- void PrintGameSummary()
- {
- if (BCGame.IsGameWon())
- {
- std::cout << "Well done you won! You took " << BCGame.GetCurrentTry() << " tries.\n\n";
- }
- else
- {
- std::cout << "Oh no you lost! You reached the maximum of " << BCGame.GetMaxTries() << " tries.\n\n";
- }
- }
- void PlayGame() //play single time to completion
- {
- BCGame.Reset();
- int32 MaxTries = BCGame.GetMaxTries();
- //loop while current turn is less than max tries and game is NOT won
- while (BCGame.GetCurrentTry() <= BCGame.GetMaxTries() && !BCGame.IsGameWon())
- {
- FText Guess = GetGuess();
- //submit guess and recieve back the cow/bull counts
- FBullCowCount BullCowCount = BCGame.SubmitValidGuess(Guess);
- //print guess back
- std::cout << "Your guess " << Guess << " hit " << BullCowCount.BullCount << " Bulls and " << BullCowCount.CowCount << " Cows." << std::endl;
- std::cout << std::endl;
- }
- PrintGameSummary();
- }
- //get user input loops until valid
- FText GetGuess()
- {
- EGuessStatus GuessStatus = EGuessStatus::Invalid;
- FText Guess = "";
- do
- {
- //guess input
- std::cout << "Enter your guess: ";
- std::getline(std::cin, Guess);
- //guess validation
- GuessStatus = BCGame.CheckGuessValid(Guess);
- switch (GuessStatus)
- {
- case EGuessStatus::Not_Isogram:
- std::cout << "Please enter an isogram (each letter is unique).\n";
- break;
- case EGuessStatus::Wrong_Length:
- std::cout << "Please enter " << BCGame.GetHiddenWordLength() << " letter word. \n";
- break;
- case EGuessStatus::Not_Lowercase:
- std::cout << "Please use lower case.\n";
- break;
- default:
- break;
- }
- std::cout << std::endl;
- } while (GuessStatus != EGuessStatus::OK);
- return Guess;
- }
- bool IsPlayingAgain()
- {
- std::cout << "Would you like to play again with the same word? Y/N" << std::endl;
- FText Response = "";
- std::getline(std::cin, Response);
- return (Response[0] == 'Y' || Response[0] == 'y');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement