Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 17 additions & 25 deletions BullCowGame/FBullCowGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand All @@ -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;
}
Expand All @@ -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
Expand Down
89 changes: 44 additions & 45 deletions BullCowGame/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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";
}