Last active
February 26, 2018 09:05
-
-
Save WolfpackGaming-SavSin/444a63f8537baeae93afb581e40c0fc8 to your computer and use it in GitHub Desktop.
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" | |
#include <map> | |
#define TMap std::map | |
using FString = std::string; | |
using int32 = int; | |
bool IsLowercase(FString str); | |
FBullCowGame::FBullCowGame(){ Reset(); } | |
int32 FBullCowGame::GetCurrentTry() const { return MyCurrentTry; } | |
int32 FBullCowGame::GetHiddenWordLength() const { return MyHiddenWord.length(); } | |
bool FBullCowGame::IsGameWon() const { return bIsGameWon; } | |
int32 FBullCowGame::GetMaxTries() const | |
{ | |
TMap<int32, int32> WordLengthToMaxTries{ {3,5}, { 4,7 }, { 5,7 }, { 6,7 }, { 7,10 }, { 8,12 }, { 9,15 }, { 10,18 } }; | |
return WordLengthToMaxTries[MyHiddenWord.length()]; | |
} | |
EGuessStatus FBullCowGame::CheckGuessValidity(FString Guess) const | |
{ | |
// If the guess is not an isogram | |
if (!IsIsogram(Guess)) | |
{ | |
return EGuessStatus::NOT_ISOGRAM; | |
} | |
else if(!IsLowercase(Guess)) | |
{ | |
return EGuessStatus::NOT_LOWERCASE; | |
} | |
else if (Guess.length() != GetHiddenWordLength()) | |
{ | |
return EGuessStatus::WRONG_LENGTH; | |
} | |
return EGuessStatus::OK; | |
} | |
void FBullCowGame::Reset() | |
{ | |
MyCurrentTry = 1; | |
bIsGameWon = false; | |
return; | |
} | |
//Sets the Hidden word from a predefined list based on word length | |
void FBullCowGame::SetHiddenWord(int32 WordLength) | |
{ | |
TMap<int32, FString> AvailableWords{ { 3,"hew" },{ 4,"fail" },{ 5,"manor" },{ 6,"carbon" },{ 7,"journal" },{ 8,"dayshift" },{ 9,"observing" },{ 10,"hyperlinks" } }; | |
MyHiddenWord = AvailableWords[WordLength]; | |
} | |
// Receives a valid guess, increments turn, and returns count | |
FBullCowCount FBullCowGame::SubmitValidGuess(FString guess) | |
{ | |
MyCurrentTry++; | |
FBullCowCount BullCowCount; | |
// loop through all letters in hidden word | |
int32 WordLength = MyHiddenWord.length(); | |
for(int32 i=0; i<WordLength; i++) | |
{ | |
// Compare letters against the guess | |
for (int32 j = 0; j < WordLength; j++) | |
{ | |
// if they are in the same place | |
if (MyHiddenWord[i] == guess[j]) | |
{ | |
if(i == j) | |
{ | |
//Increment bulls | |
BullCowCount.Bulls++; | |
} | |
else | |
{ | |
//Increment cows if not | |
BullCowCount.Cows++; | |
} | |
} | |
} | |
} | |
if (BullCowCount.Bulls == WordLength) | |
{ | |
bIsGameWon = true; | |
} | |
else | |
{ | |
bIsGameWon = false; | |
} | |
return BullCowCount; | |
} | |
bool FBullCowGame::IsIsogram(FString Word) const | |
{ | |
// Treat 0 and 1 letter words as isograms | |
if (Word.length() <= 1) { return true; } | |
// Set up our map | |
TMap<char, bool> LetterSeen; | |
// Loop through all the letters of the word | |
for(auto Letter : Word) | |
{ | |
Letter = tolower(Letter); // Handle Mixed Case | |
// If the letter is in the map | |
if (LetterSeen[Letter]) | |
{ | |
return false; | |
} | |
else | |
{ | |
LetterSeen[Letter] = true; | |
} | |
} | |
return true; // For example in cases/0 is entered | |
} | |
bool FBullCowGame::IsLowercase(FString Word) const | |
{ | |
for (auto Letter : Word) | |
{ | |
if (!islower(Letter)) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment