0

正如标题所述,我是第一次使用 Functions,我很难将它们应用到刽子手游戏中。根据我有限的知识,我假设我的变量范围超出了函数调用的范围。鉴于我完全是在书本上工作,我觉得我碰壁了。我在想可能会使用全局变量,但话又说回来,我正在写的那一章也只给了我对它们的有限理解。我也遇到了 srand(time(0)); 的问题。出于某种原因。任何帮助将不胜感激,并提前致谢。

// HangMan Game
// With Functions

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

char playerGuess();
char guessInWord();

int main()
{
    //setup
    const int MAX_WRONG = 8;//max wrong will be set at 8

    vector<string> words; //collection of possible words to guess
    words.push_back ("DAFTPUNK");
    words.push_back ("ELSALVADOR");
    words.push_back ("HIPHOP");

    srand(time(0));
    random_shuffle(words.begin(), words.end());
    const string THE_WORD = words[0];            //word to guess
    int wrong = 0;                               //number of incorrect guesses
    string soFar(THE_WORD.size(), '-');          //word guessed so far
    string used = "";                            //letters already guessed

    cout << "Welcome to Hangmam. Good luck!\n";

    //main loop
    while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
    {
        cout << "\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n";
        cout << "\nYou've used the following letters:\n" << used << endl;
        cout << "\nSo far, the word is:\n" << soFar << endl;

        char guess = playerGuess(); //call to function 1

        used += guess;

        guessInWord();              //call to function 2
    }

    //shut down
    if(wrong == MAX_WRONG)
        cout << "\nYou've been hanged!";
    else
        cout << "\nYou guessed it!";

    cout << "\nThe Word was " << THE_WORD << endl;

    return 0;
}

//function Definitions 1
char playerGuess()
{
    char letter;
    cout << "\n\nEnter your guess: ";
    cin >> letter;
    letter = toupper(letter); //makes uppercase of letter
    while (used.find(letter) != string::npos)
    {
        cout <<"\nYou've already guessed " << letter << endl;
        cout <<"Enter your guess: ";
        cin >> letter;
        letter = toupper(letter);
    }
    return letter;
}

//function Definitions 2
void guessInWord()
{
    if (THE_WORD.find(guess) != string::npos)
    {
        cout << "That's right! " << guess << " is in the word.\n";

        //update soFar to Include newly guessed letter
        for (int i = 0; i < THE_WORD.length(); ++i)
            if (THE_WORD[i] == guess)
                soFar[i] = guess;
    }
    else
    {
        cout << "Sorry, " << guess << " isn't in the word.\n";
        ++wrong;
    }
}
4

1 回答 1

1

您的函数原型不应与其实现不同:

void guessInWord();  ------> char guessInWord();
^^^^                         ^^^^

将这些设为全局(我个人不喜欢设为全局),因为您在其他函数中使用它们:

vector<string> words;
string used;
string THE_WORD;
int wrong;
string soFar;
char guess;

绝对会遇到运行时错误或逻辑问题。,继续...

char playerGuess();
char guessInWord();

vector<string> words;
string used = "";
string THE_WORD;
int wrong = 0;
string soFar(THE_WORD.size(), '-');
char guess;

int main()
{
    const int MAX_WRONG = 8;

    words.push_back("DAFTPUNK");
    words.push_back("ELSALVADOR");
    words.push_back("HIPHOP");

    THE_WORD = words[0];

    srand(time(0));
    random_shuffle(words.begin(), words.end());

    cout << "Welcome to Hangmam. Good luck!\n";

    while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
    {
        cout << "\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n";
        cout << "\nYou've used the following letters:\n" << used << endl;
        cout << "\nSo far, the word is:\n" << soFar << endl;

        guess = playerGuess();
        used += guess;
        guessInWord();
    }

    if (wrong == MAX_WRONG)
        cout << "\nYou've been hanged!";
    else
        cout << "\nYou guessed it!";

    cout << "\nThe Word was " << THE_WORD << endl;

    return 0;
}

char playerGuess()
{
    char letter;
    cout << "\n\nEnter your guess: ";
    cin >> letter;
    letter = toupper(letter); //makes uppercase of letter
    while (used.find(letter) != string::npos)
    {
        cout << "\nYou've already guessed " << letter << endl;
        cout << "Enter your guess: ";
        cin >> letter;
        letter = toupper(letter);
    }
    return letter;
}

char guessInWord()
{
    if (THE_WORD.find(guess) != string::npos)
    {
        cout << "That's right! " << guess << " is in the word.\n";

        for (int i = 0; i < THE_WORD.length(); ++i)
            if (THE_WORD[i] == guess)
                soFar[i] = guess;
    }
    else
    {
        cout << "Sorry, " << guess << " isn't in the word.\n";
        ++wrong;
    }
}
于 2013-04-23T04:11:45.620 回答