C++ 刽子手游戏

标签 c++ loops

<分区>

我是编程初学者,正在通过制作刽子手游戏进行练习。用户必须输入 yes 才能开始游戏序列,输入 no 才能关闭程序,否则任何其他输入都会导致错误消息。如果连续出现三个错误消息,程序应该关闭。在测试程序时,我注意到如果在说是开始游戏之前输入了所有三个错误,程序只会捕获连续的错误。下面是我的代码:

#include <iostream>
#include <string>
#include "MyFuncts.h"
#include "randword.h"
using namespace std;

int incorrectCount = 0;
int consecutiveErrors = 0;
void drawHangman(int incorrectCount);

int main()
{
getWords("hangman.dat");

string reply;
string wordToGuess;
char guessLetter;
do
{
    location2: if (consecutiveErrors == 3)
    break;
    cout << "\nDo you want to play hangman? (y or n): ";
    cin >> reply;
    promptYN(reply);
    if (promptYN(reply) == PLAY)
    {
        cout << "Let's PLAY\n\n";
        wordToGuess = strToUpper(getNextWord());
        if (wordToGuess == "")
            break;
        cout << "Word to Guess: " << wordToGuess << endl << endl;
        while (incorrectCount < 6 && wordToGuess != "")
        {
            drawHangman(incorrectCount);
            cout << "Enter a letter to guess: ";
            cin >> guessLetter;
            guessLetter = toupper(guessLetter);
            cout << "You entered: " << guessLetter << endl << endl;
            if (wordToGuess.find(guessLetter) != string::npos)
                cout << guessLetter << " is in the word to guess.\n\n";
            else
            {
                cout << guessLetter << " is NOT in the word to guess.\n\n";
                incorrectCount++;
            }
                if (incorrectCount == 6)
                {
                    cout << " -------|\n"
                        " |      |\n"
                        " O      |\n"
                        "-|-     |\n"
                        "/ \\     |\n"
                        "        |\n"
                        "      -----\n\n";
                    cout << "Sorry you lose - the word was: " << wordToGuess << endl << endl;
                    incorrectCount = 0;
                    cout << "\nDo you want to play hangman? (y or n): ";
                    cin >> reply;
                    promptYN(reply);
                    if (promptYN(reply) == PLAY)
                    {
                        consecutiveErrors = 0;
                        cout << "Let's PLAY\n\n";
                        wordToGuess = strToUpper(getNextWord());
                        if (wordToGuess == "")
                            break;
                        cout << "Word to Guess: " << wordToGuess << endl << endl;
                        continue;
                    }
                    else if (promptYN(reply) == STOP)
                        goto location3;
                    else
                        goto location4;
                }
            }
        }
    else if (promptYN(reply) == STOP)
    {
        location3: cout << "Goodbye";
        break;
    }
    else
    {
        location4: consecutiveErrors++;
        cout << "Error - please enter (y or n)\n";
        goto location2;
        }
} while (wordToGuess != "" && consecutiveErrors < 3);
}

void drawHangman(int incorrectCount)
{
    if (incorrectCount == 0)
    cout << " -------|\n"
            " |      |\n"
            "        |\n"
            "        |\n"
            "        |\n"
            "        |\n"
            "      -----\n\n";
else if (incorrectCount == 1)
    cout << " -------|\n"
            " |      |\n"
            " O      |\n"
            "        |\n"
            "        |\n"
            "        |\n"
            "      -----\n\n";
else if (incorrectCount == 2)
    cout << " -------|\n"
            " |      |\n"
            " O      |\n"
            " |      |\n"
            "        |\n"
            "        |\n"
            "      -----\n\n";
else if (incorrectCount == 3)
    cout << " -------|\n"
            " |      |\n"
            " O      |\n"
            "-|      |\n"
            "        |\n"
            "        |\n"
            "      -----\n\n";
else if (incorrectCount == 4)
    cout << " -------|\n"
            " |      |\n"
            " O      |\n"
            "-|-     |\n"
            "        |\n"
            "        |\n"
            "      -----\n\n";
else if (incorrectCount == 5)
    cout << " -------|\n"
            " |      |\n"
            " O      |\n"
            "-|-     |\n"
            "/       |\n"
            "        |\n"
            "      -----\n\n";
else
    cout << " -------|\n"
            " |      |\n"
            " O      |\n"
            "-|-     |\n"
            "/ \\     |\n"
            "        |\n"
            "      -----\n\n";
}

最佳答案

你有一些计数器(比如 incorrectCountconsecutiveErrors)应该在适当的时候重置;例如,在新游戏开始时。

关于C++ 刽子手游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38311648/

相关文章:

c++ - 计算 VS2015 C++ 项目的代码指标

c++ - 如何访问 boost::spirit::karma 中嵌套对象的数据?

c - 为什么中断是停止程序而不是跳回?

javascript - 如何使用 jquery 显示点击时的动态数据

c - 如何保持运行总计

matlab - 如何将矩阵的每一行除以固定行?

c++ - 使用 2 种不同方法时的 O(N) 差异很大

c++ - 为什么 MSVC 选择 long long 作为 -2147483648 的类型?

c++ - Common LNK 2019 - 头文件中的函数声明 - 源文件中的定义

loops - iOS9 Beta 和 MusicTrackLoopInfo