如何在 c++ 中使用 "int promptYN(string reply)" 结束刽子手游戏

How to end hangman game using "int promptYN(string reply)" in c++

本文关键字:string reply 游戏 刽子手 结束 promptYN int c++      更新时间:2023-10-16

在我的刽子手游戏中,我无法结束游戏。使用我定义的头文件...

#define PLAY 1 #define STOP 0 #define ERROR -1

。然后我定义了函数

int promptYN(string reply)
{
    reply = strToUpper(reply);
    if (reply == "YES", "OK", "SURE", "Y")
        return PLAY;
    else if (reply == "NO", "QUIT", "STOP", "TERMINATE", "N", "Q")
        return STOP;
    else
        return ERROR;
}

。现在,无论用户键入什么,该函数都已在main中调用,即使我以"NO","QUIT","STOP","TERMINATE","N","Q"响应。

这是我用来尝试玩游戏的while循环,我只需要帮助弄清楚用户何时响应"Y",然后游戏播放,"N"然后游戏停止,或ERROR和游戏重置为再次提问。谢谢!

    cout << "Do you want to play hangman? (y or n): ";
    cin >> userReply;
    gameStatus = promptYN(userReply);
    while (gameStatus != STOP)
    {
        if (gameStatus == PLAY)
        {
            chances = 0;
            cout << "Let's Playnn";
            guessWord = getNextWord();                  // Function call (randword.h)
            guessWord = strToUpper(guessWord);          // Function call (MyFuncts.h)
            cout << "nWord to Guess: " << guessWord << endl;
            cout << endl << h1;

            while (wrong != 6)                          // Function to find out which hangman board to print to user
            {
                cout << "nEnter a letter to guess: ";
                cin >> gLetter;
                gLetter = toupper(gLetter);
                cout << "You entered: " << gLetter << endl << endl;
                cout << gLetter << " is NOT in the word to guess.";
                wrong++;
                if (wrong == 0)
                    cout << endl << h1 << endl;
                if (wrong == 1)
                    cout << endl << h2 << endl;
                if (wrong == 2)
                    cout << endl << h3 << endl;
                if (wrong == 3)
                    cout << endl << h4 << endl;
                if (wrong == 4)
                    cout << endl << h5 << endl;
                if (wrong == 5)
                    cout << endl << h6 << endl;
                if (wrong == 6)
                    cout << endl << h7 << endl;
            }
        }
        else if (gameStatus == STOP)
        {
            cout << "Goodbyen";
        }
        else
        {
            cout << "Error - please enter (y or n)n";
        }
        cout << "Do you want to play hangman? (y or n): ";
        cin >> userReply;
        gameStatus = promptYN(userReply);
    }

这一行:

if (reply == "YES", "OK", "SURE", "Y")

不会将reply与这些字符串中的每一个进行比较。 为此,您必须单独比较字符串:

int promptYN(string reply)
{
    if (reply == "YES" || reply =="OK" || reply == "SURE" || reply == "Y")
        return PLAY;
    //...
}

这将有效,但是在C++中设置的更好方法是使用类似std::set<std::string>的东西并查看该值是否存在:

#include <set>
//...
int promptYN(string reply)
{
    static std::set<std::string> OKResponse = {"YES", "OK", "SURE", "Y"};
    static std::set<std::string> StopResponse = {"NO", "QUIT", "STOP", 
                                                 "TERMINATE", "N", "Q"};
    if ( OKResponse.count(reply) == 1 )
      return PLAY;
    else
    if (StopResponse.count(reply) == 1)
       return STOP;
    return ERROR;
}

如果集合中存在该值,则 std::set::count 将返回 1,否则返回 0。

要向"ok"或"stop"添加另一个响应,只需向每个set值添加更多字符串。

现场示例

注意:确保您有一个兼容的C++11编译器,用于设置std::set,并确保static以线程安全的方式工作。