2014-12-19 26 views
-2

我想做一個hang子手項目,但是我的代碼不工作。每當我輸入正確的字母,代碼就會告訴我這是錯誤的(即使它是正確的)。不知道爲什麼 - 代碼在某些時候起作用,但我改變了一些東西,現在我不知道爲什麼它不起作用。所以這可能是一個簡單的修復,但我只是沒有看到它。C++ Hangman遊戲,如何製作「右」字母棒

任何幫助將不勝感激!

#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; 


} 
+0

檢查下面的行if(guessLetter == mysteryWord [x])'。測試重複進行,第二個嵌套部分從不運行。 – Sonic 2014-12-19 02:57:10

回答

1

你是不是在你的int letterFill()功能更新string guessWord。只要你看到一封與你相匹配的信件,而沒有輸入第二個if聲明。

我假設你想要的是隻有在完全更新guessWord,根據你想要做的就是通過串迭代,更新guessWord爲你找到匹配什麼,你的循環後做一次檢查,返回

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

+0

你能解釋一下嗎?看起來你在正確的軌道上。 – 2014-12-19 02:56:35

+0

我正在研究這個問題,只是想最初拋出問題的原因。 – 2014-12-19 02:58:58

+0

_「她」_?!? ... – 2014-12-19 03:00:37