Im having a problem that the number of tries is not increasing can any body check and help heres the code

this is main cpp
/*
this is console executable, that makes use of the bullcow class
this acts as the view in MVC Pattren, and is responsible for all user
interaction. for game logic see the FBullCowGame Class
*/

#include
#include
#include"FBullCowGame.h"
FBullAndCow BCGame;

using FText = std::string;
using int32 = int;

// introduce the game
void printintro()
{

std :: cout << "welcome to bulls and cows a fun words game.\n";
std :: cout << "can you guess the " << BCGame.GetHiddenWordLength();
std :: cout << " letter Isogram I'm thinking of ? " << std :: endl ;

return;

}

// taking guess from the user
FText getguess()
{
FBullAndCow BCGame;
int32 CurrentTry = BCGame.GetCurrentTry();
std :: cout <<"Try " << CurrentTry << ". \nenter your guess: ";
FText guess;
std :: getline (std :: cin, guess);
return guess;
}

// play the game
void Playgame()
{
// instantiate a new game
FBullAndCow BCGame;

BCGame.Restplaygame();
int32 MaxTries = BCGame.GetMaxTries();
std ::cout << std :: endl;
//loop for the number of times asking for guesses 
// TODO Change it from for to while loop 
for (int32 x = 0; x < MaxTries; x++)
{ 
	FText  guess; 
	std :: cout << std :: endl;
	guess = getguess(); // TODO Make and check the valid Guess 
	//repeat it the guess back to them 
	// & and submit valid guess to the game and recive counts
	FBullCowCount BullCowCount =  BCGame.SubmitGuess(guess);
	// print number of Bulls and Cows
	std::cout << "Bulls = " << BullCowCount.Bulls;
	std::cout << "   Cows = " << BullCowCount.Cows;
	std::cout << std::endl; 
	std :: cout << "your guess was: " << guess << std :: endl;
	// TODO Add a Game Summary 
}
return;

}

//ask the player if they want to play again
bool asktoplayagain()
{
std :: cout << “Do you want to play again (y/n)? “;
FText answer =””;
std :: getline(std :: cin, answer);
std :: cout << std :: endl;
return ( answer[0] == ‘y’ ) || ( answer[0] == ‘Y’ );
}

int main()
{
printintro();
std :: cout << std :: endl;
do
{
Playgame();
std :: cout << std :: endl;
}
while (asktoplayagain());

return 0;

}

this is Methods header.file

#pragma once
#include

using int32 = int;
using FString = std::string;
// all values intialised to zero
struct FBullCowCount
{
int32 Bulls = 0;
int32 Cows = 0;
};

class FBullAndCow
{

public:
// constructor
FBullAndCow();

void Restplaygame(); // TODO make it more rich Value 

int32 GetMaxTries() const;
int32 GetCurrentTry() const;
bool IsGameWon() const;


int32 GetHiddenWordLength() const;

bool CheckGuessIsValid (FString) const ; // TODO make it more rich Value 
FBullCowCount SubmitGuess(FString);

private:
// see constructor for initialisation
int32 MyCurrentTry;
int32 MyMaxTries;
FString MyHiddenWord;
};

this is methods file

#include “FBullCowGame.h”

using int32 = int;
using FString = std::string;

FBullAndCow::FBullAndCow()
{
Restplaygame();
}

void FBullAndCow::Restplaygame()
{
constexpr int32 NumberOfTries = 8;
MyMaxTries = NumberOfTries;

const FString Hidden_word = "king";
MyHiddenWord = Hidden_word;

MyCurrentTry = 1;

return; 

}

int32 FBullAndCow::GetMaxTries() const
{
return MyMaxTries;
}

int32 FBullAndCow::GetCurrentTry() const
{
return MyCurrentTry;
}

int32 FBullAndCow::GetHiddenWordLength() const
{
return MyHiddenWord.length();
}

bool FBullAndCow::IsGameWon() const
{
return false;
}

bool FBullAndCow::CheckGuessIsValid(FString) const
{
return false;
}
//recives a valid guess increments try and return count
FBullCowCount FBullAndCow::SubmitGuess(FString guess)
{
//incriment the try number
MyCurrentTry++;
// setup return variable
FBullCowCount BullCowCount;
// loop through all letter in the guess
int32 HiddenWorldLength = MyHiddenWord.length();
for (int32 MyHiddenWordChar = 0 ; MyHiddenWordChar < HiddenWorldLength ; MyHiddenWordChar++ )
{

	// compare letters against the hidden word
	for (int32 GuessChar = 0; GuessChar < HiddenWorldLength; GuessChar++)
	{

		//if they match 
		if (guess[GuessChar] == MyHiddenWord[MyHiddenWordChar])
		{
			//if they are in the same place 
			if (MyHiddenWordChar == GuessChar)
			{
				// incriment bulls  
				BullCowCount.Bulls++;
			}
			// if not in the same place 
			else
			{
				//incriment cows 
				BullCowCount.Cows++;
			}
		}
	}
}

return BullCowCount;

}

The problem is here:

FText getguess()
{
	FBullAndCow BCGame;
	int32 CurrentTry = BCGame.GetCurrentTry();
        std::cout << "Try " << CurrentTry << ". \nenter your guess: ";

FBullAndCow initializes a NEW FBullAndCow game each time you call getguess(), which is everytime someone enters a guess.

The FBullAndCow instance is built from this class:

 class FBullAndCow
    {
    ...
    	int32 GetMaxTries() const;
    	int32 GetCurrentTry() const;
    	bool IsGameWon() const;
    ...
    	int32 MyCurrentTry;
    	int32 MyMaxTries;
    	FString MyHiddenWord;
    };

It holds your “MyCurrentTry” which you print each time a guess was submitted. In the Constructor of this class (which is called EVERY time you create a new instance of this class) “MyCurrentTry” gets set to 1:

FBullAndCow::FBullAndCow()
{
	Restplaygame();
}

void FBullAndCow::Restplaygame()
{
	constexpr int32 NumberOfTries = 8;
	MyMaxTries = NumberOfTries;

	const FString Hidden_word = "king";
	MyHiddenWord = Hidden_word;

	MyCurrentTry = 1;

	return;

}

Therefore every time you call getguess(), the newly created Instance “BCgame” of Class “FBullAndCow” has “MyCurrentTry” initialized to 1 and std::cout will print 1.

Delete this line everywhere:

FBullAndCow BCGame;

and put it into the main():

int main()
{
        FBullAndCow BCGame;
	printintro();
	std::cout << std::endl;
	do
	{
		Playgame();
		std::cout << std::endl;
	} while (asktoplayagain());

	return 0;

}

Here’s the simplified explanation, if you want it:

Your usage of the line

FBullAndCow BCGame;

is the problem.

Let me explain what a class and what an instance is:

A Class
is the blueprint, the set of rules, or the mold (however you want to imagine it). The class itself doesn’t store the data/progress and doesn’t do the work in your game. It’s basically the factory that can create an infinite amout of basically the same thing (well, not infinite, you’ll run out of Storage eventually).
Let’s say the class is the factory that produces Monopoly-packages with figures, money and the little houses.
You can NOT “play” Monopoly with the “Factory”, that’s where the instance comes in.
You create a class with code looking like this:

class FBullAndCow
{

public:
	// constructor
	FBullAndCow();

	void Restplaygame(); // TODO make it more rich Value 

	int32 GetMaxTries() const;
	int32 GetCurrentTry() const;
	bool IsGameWon() const;


	int32 GetHiddenWordLength() const;

	bool CheckGuessIsValid(FString) const; // TODO make it more rich Value 
	FBullCowCount SubmitGuess(FString);

private:
	// see constructor for initialisation
	int32 MyCurrentTry;
	int32 MyMaxTries;
	FString MyHiddenWord;
};

An Instance
is the “product” or “output” of the class. In our comparison it is the Monopoly-package itself, with which you can play the game. You can tell the Class/Factory that one of these packages should have green houses, the other red houses, and another one shall have 6 instead of 4 figures and one is Disney-themed, the others are not. Each Instance can be different from one another, by setting the values of its members/variables differently in the constructor.
You can create multiple Instances with this little code:

FBullAndCow BCGame;
FBullAndCow BullGame;
FBullAndCow CowGame;

Now you have 3 Instances of the same class. Each of these instances has a MyCurrentTry-Value of 1 in the beginning (that’s what you wrote in the contructor), but if you call

BCGame.SubmitGuess(...);

The “MyCurrentTry”-value of THIS Instance gets set to 2.
The others stay at 1, since they have no relationship with each other.

It gets Problematic if you create THREE Instances with the SAME NAME:

FBullAndCow BCGame;
....
FBullAndCow BCGame;
....
FBullAndCow BCGame;

The third one overwrites the previous, therefore MyCurrentTry gets lost with every new instance. That’s what happened in your code:

1st occurence of FBullAndCow

 FBullAndCow BCGame;
 
 using FText = std::string;
 using int32 = int;
 
 // introduce the game
 void printintro()
 {....

2nd occurenceof FBullAndCow

// taking guess from the user
FText getguess()
{
	FBullAndCow BCGame;
	int32 CurrentTry = BCGame.GetCurrentTry();
	std::cout << "Try " << CurrentTry << ". \nenter your guess: ";
	FText guess;
	std::getline(std::cin, guess);
	
	return guess;
}

3rd occurence of FBullAndCow

// play the game
void Playgame()
{
	// instantiate a new game
	FBullAndCow BCGame;

	BCGame.Restplaygame();
....

4th occurence of FBullAndCow

FBullCowCount FBullAndCow::SubmitGuess(FString guess)
{
	//incriment the try number
	MyCurrentTry++;
	// setup return variable
	FBullCowCount BullCowCount;
	// loop through all letter in the guess
        ...
1 Like

Thanks Daniel it works, thanks alot I was stuck in it and thanks for the explanation .

@Daniel_Udemy That explanation of class and instances helped a lot. I know Ben said it in the video but sometimes you need to hear again and again for it to sink in.

Privacy & Terms