My IsGameWon method using enum class and switch

Before i even started watching the video, i had to see if i could figure it out, so i did.

here is my enum class in FBullCowGame.h

enum class EGameWinStatus{Not_Enough_Bulls, Enough_Bulls, DEFAULT};

i changed the type of IsGameWon() here

EGameWinStatus IsGameWon(FBullCowCount)const;

Its definition

EGameWinStatus FBullCowGame::IsGameWon(FBullCowCount Bull_Count) const
{
if (Bull_Count.Bulls == GetHiddenWordLength()) { return EGameWinStatus::Enough_Bulls;}
else { return EGameWinStatus::Not_Enough_Bulls;}
}

and in main.cpp here is my PlayGame() function

void PlayGame()
{
int32 MAX_TURNS = BCGAME.GetMaxTries();
EGameWinStatus WinLoseConditions = EGameWinStatus::DEFAULT;
do
{	
	FText Guess = GetValidGuess();
	FBullCowCount BULLS_COWS = BCGAME.SubmitValidGuess(Guess);
	std::cout << "\nYou Got " << BULLS_COWS.Bulls << " Bulls and ";
	std::cout << BULLS_COWS.Cows << " Cows\n\n";
	WinLoseConditions = BCGAME.IsGameWon(BULLS_COWS);
	switch (WinLoseConditions)
	{
	case EGameWinStatus::Not_Enough_Bulls:
		std::cout << "try again\n";
		break;
	case EGameWinStatus::DEFAULT:
		std::cout << "Something went wrong updating bull count\n";
		break;
	default:
		std::cout << "YOU WON!!!!\n";
		break;
	}
} while ((WinLoseConditions != EGameWinStatus::Enough_Bulls) && (BCGAME.GetCurrentTry() <= MAX_TURNS));
}

This solution is very different from Ben’s, but I am very satisfied with my code,(and skills at this point)and the compiler see no warnings.

Thoughts, opinions?

Follow up - Randomly selected hidden words with rand() and namespace{}

heres what i did.

in my FBullCowClass.cpp

namespace Guesses
{
FString Guess1 = "kanto";
FString Guess2 = "pikachu";
FString Guess3 = "rockman";
FString Guess4 = "buster";
}

heres my random number generator

int32 FBullCowGame::WordRandomizer()
{
int32 WordToChoose = rand() % 4 + 1;
return WordToChoose;
}

And heres the function which uses it

FString FBullCowGame::WordSelector(int32 WhichWord)
{
if (WhichWord == 1) { return Guesses::Guess1; }
else if (WhichWord == 2) { return Guesses::Guess2; }
else if (WhichWord == 3) { return Guesses::Guess3; }
else { return Guesses::Guess4; }
}

in Reset()

void FBullCowGame::Reset()
{
MyCurrentTry = 1;
MyMaxTries = MAX_TRIES;

FString HIDDEN_WORD = WordSelector(WordRandomizer());
MyHiddenWord = HIDDEN_WORD;
}

Im sure later on there is a WAY better way to do this, but i just wanted t test myself again.

What SHOULD namespace be used for? and is there a better way to use rand?

Hi

  • One important thing to remember when you use rand() :
    rand() uses a seed to calculate a number that seems to be random. Although the thing is it is not random, it just seems to be. Before you use rand(), you should set your seed before using rand(), so that it would really be random.
    Setting your seed is not necessary, but every time you run your program it will give the exact same numbers.
    To see what im talking about you should compile this code, and run more than one time.
    To set your seed, you need to include time.h in which you have access to a function called time(). In practise people use srand(time(NULL)); in their code before rand() if they really want it to be random.

You might have already known this, but in case you’ve not, I hope you can benefit from what I wrote.

  • The purpose of namespaces are basically to allow you to use the same name for a variable or function twice or more. For example you have a namespace first{ void foo(){std::cout << “first”;} } AND a namespace second{ void foo(){std::cout << “second”;} } you’ll end up with two functions with the same name. Later you can access via :: like second::foo() will write you second.

I hope I could help :slight_smile:
Patrik

1 Like

That’s good to know about rand(). Its always values 6334 18467 41 in that sample program.
I kinda wish i knew more about the STL and srand() at the time.

So heres what i did

int main() 
{
srand(time(NULL));
rng(); 
rng(); 
rng(); 
rng(); 
rng(); 
rng(); 

return 0;
}

void rng()
{

int RandomNumber = rand() % 4 + 1;
std::cout << "number: " << RandomNumber << "\n" << std::endl;
}

Its definately more random now.

And i did know a little bit about the namespaces, i thought i would be useful to make a list FStrings without making them global.

Thanks for the useful information :smiley:

my new WordRandomizer()

int32 FBullCowGame::WordRandomizer()const
{
srand((unsigned int) time(NULL));
int32 WordToChoose = (int32) rand() % 4 + 1;
return WordToChoose;
}

The seed makes a difference. if there were more guesses it would be better.

Privacy & Terms