简体   繁体   English

如何跟踪在C ++中的Hangman游戏中猜出的字母?

[英]How do I keep track of letters guessed in a Hangman game in C++?

I have been trying to fix this program for the past two days and it is proving to be quite troublesome. 在过去的两天里,我一直在尝试修复此程序,事实证明这很麻烦。 It is an assignment for my intro to C++ course and has given me nothing but trouble. 这是我介绍C ++课程的一项工作,除了给我带来麻烦之外,没有给我带来任何麻烦。 I have searched this board, posted on Cplusplus.com and spent hours on Google, looking for some assistance. 我已经搜索了该板,并将其发布在Cplusplus.com上,并在Google上花费了数小时,以寻求帮助。

Here is my problem. 这是我的问题。 I have been given a program and need to add a few features to it: 我已获得一个程序,需要在其中添加一些功能:

  1. I have to save the users entries. 我必须保存用户条目。
  2. I have to display an error message if the user enters the same entry twice. 如果用户两次输入相同的条目,我必须显示一条错误消息。

Seems simple? 看起来很简单? Not for a beginner such as myself. 不适合像我这样的初学者。 Here is the code, with what I have attempted to add to it in order to meet the problem's requirements. 这是代码,为了满足问题的要求,我尝试将其添加到其中。

int main()
{
    //declare variables
    string origWord = "";
    string letter = "";
    char dashReplaced = 'N';
    char gameOver = 'N';
    int numIncorrect = 0;
    string displayWord = "-----";
    string letterGuess[26];

    //get original word
    do //begin loop
    {
        cout << "Enter a 5-letter word in uppercase: ";
        getline(cin, origWord);
    } while (origWord.length() != 5);

    //clear the screen
    system("cls");

    //start guessing
    cout << "Guess this word: " <<
    displayWord << endl;

    while (gameOver == 'N')
    {
        cout << "Enter an uppercase letter: ";
        cin >> letter;

        //Entry Storage and Error Message. This is my problem.
        for (int x = 0; x < 26; x++)
        {
            letterGuess[x] = letter;
            for (int i = x; i < 26; i++)
            {
                if (i != x)
                {
                    if (letterGuess[x] == letterGuess[i])
                    {
                        cout << "Letter already entered. Choose another letter."
                        << endl;
                    }
                }
            }
        }

        //search for the letter in the original word
        for (int x = 0; x < 5; x += 1)
        {
            //if the current character matches
            //the letter, replace the corresponding
            //dash in the displayWord variable and then
            //set the dashReplaced variable to 'Y'

            if (origWord.substr(x, 1) == letter)
            {
                displayWord.replace(x, 1, letter);
                dashReplaced = 'Y';
            } //end if
        } //end for

        //if a dash was replaced, check whether the
        //displayWord variable contains any dashes

        if (dashReplaced == 'Y')
        {
            //if the displayWord variable does not
            //contain any dashes, the game is over

            if (displayWord.find("-", 0) == -1)
            {
                gameOver = 'Y';
                cout << endl << "Yes, the word is "
                << origWord << endl;
                cout << "Great guessing!" << endl;
            }
            else //otherwise, continue guessing
            {
                cout << endl << "Guess this word: "
                << displayWord << endl;
                dashReplaced = 'N';
            } //end if
        }
        else //processed when dashReplaced contains 'N'
        {
            //add 1 to the number of incorrect guesses
            numIncorrect += 1;
            //if the number of incorrect guesses is 10,
            //the game is over
            if (numIncorrect == 10)
            {
                gameOver = 'Y';
                cout << endl << "Sorry, the word is "
                << origWord << endl;
            } //end if
        } //end if
    } //end while

    system("pause");
    return 0;
} //end of main function

My only edit to the program is directly under the header of Entry Storage and Error Message. 我对该程序的唯一编辑是直接在Entry Storage和Error Message的标题下。 I have tried a single for loop, but that simply displayed the error message for every letter entered. 我已经尝试过一个for循环,但是它只是显示输入的每个字母的错误消息。 Not only that but it displayed it 26 times. 不仅如此,它还显示了26次。 Adding a Break command fixed that and it only displayed once. 添加一个Break命令可以解决该问题,并且只显示一次。 However, it still displayed on every entry. 但是,它仍显示在每个条目上。

A member of Cplusplus, pointed out that I was incorrectly testing the same variable against the array in the same location. Cplusplus的一名成员指出,我在同一位置针对数组错误地测试了同一变量。 That is why it displayed the error on every entry. 这就是为什么它在每个条目上都显示错误。 Now with this loop, the error only displays when an entry is entered twice. 现在,使用此循环,仅当输入两次条目时才会显示错误。 However, the error message displays all 26 times once more. 但是,错误消息再次显示所有26次。 On top of that, it will only error if the letters are entered one after another. 最重要的是,只有字母一个接一个地输入,它才会出错。

For example, if I enter A then X then A again, no error is shown. 例如,如果我输入A,然后输入X,然后再次输入A,则不会显示任何错误。 If I enter, A then A again, the error is displayed 26 times. 如果我输入,则再次输入A,然后输入A,该错误将显示26次。 Something is clearly wrong with how the letter variable is being entered into the array on top of the whatever is causing the error message to display multiple times. 在导致错误消息多次显示的原因之上,如何将letter变量输入到数组中显然是错误的。

Any amount of assistance would be greatly appreciated. 任何援助将不胜感激。

Edit: My professor has gotten back to me and suggested using the following instead of what I have been tinkering with: 编辑:我的教授回到我身边,建议使用以下内容代替我一直在修改的内容:

for (int x=0; x<5; x++)
 if (origWord[x] == letterEntered)
    origWord[x] = '-';

Is it just me, or does this miss the mark completely? 只是我,还是这完全错过了标记? I haven't tried converting it into my program as a simple copy and paste job produces compile errors. 我没有尝试将其转换为程序,因为简单的复制和粘贴作业会产生编译错误。 However, I don't see how that does anything with what I'm trying to do. 但是,我看不到我要做什么。

This set's all entries of your letterGuess array to the most recently guessed letter. letterGuess您的letterGuess数组中的所有条目都letterGuess最近猜到的字母。

letterGuess[x] = letter;

This isn't what you want. 这不是你想要的。

You need to think about the actual algorithm you need to implement: 您需要考虑需要实现的实际算法:

  1. The user enters a guess 用户输入一个猜测
  2. Check to see if they've already guessed that letter 检查他们是否已经猜到了那封信
  3. If they have, display an error message, return to 1. 如果有,则显示错误消息,然后返回1。
  4. If they have not, save that guess, continue with the game logic. 如果没有,请保留该猜测,然后继续游戏逻辑。

If you have already learned about standard containers, this can be trivially done with a std::set , or a std::vector that has been sorted. 如果您已经了解了标准容器,则可以使用std::set或已排序的std::vector轻松完成。

You need to compare each element in the array to the guessed word. 您需要将数组中的每个元素与猜词进行比较。 Best use a for loop for this. 最好为此使用for循环。 No more needs to be said if this is an assignment. 如果这是一项任务,则无需多说。

Also don't use system("cls") in your program, it is a massive security flaw and may lose you marks. 另外,请勿在您的程序中使用system("cls") ,这是一个巨大的安全漏洞,可能会给您造成损失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM