C ++ Hangman游戏

时间:2016-07-11 16:17:03

标签: 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";
}

1 个答案:

答案 0 :(得分:2)

您有计数器(如incorrectCountconsecutiveErrors),应在适当时重置;例如,在新游戏开始时。