C++ 刽子手游戏

C++ Hangman Game

本文关键字:游戏 刽子手 C++      更新时间:2023-10-16

我是编程初学者,正在通过制作刽子手游戏来练习。用户必须输入 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 PLAYnn";
        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.nn";
            else
            {
                cout << guessLetter << " is NOT in the word to guess.nn";
                incorrectCount++;
            }
                if (incorrectCount == 6)
                {
                    cout << " -------|n"
                        " |      |n"
                        " O      |n"
                        "-|-     |n"
                        "/ \     |n"
                        "        |n"
                        "      -----nn";
                    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 PLAYnn";
                        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"
            "      -----nn";
else if (incorrectCount == 1)
    cout << " -------|n"
            " |      |n"
            " O      |n"
            "        |n"
            "        |n"
            "        |n"
            "      -----nn";
else if (incorrectCount == 2)
    cout << " -------|n"
            " |      |n"
            " O      |n"
            " |      |n"
            "        |n"
            "        |n"
            "      -----nn";
else if (incorrectCount == 3)
    cout << " -------|n"
            " |      |n"
            " O      |n"
            "-|      |n"
            "        |n"
            "        |n"
            "      -----nn";
else if (incorrectCount == 4)
    cout << " -------|n"
            " |      |n"
            " O      |n"
            "-|-     |n"
            "        |n"
            "        |n"
            "      -----nn";
else if (incorrectCount == 5)
    cout << " -------|n"
            " |      |n"
            " O      |n"
            "-|-     |n"
            "/       |n"
            "        |n"
            "      -----nn";
else
    cout << " -------|n"
            " |      |n"
            " O      |n"
            "-|-     |n"
            "/ \     |n"
            "        |n"
            "      -----nn";
}

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