So, I’m in the middle of lecture 28, I just put “MyCurrentTry” and “MyMaxTries” into the constructor, and I’ve taken out the values from the header file. My code was working as usual before I did this, but I compile afterwards, and instead of taking me through the loop to get my guesses, it says the welcome statement, then immediately says: “Want to Play Again?”
So here’s my code:
main.cpp:
#include <iostream>
#include <string>
#include "FBullCowGame.h"
FBullCowGame BCGame; // instantiate a new game
void PrintIntro();
void PlayGame();
std::string GetGuess();
bool PlayAgain();
int main() {
do {
PrintIntro();
PlayGame();
} while (PlayAgain());
std::cout << std::endl;
return 0;
}
//introduce the game
void PrintIntro() {
constexpr int WORD_LENGTH = 5;
std::cout << "Welcome to Bulls and Cows, a fun word game.\n";
std::cout << "Can you guess the ";
std::cout << WORD_LENGTH;
std::cout << " letter isogram I'm thinking of?\n\n";
return;
}
//get a guess from the player
std::string GetGuess() {
std::cout << "Try " << BCGame.GetCurrentTry() << ". Enter your guess here: ";
std::string Guess = "";
std::getline(std::cin, Guess);
return Guess;
}
//Play the game
void PlayGame() {
int MaxTries = BCGame.GetMaxTries();
//loop for number of turns/guesses
for (int count = 1; count <= MaxTries; count++) {
std::cout << "Your guess was: " << GetGuess() << std::endl << std::endl;
}
return;
}
bool PlayAgain() {
std::cout << "Do you want to play again? ";
std::string Response = "";
std::getline(std::cin, Response);
return (Response[0] == 'y') || (Response[0] == 'Y');
std::cout << std::endl;
}
FBullCowGame.cpp:
#include "FBullCowGame.h"
FBullCowGame::FBullCowGame()
{
int MyCurrentTry = 1;
int MyMaxTries = 5;
}
void FBullCowGame::Reset() {
return;
}
int FBullCowGame::GetMaxTries() const { return MyMaxTries; }
int FBullCowGame::CheckGuessValidity(std::string) const {
return 0;
}
int FBullCowGame::GetCurrentTry() const { return MyCurrentTry; }
bool FBullCowGame::IsWon() const {
return false;
}
FBullCowGame.h:
#pragma once
#include <string>
class FBullCowGame {
public:
FBullCowGame(); //constructor
void Reset(); // TODO make a more rich return value
int GetMaxTries() const;
int CheckGuessValidity(std::string) const; // TODO make a more rich return value
int GetCurrentTry() const;
bool IsWon() const;
private:
//initialized in constructor
int MyCurrentTry;
int MyMaxTries;
};
Thanks,
Aidan
Edit: Nevermind, I figured it out. I accidentally kept the “int” when I moved the values from header to Constructor, and something broke it when trying to re-declare those variables.