Whenever I try to implement the constructor, copy and pasting the private class information over into the constructor like in the video it ignores the constructor values. Instead going straight into the header file private for the variables.
When I remove the numbers from the private section of the header file, it reads as “Try 0” and ignores the loops in main.cpp entirely. The only way the code runs is if there’s a vlaue in the private section of the header file.
FBullCowGame.h
#pragma once
#includeclass FBullCowGame {
public:
FBullCowGame(); //Constructorvoid Reset(); //TODO make a more rich return value
int GetMaxTries() const;
bool IsGameWon() const;
int DisplayCurrentTry() const;
void CheckAnswer(std::string);private:
int MyCurrentTry;
int MyMaxTries;
};
FBullCowGame.cpp
#include “FBullCowGame.h”
FBullCowGame::FBullCowGame()
{
int MyCurrentTry = 5;
int MyMaxTries = 1;
}void FBullCowGame::Reset(){ return; }
int FBullCowGame::GetMaxTries() const { return MyMaxTries; }
bool FBullCowGame::IsGameWon() const { return false; }
int FBullCowGame::DisplayCurrentTry() const { return MyCurrentTry; }
void FBullCowGame::CheckAnswer(std::string){ }
main.cpp
#include
#include
#include “FBullCowGame.h”void PrintIntro();
std::string GetGuess();
bool AskToPlayAgain();
FBullCowGame BCGame;
void PlayGame();void PlayGame()
{int MaxTries = BCGame.GetMaxTries();
// loop for the number of turns asking for guesses
std::cout << MaxTries << std::endl;
for (int count = 1; count <= MaxTries; count++)
{
std::string Guess = GetGuess();
std::cout << “You guessed: \n” << Guess;
}
}void PrintIntro()
{
// introduce the game
constexpr int WORLD_LENGTH = 5;
std::cout << “Welcome to Bulls and Cows, a cool and good word game.\n” << std::endl;
std::cout << "Can you guess the " << WORLD_LENGTH;
std::cout << " letter isogram I’m thinking of? ";
return;
}
std::string GetGuess()
{
int CurrentTry = BCGame.DisplayCurrentTry();
std::cout << "Try " << CurrentTry << std::endl;
// get a guess from the player
std::string Guess = " ";
std::cout << "\nYour guess: ";
std::getline(std::cin, Guess);
return Guess;
}bool AskToPlayAgain()
{
std::cout << "Do you want to play again? (y/n) ";
std::string Response = “”;
std::getline(std::cin, Response);(Response[0] == ‘y’);
std::cout << std::endl;
return (Response[0] == ‘y’) || (Response[0] == ‘Y’);
}// The intro point of our program
int main()
{
std::cout << BCGame.DisplayCurrentTry();
bool bPlayAgain = false;
do {
PrintIntro();
PlayGame();
bPlayAgain = AskToPlayAgain();
} while (bPlayAgain);
return 0; //exits application
}