-
-
Save VirgelMitchell/df613e9dadf9feb6f7399f2430c88f9b to your computer and use it in GitHub Desktop.
Bulls & Cows Project Udemy -> UE4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "fBullCowGame.h" | |
using FString = std::string; | |
using int32 = int; | |
// returns the number of attemts a player may have to correctly guess the hidden word | |
int32 FBullCowGame::GetMaxTries() const { | |
return MaxTries; } | |
// returns the current attempt ID number | |
int32 FBullCowGame::GetCurrentTry() const { | |
return CurrentTry;} | |
// returns the length of the hidden word is | |
int32 FBullCowGame::GetWordLength() { | |
return HiddenWord.length(); | |
} | |
// check to see if the player's current guess is valid in terms of the game. | |
EValidGuess FBullCowGame::IsValid(FString Guess) const | |
{ | |
if (FBullCowGame::IsIsogram(Guess) != true) | |
{ | |
return EValidGuess::Not_Isogram; | |
} | |
else if (FBullCowGame::IsAllCaps(Guess) != true) | |
{ | |
return EValidGuess::Caps_Mismatch; | |
} | |
else if (Guess.length( ) != GetWordLength()) | |
{ | |
return EValidGuess::Wrong_Length; | |
} | |
else return EValidGuess::OK; | |
} | |
bool FBullCowGame::IsIsogram(FString Guess) const { | |
return true; | |
} | |
bool FBullCowGame::IsAllCaps(FString) const { | |
return true; | |
} | |
// Constructor | |
FBullCowGame::FBullCowGame() { | |
Reset(); | |
std::cout << "Hidden Word contains " << GetWordLength() << " letters."; | |
std::cout << "You have " << MaxTries << " attempts to guess the word."; | |
} | |
// find out how many correct letters the player's guess contains and how many of them are in the correct place | |
FBullCowCount FBullCowGame::GetBovines(FString Guess) { | |
FBullCowCount Bovines; | |
CurrentTry++; | |
for (int32 HWChar = 0; HWChar < HiddenWord.length(); HWChar++) { | |
for (int32 GChar = 0; GChar < HiddenWord.length(); GChar++) { | |
if (Guess[GChar] == HiddenWord[HWChar]) { | |
if (HWChar == GChar) { | |
Bovines.Bulls++; | |
} else { | |
Bovines.Cows++; | |
} | |
} | |
} | |
} | |
return Bovines; | |
} | |
// resets the games variables | |
void FBullCowGame::Reset() | |
{ | |
CurrentTry = 1; | |
InvalidTry = 0; | |
MaxTries = GetWordLength(); | |
HiddenWord = "Woman"; // TODO write a method to pick a random word | |
} | |
void FBullCowGame::IncrementInvalidtTry() | |
{ | |
InvalidTry++; | |
// check to see if the player has entered 10 invalid guesses | |
if (InvalidTry >= 10) { | |
// if so then incriment the current try ID and zero out the invalid try count | |
CurrentTry++; | |
InvalidTry = 0; | |
} | |
// if not then do nothing | |
return; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <iostream> | |
#include <string> | |
using FString = std::string; | |
using int32 = int; | |
struct FBullCowCount | |
{ | |
int32 Bulls = 0; | |
int32 Cows = 0; | |
}; | |
enum class EValidGuess | |
{ | |
Invalid_Status, // initialization state | |
OK, // guess is valid | |
Wrong_Length, // guess is either too short or too long | |
Caps_Mismatch, // guess contains lower-case letters | |
Not_Isogram // guess contains 1 or more repeating characters | |
}; | |
class FBullCowGame | |
{ | |
public: | |
FBullCowGame(); | |
int32 GetMaxTries() const; | |
int32 GetCurrentTry() const; | |
EValidGuess IsValid(FString Guess) const; | |
bool IsIsogram(FString Guess) const; | |
bool IsAllCaps(FString Guess) const; | |
void Reset(); | |
void IncrementInvalidtTry(); | |
int32 GetWordLength(); | |
FBullCowCount GetBovines(FString Guess); // returns the number of bulls and cows in the current guess. | |
private: | |
int32 CurrentTry; | |
int32 InvalidTry; | |
int32 MaxTries; | |
FString HiddenWord; // TODO write a method to generate a random hidden word | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "fBullCowGame.h" | |
using FText = std::string; | |
using int32 = int; | |
FBullCowGame BCGame; | |
// declare the variables needed | |
FText Guess = ""; // a string variable for holding the player's guess | |
bool bWinState = false; // has the game been won? if "yes" then true | |
bool bReplay = true; // does the player want to play again? if "yes" then true (assumes the player wants to play the first time) | |
// list Methods | |
void PrintIntro(); | |
void PlayGame(); | |
FText GetValidGuess(); | |
//TODO write a method to determin whether the game has been won or not | |
bool EndGame(); | |
// primary play loop | |
int main() { | |
do { | |
BCGame.Reset(); | |
PrintIntro(); | |
PlayGame(); | |
// PH input winstate method call here | |
EndGame(); | |
} while (EndGame() == true); | |
return 0; | |
} | |
// introduce the game | |
void PrintIntro (){ | |
std::cout << std::endl; | |
std::cout << std::endl; | |
std::cout << "Welcome to Bulls & Cows, a trite word game.\n"; | |
std::cout << "Where you try and guess the isogram I am thinking of.\n"; | |
return; | |
} | |
// main game loop | |
void PlayGame() { | |
// local variables | |
int32 MaxGuesses = BCGame.GetMaxTries(); | |
int32 WordLength = BCGame.GetWordLength(); | |
// gameplay loop | |
for (int32 Tries = 1; Tries <= MaxGuesses; Tries++) { // TODO change to a do-while loop after writing a method to check validity of each guess | |
std::cout << "I am thinking of a " << WordLength; | |
std::cout << " letter word.\n\nWhat do you think my word is? (attempt "; | |
std::cout << BCGame.GetCurrentTry() << "): "; | |
GetValidGuess(); | |
std::cout << std::endl; | |
FBullCowCount Bovines = BCGame.GetBovines(Guess); | |
std::cout << "You have earned " << Bovines.Bulls; | |
std::cout << " bulls and " << Bovines.Cows << " cows."; | |
std::cout << std::endl; | |
} | |
return; | |
} | |
// find out if the player wishes to play again or end the game session | |
bool EndGame() { | |
// TODO move "Declare WinState block to WinState() when written | |
// Declare WinState | |
std::cout << "You are a "; | |
if(bWinState = true) { | |
std::cout << "Winner!\n\n"; | |
} | |
else { std::cout << "Looser!\n\n"; } | |
// Ask player if they want to replay the game | |
std::cout << "Would you like to play again? (y/n)"; | |
FText Response = ""; | |
std::getline(std::cin, Response); | |
if (Response[0] == 'y' || Response[0] == 'Y') { | |
return true; | |
} | |
else { return false; | |
} | |
} | |
// loop to get a valid guess | |
FText GetValidGuess() { | |
EValidGuess Status = EValidGuess::Invalid_Status; | |
do { | |
std::getline(std::cin, Guess); | |
BCGame.IsValid(Guess); | |
switch (Status) { | |
case EValidGuess::Caps_Mismatch: | |
std::cout << "Please capitolize all leters of your word."; | |
BCGame.IncrementInvalidtTry(); | |
break; | |
case EValidGuess::Not_Isogram: | |
std::cout << "Your guess is not an Isogram. NOTE: An Isogram is a word that does not contain any repeat letters."; | |
BCGame.IncrementInvalidtTry(); | |
break; | |
case EValidGuess::Wrong_Length: | |
std::cout << "I said a " << BCGame.GetWordLength() << " letter word you imbacile!"; | |
BCGame.IncrementInvalidtTry(); | |
break; | |
default: | |
return Guess; | |
} | |
std::cout << std::endl << std::endl; | |
} while (Status != EValidGuess::OK); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment