Skip to content

Instantly share code, notes, and snippets.

Created November 21, 2016 11:13
Show Gist options
  • Save anonymous/2e733b210353591759fde0323bee325d to your computer and use it in GitHub Desktop.
Save anonymous/2e733b210353591759fde0323bee325d to your computer and use it in GitHub Desktop.
my code with GetHiddenWordLength() / HiddenWord.length() problems
//my main.cpp file
#include "FBullCowGame.h"
using int32 = int;
FBullCowGame::FBullCowGame()
{
Reset();
}
int32 FBullCowGame::GetMaxTries() const { return MyMaxTries; }
int32 FBullCowGame::GetCurrentTry() const { return MyCurrentTry; }
int32 FBullCowGame::GetHiddenWordLength() { return HiddenWord.length(); }
void FBullCowGame::Reset()
{
constexpr int32 Max_Tries = 8;
MyMaxTries = Max_Tries;
FString HIDDEN_WORD = "planet";
HiddenWord = HIDDEN_WORD;
MyCurrentTry = 1;
return;
}
FString FBullCowGame::GetHiddenWord()
{
// TODO create a library of guessable isograms
// TODO make it so the program picks one of the words at random
// TODO make it return that word
return HiddenWord;
}
void FBullCowGame::IsGameWon()
{
return;
}
void FBullCowGame::PrintWinner()
{
return;
}
EGuessStatus FBullCowGame::CheckGuessValidity(FString Guess) const
{
if (false)//if guess isnt isogram
{
return EGuessStatus::Not_Isogram;
}
else if (false) //if guess contains caps
{
return EGuessStatus::Caps_Found;
}
else if (false) //if guess contains non-alphabetical characters
{
return EGuessStatus::Not_Letters_Found;
}
else if (Guess.length() != GetHiddenWordLength())//if guess is the wrong length
{
return EGuessStatus::Wrong_Length;
}
else //otherwise
{
return EGuessStatus::OK;
}
return EGuessStatus::OK; //TODO make actual error
}
// recieves a VALID guess, increments turn, returns No. Bulls&Cows
FBullCowCount FBullCowGame::SubmitGuess(FString Guess)
{
// increment the turn number
MyCurrentTry++;
// setup a return variable
FBullCowCount BullCowCount;
// loop through all letters in the guess
int32 HiddenWordLength = HiddenWord.length();
for (int32 HiddenWordChar = 0; HiddenWordChar < HiddenWordLength; HiddenWordChar++) {
// for each letter, compare against HiddenWord
for (int32 GuessChar = 0; GuessChar < HiddenWordLength; GuessChar++) {
if (Guess[GuessChar] == HiddenWord[HiddenWordChar]) { // if they match
if (HiddenWordChar == GuessChar) { // and in the same place
BullCowCount.Bulls++; // increment Bulls
}
else {
BullCowCount.Cows++; // must be cow so increment Cows
}
}
}
}
return BullCowCount;
}
//my FBullCowGame class header file
#include "FBullCowGame.h"
using int32 = int;
FBullCowGame::FBullCowGame()
{
Reset();
}
int32 FBullCowGame::GetMaxTries() const { return MyMaxTries; }
int32 FBullCowGame::GetCurrentTry() const { return MyCurrentTry; }
int32 FBullCowGame::GetHiddenWordLength() { return HiddenWord.length(); }
void FBullCowGame::Reset()
{
constexpr int32 Max_Tries = 8;
MyMaxTries = Max_Tries;
FString HIDDEN_WORD = "planet";
HiddenWord = HIDDEN_WORD;
MyCurrentTry = 1;
return;
}
FString FBullCowGame::GetHiddenWord()
{
// TODO create a library of guessable isograms
// TODO make it so the program picks one of the words at random
// TODO make it return that word
return HiddenWord;
}
void FBullCowGame::IsGameWon()
{
return;
}
void FBullCowGame::PrintWinner()
{
return;
}
EGuessStatus FBullCowGame::CheckGuessValidity(FString Guess) const
{
if (false)//if guess isnt isogram
{
return EGuessStatus::Not_Isogram;
}
else if (false) //if guess contains caps
{
return EGuessStatus::Caps_Found;
}
else if (false) //if guess contains non-alphabetical characters
{
return EGuessStatus::Not_Letters_Found;
}
else if (Guess.length() != GetHiddenWordLength())//if guess is the wrong length
{
return EGuessStatus::Wrong_Length;
}
else //otherwise
{
return EGuessStatus::OK;
}
return EGuessStatus::OK; //TODO make actual error
}
// recieves a VALID guess, increments turn, returns No. Bulls&Cows
FBullCowCount FBullCowGame::SubmitGuess(FString Guess)
{
// increment the turn number
MyCurrentTry++;
// setup a return variable
FBullCowCount BullCowCount;
// loop through all letters in the guess
int32 HiddenWordLength = HiddenWord.length();
for (int32 HiddenWordChar = 0; HiddenWordChar < HiddenWordLength; HiddenWordChar++) {
// for each letter, compare against HiddenWord
for (int32 GuessChar = 0; GuessChar < HiddenWordLength; GuessChar++) {
if (Guess[GuessChar] == HiddenWord[HiddenWordChar]) { // if they match
if (HiddenWordChar == GuessChar) { // and in the same place
BullCowCount.Bulls++; // increment Bulls
}
else {
BullCowCount.Cows++; // must be cow so increment Cows
}
}
}
}
return BullCowCount;
}
//my FBullsCowGame .cpp file
#include "FBullCowGame.h"
using int32 = int;
FBullCowGame::FBullCowGame()
{
Reset();
}
int32 FBullCowGame::GetMaxTries() const { return MyMaxTries; }
int32 FBullCowGame::GetCurrentTry() const { return MyCurrentTry; }
int32 FBullCowGame::GetHiddenWordLength() { return HiddenWord.length(); }
void FBullCowGame::Reset()
{
constexpr int32 Max_Tries = 8;
MyMaxTries = Max_Tries;
FString HIDDEN_WORD = "planet";
HiddenWord = HIDDEN_WORD;
MyCurrentTry = 1;
return;
}
FString FBullCowGame::GetHiddenWord()
{
// TODO create a library of guessable isograms
// TODO make it so the program picks one of the words at random
// TODO make it return that word
return HiddenWord;
}
void FBullCowGame::IsGameWon()
{
return;
}
void FBullCowGame::PrintWinner()
{
return;
}
EGuessStatus FBullCowGame::CheckGuessValidity(FString Guess) const
{
if (false)//if guess isnt isogram
{
return EGuessStatus::Not_Isogram;
}
else if (false) //if guess contains caps
{
return EGuessStatus::Caps_Found;
}
else if (false) //if guess contains non-alphabetical characters
{
return EGuessStatus::Not_Letters_Found;
}
else if (Guess.length() != GetHiddenWordLength())//if guess is the wrong length
{
return EGuessStatus::Wrong_Length;
}
else //otherwise
{
return EGuessStatus::OK;
}
return EGuessStatus::OK; //TODO make actual error
}
// recieves a VALID guess, increments turn, returns No. Bulls&Cows
FBullCowCount FBullCowGame::SubmitGuess(FString Guess)
{
// increment the turn number
MyCurrentTry++;
// setup a return variable
FBullCowCount BullCowCount;
// loop through all letters in the guess
int32 HiddenWordLength = HiddenWord.length();
for (int32 HiddenWordChar = 0; HiddenWordChar < HiddenWordLength; HiddenWordChar++) {
// for each letter, compare against HiddenWord
for (int32 GuessChar = 0; GuessChar < HiddenWordLength; GuessChar++) {
if (Guess[GuessChar] == HiddenWord[HiddenWordChar]) { // if they match
if (HiddenWordChar == GuessChar) { // and in the same place
BullCowCount.Bulls++; // increment Bulls
}
else {
BullCowCount.Cows++; // must be cow so increment Cows
}
}
}
}
return BullCowCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment