我正在编写由程序和其他一些文件实现的源文件和头文件,我需要通过编辑一个源文件/头文件来使代码工作(game.h和game.cpp) )。我试图从另一个源/头文件调用另一个函数,但它无法正常工作。这是我的代码
game.cpp
#include "game.h"
#include "guesser.h"
#include "provider.h"
using namespace std;
const char FILL_CHARACTER = '.';
string wordSoFar;
int numMissedGuesses;
const int MAX_MISTAKE_LIMIT = 10;
void Game::setUpGame (int wordLength)
{
wordSoFar = string(wordLength, FILL_CHARACTER);
numMissedGuesses = 0;
}
bool Game::guesserHasWon()
{
return wordSoFar.find(FILL_CHARACTER) == string::npos;
}
bool Game::guesserHasLost()
{
return numMissedGuesses >= MAX_MISTAKE_LIMIT;
}
void Game::guessHasBeenMade (char guess)
{
bool isInWord;
Provider::getResponseToGuess(guess, isInWord, wordSoFar);
if (isInWord)
{
characterIsInWord (guess, wordSoFar);
}
else
{
++numMissedGuesses;
characterIsNotInWord (guess);
}
}
game.h
#ifndef GAME_H
#define GAME_H
#include <string>
class Provider;
class Guesser;
class Game{
const char FILL_CHARACTER;
std::string wordSoFar;
int numMissedGuesses;
const int MAX_MISTAKE_LIMIT;
void setUpGame (int wordLength);
bool guesserHasWon();
bool guesserHasLost();
void guessHasBeenMade (char guess);
};
#endif
provider.h
#ifndef PROVIDER_H
#define PROVIDER_H
#include <string>
class Provider {
public:
int initialPrompt ();
void getResponseToGuess (char guess, bool& isInWord,
std::string& wordSoFar,
int numMissedGuesses);
std::string providerHasWon ();
void providerHasLost (std::string wordSoFar);
private:
};
#endif
provider.cpp
#include "provider.h"
#include "game.h"
#include <iostream>
using namespace std;
int Provider::initialPrompt ()
{
cout << "Let's play Hangman!\n\n"
<< "Please think of a word from 4-9 characters long. The word\n"
<< "should not be a proper name (something that you would normally\n"
<< "capitalize, nor should it contain any punctuation characters.\n"
<< "\n\nOK, got a word?\n" << endl;
int len = 1;
while (len < 4 || len > 9)
{
cout << "How long is your word? " << flush;
cin >> len;
if (len < 4 || len > 9)
{
cout << "Please choose a word between 4 and 9 characters long."
<< endl;
}
}
return len;
}
bool getYesNoResponse()
{
string response;
getline (cin, response);
while (response.size() == 0 ||
(response[0] != 'y' && response[0] != 'Y'
&& response[0] != 'n' && response[0] != 'N'))
{
if (response.size() > 0)
cout << "Please respond 'yes' or 'no'. " << flush;
getline (cin, response);
}
return response[0] == 'y' || response[0] == 'Y';
}
void Provider::getResponseToGuess (char guess, bool& isInWord,
std::string& wordSoFar,
int numMissedGuesses)
{
cout << "I have missed " << numMissedGuesses << " guesses ("
<< Game::MAX_MISTAKE_LIMIT - numMissedGuesses << " misses left)"
<< endl;
cout << "\n" << wordSoFar << "\n";
for (int i = 1; i <= wordSoFar.size(); ++i)
cout << i;
cout << "\n\nDoes your word contain the letter '"
<< guess << "'? (y/n) " << flush;
isInWord = getYesNoResponse();
if (isInWord) {
string response;
bool done = false;
string newWord;
while (!done)
{
cout << "Enter all of the character positions (1-"
<< wordSoFar.size() << ") in which the letter '"
<< guess << "' appears: " << flush;
getline (cin, response);
bool digitsFound = false;
newWord = wordSoFar;
for (int i = 0; i < response.size(); ++i)
{
char d = response[i];
if (d >= '1' && d <= '0' + wordSoFar.size())
{
int k = d - '1';
if (wordSoFar[k] == Game::FILL_CHARACTER)
{
newWord[k] = guess;
digitsFound = true;
}
}
}
if (digitsFound)
{
cout << "Like this: " << newWord << "? (y/n) " << flush;
bool yn = getYesNoResponse();
if (yn)
{
wordSoFar = newWord;
done = true;
}
}
}
}
}
/**
* Announce that the provider has won the game, and get the
* provider's actual word.
*/
std::string Provider::providerHasWon ()
{
cout << "Congratulations, you have won." << endl;
cout << "\nOut of curiosity, what was your word? " << flush;
string answer;
getline (cin, answer);
return answer;
}
/**
* Announce that the guesser has won the game, and get the
* provider's actual word.
*/
void Provider::providerHasLost (string wordSoFar)
{
cout << wordSoFar
<< "\n\nI have won!\nThanks for playing" << endl;
}
我的问题是行
Provider::getResponseToGuess(guess, isInWord, wordSoFar);
原来只是
getResponseToGuess(guess, isInWord, wordSoFar);
但我尝试了几种变体。我已尝试Provider::Provider
和Provider.getReponseToGuess
以及其他一些人,但似乎没有任何效果。谁能帮我这个?我只是试图从提供商文件中调用该函数。
Provider providerobj;
Guesser guesserobj;
providerobj.getResponseToGuess(guess, isInWord, wordSoFar, numMissedGuesses);
if (isInWord)
{
guesserobj.characterIsInWord(guess, wordSoFar);
}
我尝试的方式与使用providerobj的方式相同,但由于某种原因它无法工作。 这是猜测者的代码。
guesser.cpp
#include "guesser.h"
#include "game.h"
#include <iostream>
#include <fstream>
using namespace std;
const std::string Guesser::alphabet = "abcdefghijklmnopqrstuvwxyz";
// Initialize the guesser for a game wit hthe indicated wordlength,
// using words from an indicated file.
Guesser::Guesser (int wordLength, const char* wordListFilename)
{
for (int i = 0; i < 26; ++i)
charactersTried[i] = false;
string word;
ifstream in (wordListFilename);
while (in >> word)
{
if (word.size() == wordLength)
{
// word is of desired length
if (word.find_first_not_of(alphabet) == string::npos) {
// word contains only lowercse alphabetics
possibleSolutions.push_back (word);
}
}
}
in.close();
}
/**
* 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 Guesser::guessACharacter()
{
int counts[26];
for (int i = 0; i < 26; ++i)
counts[i] = 0;
// Count the number of words in which each letter can be found
for (int i = 0; i < possibleSolutions.size(); ++i)
{
string word = possibleSolutions[i];
for (char c = 'a'; c <= 'z'; ++c)
{
if (!charactersTried[c- 'a'])
{
// Character c has not been tried yet
if (word.find(c) != string::npos)
// c is in this word
++counts[c - 'a'];
}
}
}
// Find the character that occurs in the most words
char guess = ' ';
int maxSoFar = -1;
for (char c = 'a'; c <= 'z'; ++c)
{
if (counts[c - 'a'] > maxSoFar)
{
guess = c;
maxSoFar = counts[c - 'a'];
}
}
if (maxSoFar <= 0)
{
guess = 'a';
while (charactersTried[guess-'a'])
++guess;
}
charactersTried[guess-'a'] = true;
return guess;
}
/**
* 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 Guesser::characterIsInWord (char guess, const string& wordSoFar)
{
vector<string> remainingSolutions;
for (int i = 0; i < possibleSolutions.size(); ++i)
{
string wd = possibleSolutions[i];
bool OK = true;
for (int k = 0; OK && k < wordSoFar.size(); ++k)
{
if (wordSoFar[k] == guess)
{
if (wd[k] != guess)
{
OK = false;
}
}
}
if (OK)
{
//cerr << "Keeping " << wd << endl;
remainingSolutions.push_back (wd);
}
}
possibleSolutions = remainingSolutions;
}
/**
* 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 Guesser::characterIsNotInWord (char guess)
{
vector<string> remainingSolutions;
for (int i = 0; i < possibleSolutions.size(); ++i)
{
string wd = possibleSolutions[i];
if (wd.find(guess) == string::npos)
{
remainingSolutions.push_back (wd);
}
}
possibleSolutions = remainingSolutions;
}
/**
* Guesser has lost the game. Look at the provider's actual word
* and gripe a bit about losing.
*/
void Guesser::admitToLoss (std::string actualWord, const string& wordSoFar)
{
bool match = actualWord.size() == wordSoFar.size();
for (int i = 0; match && i < actualWord.size(); ++i)
{
match = wordSoFar[i] == Game::FILL_CHARACTER ||
wordSoFar[i] == actualWord[i];
}
if (!match)
{
cout << "Ummm...your word '" << actualWord
<< "' does not match the patterh '"
<< wordSoFar <<"'.\nDid you make a mistake somewhere?"
<< endl;
}
else
{
for (int i = 0; match && i < actualWord.size(); ++i)
{
if (wordSoFar[i] == Game::FILL_CHARACTER
&& charactersTried[actualWord[i]-'a'])
{
cout << "Did you forget to mention the '"
<< actualWord[i]
<< "' in position " << i+1 << "?"
<< endl;
return;
}
}
for (int i = 0; (!match) && i < possibleSolutions.size(); ++i)
match = (actualWord == possibleSolutions[i]);
match = match && (possibleSolutions.size() > 0);
if (match)
{
cout << "OK, I might have guessed that eventually." << endl;
}
else
{
cout << "Interesting, I don't know that word. Are you sure you\n"
<< "spelled it correctly?." << endl;
}
}
}
guesser.h
#ifndef GUESSER_H
#define GUESSER_H
#include <string>
#include <vector>
class Game;
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];
static const std::string alphabet;
};
#endif
答案 0 :(得分:0)
Provider :: getResponseToGuess(guess,isInWord,wordSoFar);将无效,因为getResponseToGuess不是静态函数。
Provider.getReponseToGuess也是错误的,因为Provider是一个类,而不是一个对象,你不能调用类名的类的成员函数。
首先必须创建该类的对象,然后使用该对象调用成员函数。
Provider provider_obj;
proveder_obj.getResponseToGuess(guess, isInWord, wordSoFar);
如果您是C ++的新手,我建议先尝试一些基本程序。