From 193e922cac522b334a109dd07eb97f0df79cae79 Mon Sep 17 00:00:00 2001 From: Bogdan MATA Date: Sat, 30 Dec 2017 19:20:21 +0100 Subject: [PATCH] Some proposals --- BullCowGame/FBullCowGame.cpp | 42 +++++++---------- BullCowGame/main.cpp | 89 ++++++++++++++++++------------------ 2 files changed, 61 insertions(+), 70 deletions(-) diff --git a/BullCowGame/FBullCowGame.cpp b/BullCowGame/FBullCowGame.cpp index a7c3557..19b5c67 100644 --- a/BullCowGame/FBullCowGame.cpp +++ b/BullCowGame/FBullCowGame.cpp @@ -26,28 +26,25 @@ void FBullCowGame::Reset() MyCurrentTry = 1; bGameIsWon = false; - return; } EGuessStatus FBullCowGame::CheckGuessValidity(FString Guess) const { + // we are returning ;) if (!IsIsogram(Guess)) // if the guess isn't an isogram { return EGuessStatus::Not_Isogram; } - else if (!IsLowercase(Guess)) // if the guess isn't all lowercase + if (!IsLowercase(Guess)) // if the guess isn't all lowercase { return EGuessStatus::Not_Lowercase; } - else if (Guess.length() != GetHiddenWordLength()) // if the guess length is wrong + if (Guess.length() != GetHiddenWordLength()) // if the guess length is wrong { return EGuessStatus::Wrong_Length; } - else - { - return EGuessStatus::OK; - } + return EGuessStatus::OK; } @@ -59,26 +56,21 @@ FBullCowCount FBullCowGame::SubmitValidGuess(FString Guess) int32 WordLength = MyHiddenWord.length(); // assuming same length as guess // loop through all letters in the hidden word - for (int32 MHWChar = 0; MHWChar < WordLength; MHWChar++) { + for (int32 MHWChar = 0; MHWChar < WordLength; MHWChar++) + { // compare letters against the guess - for (int32 GChar = 0; GChar < WordLength; GChar++) { + for (int32 GChar = 0; GChar < WordLength; GChar++) + { // if they match then - if (Guess[GChar] == MyHiddenWord[MHWChar]) { - if (MHWChar == GChar) { // if they're in the same place - BullCowCount.Bulls++; // incriment bulls - } - else { - BullCowCount.Cows++; // must be a cow - } + if (Guess[GChar] == MyHiddenWord[MHWChar]) + { + (MHWChar == GChar) ? BullCowCount.Bulls++ : BullCowCount.Cows++; } } } - if (BullCowCount.Bulls == WordLength) { - bGameIsWon = true; - } - else + if (BullCowCount.Bulls == WordLength) { - bGameIsWon = false; + bGameIsWon = true; } return BullCowCount; } @@ -92,11 +84,11 @@ bool FBullCowGame::IsIsogram(FString Word) const for (auto Letter : Word) // for all letters of the word { Letter = tolower(Letter); // handle mixed case - if (LetterSeen[Letter]) {// if the letter is in the map + if (LetterSeen[Letter]) + {// if the letter is in the map return false; // we do NOT have an isogram - } else { - LetterSeen[Letter] = true;// add the letter to the map - } + } + LetterSeen[Letter] = true;// add the letter to the map } return true; // for example in cases where /0 is entered diff --git a/BullCowGame/main.cpp b/BullCowGame/main.cpp index b6f7d97..949ac05 100644 --- a/BullCowGame/main.cpp +++ b/BullCowGame/main.cpp @@ -17,22 +17,21 @@ void PrintIntro(); void PlayGame(); FText GetValidGuess(); bool AskToPlayAgain(); -void PrintGameSummary(); +void PrintGameWin(); +void PrintGameLose(); FBullCowGame BCGame; // instantiate a new game, which we re-use across plays // the entry point for our application int main() { - bool bPlayAgain = false; - do { + do + { PrintIntro(); PlayGame(); - bPlayAgain = AskToPlayAgain(); - } - while (bPlayAgain); + } while (AskToPlayAgain()); - return 0; // exit the application + return 0; // exit the application (equivalent to success) } void PrintIntro() @@ -55,72 +54,72 @@ void PrintIntro() 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) { + do + { FText Guess = GetValidGuess(); - + // submit valid guess to the game, and receive counts FBullCowCount BullCowCount = BCGame.SubmitValidGuess(Guess); + if (BCGame.IsGameWon()) + { + PrintGameWin(); + return; + } std::cout << "Bulls = " << BullCowCount.Bulls; std::cout << ". Cows = " << BullCowCount.Cows << "\n\n"; - } + } while (BCGame.GetCurrentTry() <= BCGame.GetMaxTries()); - PrintGameSummary(); - return; + PrintGameLose(); } // loop continually until the user gives a valid guess FText GetValidGuess() { - FText Guess = ""; - EGuessStatus Status = EGuessStatus::Invalid_Status; - do { + while (true) + { // get a guess from the player - int32 CurrentTry = BCGame.GetCurrentTry(); - std::cout << "Try " << CurrentTry << " of " << BCGame.GetMaxTries(); + std::cout << "Try " << BCGame.GetCurrentTry() << " of " << BCGame.GetMaxTries(); std::cout << ". Enter your guess: "; + FText 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; + switch (BCGame.CheckGuessValidity(Guess)) + { + 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 + return Guess; } - } while (Status != EGuessStatus::OK); // keep looping until we get no errors - return Guess; + } } bool AskToPlayAgain() { std::cout << "Do you want to play again with the same hidden word (y/n)? "; - FText Response = ""; + FText Response; std::getline(std::cin, Response); return (Response[0] == 'y') || (Response[0] == 'Y'); } -void PrintGameSummary() +void PrintGameWin() { - if (BCGame.IsGameWon()) - { - std::cout << "WELL DONE - YOU WIN!\n"; - } - else - { - std::cout << "Better luck next time!\n"; - } + std::cout << "WELL DONE - YOU WIN!\n"; +} + +void PrintGameLose() +{ + std::cout << "Better luck next time!\n"; }