Game::Game (int& wordLength, const char* words, Provider& prov)
{
wordSoFar = string(wordLength, FILL_CHARACTER);
numMissedGuesses = 0;
Guesser guesser (wordLength, words); // The problem: goes out of scope.
}
// Indicates whether the game is finished and, if so,
// if the guesser has won
bool Game::guesserHasWon()
{
return wordSoFar.find(FILL_CHARACTER) == string::npos;
}
// Indicates whether the game is finished and, if so,
// if the guesser has lost
bool Game::guesserHasLost()
{
return numMissedGuesses >= MAX_MISTAKE_LIMIT;
}
// Update game state to reflect a new guess
void Game::guessHasBeenMade (char guess)
{
bool isInWord;
provider.getResponseToGuess (guess, isInWord, wordSoFar, numMissedGuesses);
// A non-alphabetic character
static const char FILL_CHARACTER = '.';
// Maximum number of mistaken guesses permitted before the guesser has
// lost the game.
static const int MAX_MISTAKE_LIMIT = 10;
Game (int& wordLength, const char* words, Provider& prov);
// Indicates whether the game is finished and, if so,
// if the guesser has won
bool guesserHasWon();
// Indicates whether the game is finished and, if so,
// if the guesser has lost
bool guesserHasLost();
// Update game state to reflect a new guess
void guessHasBeenMade (char guess);
std::string getWordSoFar();
int getNumMissedGuesses();
Guesser getGuesser();
Provider getProvider();
private:
// What is known of the word being guessed. This string is of the
// correct length, with all known characters filled in and the unknown
// positions containing FILL_CHARACTER.
std::string wordSoFar;
// How many characters have been guessed that are not in the word?
int numMissedGuesses;
class Guesser {
public:
// Initialize the guesser for a game with the indicated wordlength,
// using words from an indicated file.
Guesser (int wordLength, const char* wordListFilename);
/**
* Scan the words that are possible solutions so far, counting, for
* each letter not already tried, the number of words with that letter.
* Guess the letter that occurs in the most words.
*/
char guessACharacter();
/**
* Following a successful guess of a letter in the word, make a pass through
* the possibleSolutions, removing all words that do not contain the
* guess character in the positions indicated in wordSoFar.
*/
void characterIsInWord (char guess, const std::string& wordSoFar);
/**
* Following a mistaken guess of a letter in the word, make a pass through
* the possibleSolutions, removing all words that contain the
* guess character.
*/
void characterIsNotInWord (char guess);
/**
* Guesser has lost the game. Look at the provider's actual word
* and gripe a bit about losing.
*/
void admitToLoss (std::string actualWord, const std::string& wordSoFar);
private:
// A collection of words that match all guesses made so far
std::vector<std::string> possibleSolutions;
// Tracks characters already guessed.
// charactersTried[c-'a'] is true if the character c
// has been guessed previously
bool charactersTried[26];
Well I can't put "Guesser guesser (wordLength, words);" as a member variable because of the parameters.
I tried putting "Guesser guesser;" in Game.h and then "guesser (wordLength, words);" in Game::Game, but then I get the error no matching call to function Guesser::Guesser() at this line -> Game::Game (int& wordLength, const char* words, Provider& prov)
and the error "no matching fnction for call to Guesser (int&, const char*&)" at the line -> guesser (wordLength, words);
I'm sorry if I'm missing something really obvious. I just started using classes.
Checked your code. There seems to be a few output errors. I've tried fixing them but I have not had any success. Just wanted to point out that there were flaws. Sorry I couldn't help.