Skip to content

Instantly share code, notes, and snippets.

@HooperTrooper17
Created March 1, 2018 01:04
Show Gist options
  • Save HooperTrooper17/1fdf06469cf59488ad0757e6f0da86b2 to your computer and use it in GitHub Desktop.
Save HooperTrooper17/1fdf06469cf59488ad0757e6f0da86b2 to your computer and use it in GitHub Desktop.
// This has the actions and commands that are in the 'boxes from the header file
#pragma once
#include "FBullCowGame.h"
#include <iostream>
#include <string>
#include <map>
#define TMap std::map // to make syntax Unreal friendly
// to make the syntax Unreal friendly
using FString = std::string;
using int32 = int;
FBullCowGame::FBullCowGame()
{
Reset();
}
int32 FBullCowGame::GetCurrentTry() const { return MyCurrentTry; }
int32 FBullCowGame::GetHiddenWordLength() const { return MyHiddenWord.length(); }
bool FBullCowGame:: IsGameWon() const { return bGameIsWon; }
int32 FBullCowGame::GetMaxTries() const
{
TMap<int32, int32> WordLengthToMaxTries{ { 3,5 },{ 4,7 },{ 5,8 },{ 6,9 },{ 7,10 } };
return WordLengthToMaxTries[MyHiddenWord.length()];
}
FString FBullCowGame::GetHiddenWord()
{
return MyHiddenWord;
}
void FBullCowGame::Reset()
{
std::cout << "Please put in a word for your opponent to guess: ";
MyHiddenWord = TypeWord(); // this MUST be an isogram
const int32 CURRENT_TRY = 1;
MyCurrentTry = CURRENT_TRY;
bGameIsWon = false;
return;
}
FString FBullCowGame::TypeWord()
{
FString Word = "";
std::getline(std::cin, Word);
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
return Word;
}
EGuessStatus FBullCowGame::CheckGuessValidity(FString Guess) const
{
if (!IsIsogram(Guess)) // if the guess is isn't an isogram
{
return EGuessStatus::Not_Isogram;
}
else 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
{
return EGuessStatus::Wrong_Length;
}
else
{
return EGuessStatus::Ok; //
}
}
bool FBullCowGame::IsIsogram(FString Word) const
{
// treat 0 and 1 letter words as isograms
if (Word.length() <= 1) { return true; }
TMap<char, bool> LetterSeen;
for (auto Letter : Word) // for all the letters of the word
{
Letter = tolower(Letter); // handle mixed cases
if (LetterSeen[Letter]) {// if the letter is in the map
return false; // not isogram
}
else {
LetterSeen[Letter] = true; // is isogram
}
}
return true; // for example in cases here /0 are entered
}
bool FBullCowGame::IsLowercase(FString Word) const
{
for (auto Letter : Word) { // forf all letters in the word
if (!islower(Letter)) // if it is NOT (the !) a lowercase letter the
{
return false;
}
}
return true;
}
//receives a VALID guess, increments turn and focus on the interface above
FBullCowCount FBullCowGame::SubmitValidGuess(FString Guess)
{
MyCurrentTry++; // Increasing tries after each turn
FBullCowCount BullCowCount;
int32 WordLength = MyHiddenWord.length(); // assuming same length as guess
for (int32 MHWChar = 0; MHWChar < WordLength; MHWChar++) { // loop through all letters in the hidden word
//compare letters against the hidden word
for (int32 GChar = 0; GChar < WordLength; GChar++) { //if they match then
if (Guess[GChar] == MyHiddenWord[MHWChar]) { // if they are in the same place
if (MHWChar == GChar) {
BullCowCount.Bulls++; //increment bulls
}
else {
BullCowCount.Cows++; // must be a cow
}
}
}
}
if (BullCowCount.Bulls == WordLength)
{
bGameIsWon = true;
}
else {
bGameIsWon = false;
}
return BullCowCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment