Advertisement
Guest User

exmple

a guest
Jan 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include <string>
  4. #include "FBullCowGame.h"
  5.  
  6. using FText = std::string;
  7. using int32 = int;
  8.  
  9. void PrintIntro();
  10. void PrintGameSummary();
  11. void PlayGame();
  12. FText GetGuess();
  13. bool IsPlayingAgain();
  14.  
  15. FBullCowGame BCGame; //instantiate a new game
  16.  
  17. int main()
  18. {
  19.     bool bPlayingAgain = false;
  20.     do //dowhile loop as main game loop
  21.     {
  22.  
  23.     PrintIntro();
  24.     PlayGame();
  25.     bPlayingAgain = IsPlayingAgain();
  26.  
  27.     } while (bPlayingAgain);
  28.  
  29.     return 0;
  30. }
  31.  
  32. //introduce the game
  33. void PrintIntro()
  34. {
  35.     int32 WordLength = BCGame.GetHiddenWordLength();
  36.  
  37.     std::cout << "Welcome to Bulls and Cows, a word guessing game." << std::endl;
  38.     std::cout << "Can you guess the " << WordLength << " letter isogram within " << BCGame.GetMaxTries() << " tries." << std::endl;
  39.  
  40.     return;
  41. }
  42.  
  43. void PrintGameSummary()
  44. {
  45.     if (BCGame.IsGameWon())
  46.     {
  47.         std::cout << "Well done you won! You took " << BCGame.GetCurrentTry() << " tries.\n\n";
  48.     }
  49.     else
  50.     {
  51.     std::cout << "Oh no you lost! You reached the maximum of " << BCGame.GetMaxTries() << " tries.\n\n";
  52.     }
  53. }
  54.  
  55. void PlayGame() //play single time to completion
  56. {
  57.     BCGame.Reset();
  58.  
  59.     int32 MaxTries = BCGame.GetMaxTries();
  60.  
  61.     //loop while current turn is less than max tries and game is NOT won
  62.     while (BCGame.GetCurrentTry() <= BCGame.GetMaxTries() && !BCGame.IsGameWon())
  63.     {
  64.         FText Guess = GetGuess();
  65.  
  66.         //submit guess and recieve back the cow/bull counts
  67.         FBullCowCount BullCowCount =  BCGame.SubmitValidGuess(Guess);
  68.         //print guess back
  69.        
  70.         std::cout << "Your guess " << Guess << " hit " << BullCowCount.BullCount << " Bulls and " << BullCowCount.CowCount << " Cows." << std::endl;
  71.         std::cout << std::endl;
  72.     }
  73.  
  74.     PrintGameSummary();
  75. }
  76.  
  77. //get user input loops until valid
  78. FText GetGuess()
  79. {
  80.     EGuessStatus GuessStatus = EGuessStatus::Invalid;
  81.     FText Guess = "";
  82.     do
  83.     {
  84.         //guess input
  85.  
  86.         std::cout << "Enter your guess: ";
  87.         std::getline(std::cin, Guess);
  88.  
  89.  
  90.         //guess validation
  91.         GuessStatus = BCGame.CheckGuessValid(Guess);
  92.  
  93.         switch (GuessStatus)
  94.         {
  95.         case EGuessStatus::Not_Isogram:
  96.             std::cout << "Please enter an isogram (each letter is unique).\n";
  97.             break;
  98.         case EGuessStatus::Wrong_Length:
  99.             std::cout << "Please enter " << BCGame.GetHiddenWordLength() << " letter word. \n";
  100.             break;
  101.         case EGuessStatus::Not_Lowercase:
  102.             std::cout << "Please use lower case.\n";
  103.             break;
  104.         default:
  105.  
  106.             break;
  107.         }
  108.         std::cout << std::endl;
  109.     } while (GuessStatus != EGuessStatus::OK);
  110.  
  111.     return Guess;
  112. }
  113.  
  114. bool IsPlayingAgain()
  115. {
  116.     std::cout << "Would you like to play again with the same word? Y/N" << std::endl;
  117.  
  118.     FText Response = "";
  119.     std::getline(std::cin, Response);
  120.  
  121.     return (Response[0] == 'Y' || Response[0] == 'y');
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement