C ++ Hangman Game,如何制作“正确”字母棒

时间:2014-12-19 02:50:41

标签: c++

我正在尝试做一个刽子手项目,但我的代码无效。每当我输入正确的字母时,代码告诉我它是错误的(即使它是正确的)。不确定为什么 - 代码在某些时候工作但我改变了一些东西,现在我不知道为什么它不起作用。所以它可能是一个简单的修复,但我只是没有看到它。

任何帮助都将非常感谢!

#include <iostream>

using namespace std;

int letterFill (char, string, string&);


int main()
{
    string name;
    int maxAttempts = 5;
    int wrongGuesses;
    char letter;

    srand(time(0));

    const string wordList[15] = { "hanukkah", "sparklers", "mistletoe", "menorah", "presents", "reindeer",
        "kwanzaa", "snowman", "eggnog", "celebration", "yuletide", "resolution", "nutcracker", "ornaments", "gingerbread" };

    string correctWord = wordList[rand() % 15];
    string unknown(correctWord.length(),'*');

    cout << correctWord << endl;

    cout << "Welcome to a fun game of winter holiday hangman! What is your name? " << endl;
    cin >> name;
    cout << name <<", there are some simple things you should know about this game before you start playing!" << endl;
    cout << "You will be trying to guess a randomly selected word by typing in ONE letter at a time " << endl;
    cout << "You will have " << maxAttempts << " tries before losing the game " << endl;
    cout << "And remember, all of the words are winter holiday related. Good luck " << name <<"!" << endl;
    cout << "*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*" <<endl;

    while (wrongGuesses == 0)
    {
        cout << "Guess a letter" << cout;
        cin >> letter;

        if (letterFill(letter, correctWord, unknown)==0)
        {
            cout << endl << "That letter is not in this word! Try again " << endl;
            wrongGuesses = wrongGuesses + 1;
        }

        else
        {
            cout << endl << "You found a letter! Keep up the good work! " << endl;
        }

        if (correctWord==unknown)
        {
            cout << correctWord << endl;
            cout << "Congratulations! You guessed the correct word!" << endl;

        }

    }

    while (wrongGuesses == 1)
    {
        cout << "You have 4 guesses left " << endl;
        cout << "Guess a letter " << cout;
        cin >> letter;

      if (letterFill(letter, correctWord, unknown)==0)
        {
            cout << endl << "That letter is not in this word! Try again " << endl;
            wrongGuesses = wrongGuesses + 1;
        }
        else
        {
            cout << endl << "You found a letter! Keep up the good work! " << endl;
        }

        if (correctWord==unknown)
        {
            cout << correctWord << endl;
            cout << "Congratulations! You guessed the correct word!" << endl;

        }
    }

     while (wrongGuesses == 2)
    {
        cout << "You have 3 guesses left " << endl;
        cout << "Guess a letter " << cout;
        cin >> letter;

      if (letterFill(letter, correctWord, unknown)==0)
        {
            cout << endl << "That letter is not in this word! Try again " << endl;
            wrongGuesses = wrongGuesses + 1;
        }
        else
        {
            cout << endl << "You found a letter! Keep up the good work! " << endl;
        }

        if (correctWord==unknown)
        {
            cout << correctWord << endl;
            cout << "Congratulations! You guessed the correct word!" << endl;

        }
    }

     while (wrongGuesses == 3)
    {
        cout << "You have 2 guesses left " << endl;
        cout << "Guess a letter " << cout;
        cin >> letter;
          if (letterFill(letter, correctWord, unknown)==0)
        {
            cout << endl << "That letter is not in this word! Try again " << endl;
            wrongGuesses = wrongGuesses + 1;
        }
        else
        {
            cout << endl << "You found a letter! Keep up the good work! " << endl;
        }

        if (correctWord==unknown)
        {
            cout << correctWord << endl;
            cout << "Congratulations! You guessed the correct word!" << endl;

        }
    }


     while (wrongGuesses == 4)
    {
        cout << "You have 1 guess left " << endl;
        cout << "Guess a letter " << cout;
        cin >> letter;

          if (letterFill(letter, correctWord, unknown)==0)
        {
            cout << endl << "That letter is not in this word! Try again " << endl;
            wrongGuesses = wrongGuesses + 1;
        }
        else
        {
            cout << endl << "You found a letter! Keep up the good work! " << endl;
        }

        if (correctWord==unknown)
        {
            cout << correctWord << endl;
            cout << "Congratulations! You guessed the correct word!" << endl;

        }
    }

     while (wrongGuesses == 5)
    {
        cout << "Sorry " << name << " you have made 5 wrong guesses!" << endl;
        cout << "Game over. Click any key to exit. Play again soon :) " << endl;
      if (letterFill(letter, correctWord, unknown)==0)
        {
            cout << endl << "That letter is not in this word! Try again " << endl;
            wrongGuesses = wrongGuesses + 1;
        }
        else
        {
            cout << endl << "You found a letter! Keep up the good work! " << endl;
        }

        if (correctWord==unknown)
        {
            cout << correctWord << endl;
            cout << "Congratulations! You guessed the correct word!" << endl;

        }
    }

    system("pause");
    return 0;
}


int letterFill (char guessLetter, string mysteryWord, string& guessWord)
{
    int x;
    int matches=0;
    int lengthWord=mysteryWord.length();
    for (x = 0; x< lengthWord; x++)
    {

        if (guessLetter == mysteryWord[x])
            return 0;

        if (guessLetter == mysteryWord[x])
        {
            guessWord[x] = guessLetter;
            matches++;
        }
    }
    return matches;


}

1 个答案:

答案 0 :(得分:1)

您没有更新string guessWord功能中的int letterFill()。只要您看到匹配的信件,就会在不输入第二个if声明的情况下返回。

我假设你想要的只是在完全更新guessWord之后返回,基于你想要做的是遍历字符串,在找到匹配时更新guessWord并在你的循环之后做一个检查

if(matches == 0) return 0; else return matches;