Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- #include <string>
- #include "FBullCowGame.h"
- // to make syntax Unreal friendly
- using FText = std::string;
- using int32 = int;
- // function prototypes as outside a class
- void PrintIntro();
- void PlayGame();
- FText GetValidGuess();
- bool AskToPlayAgain();
- void PrintGameSummary();
- int32 WordSelector();
- FBullCowGame BCGame; // instantiate a new game, which we re-use across plays
- int32 Answer;
- // the entry point for our application
- int main()
- {
- do
- {
- PrintIntro();
- PlayGame();
- }
- while (AskToPlayAgain() == true);
- return 0; // exit the application
- }
- void PrintIntro()
- {
- std::cout << "Welcome to Bulls and Cows, a fun word game.\n";
- std::cout << std::endl;
- std::cout << " } { ___ " << std::endl;
- std::cout << " (o o) (o o) " << std::endl;
- std::cout << " /-------\\ / \\ /-------\\ " << std::endl;
- std::cout << " / | BULL |O O| COW | \\ " << std::endl;
- std::cout << " * |-,--- | |------| * " << std::endl;
- std::cout << " ^ ^ ^ ^ " << std::endl;
- Answer = WordSelector();
- FWordSelection WordSelected = BCGame.WordSelection(Answer);
- std::cout << "Can you guess the " << BCGame.GetHiddenWordLength();
- std::cout << " letter isogram I'm thinking of?\n";
- std::cout << std::endl;
- return;
- }
- // plays a single game to completion
- void PlayGame()
- {
- BCGame.Reset();
- int32 MaxTries = BCGame.GetMaxTries();
- // loop asking for guesses while the game
- // is NOT won and there are still tries remaining
- while (!BCGame.IsGameWon() && BCGame.GetCurrentTry() < MaxTries) {
- FText Guess = GetValidGuess();
- // submit valid guess to the game, and receive counts
- FBullCowCount BullCowCount = BCGame.SubmitValidGuess(Guess);
- std::cout << "Bulls = " << BullCowCount.Bulls;
- std::cout << ". Cows = " << BullCowCount.Cows << "\n\n";
- }
- PrintGameSummary();
- return;
- }
- // loop continually until the user gives a valid guess
- FText GetValidGuess()
- {
- FText Guess = "";
- EGuessStatus Status = EGuessStatus::Invalid_Status;
- do {
- // get a guess from the player
- int32 CurrentTry = BCGame.GetCurrentTry();
- std::cout << "Try " << CurrentTry << " of " << BCGame.GetMaxTries();
- std::cout << ". Enter your guess: ";
- std::getline(std::cin, Guess);
- // check status and give feedback
- Status = BCGame.CheckGuessValidity(Guess);
- switch (Status) {
- case EGuessStatus::Wrong_Length:
- std::cout << "Please enter a " << BCGame.GetHiddenWordLength() << " letter word.\n\n";
- break;
- case EGuessStatus::Not_Isogram:
- std::cout << "Please enter a word witout repeating letters.\n\n";
- break;
- case EGuessStatus::Not_Lowercase:
- std::cout << "Please enter all lowercase letters.\n\n";
- break;
- default:
- // assume the guess is valid
- break;
- }
- } while (Status != EGuessStatus::OK);
- return Guess;
- }
- bool AskToPlayAgain()
- {
- std::cout << "Do you want to play again with the same hidden word (y/n)? ";
- 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!\n";
- }
- else
- {
- std::cout << "Better luck next time!\n";
- }
- }
- int32 WordSelector()
- {
- std::cout << "How many letter word do you want?(3/4/5)" << std::endl;
- Answer;
- std::cin >> Answer;
- return Answer;
Add Comment
Please, Sign In to add comment