2016-07-11 24 views
0

私はプログラミングの初心者です。ハングマンゲームを作って練習しています。ユーザは、ゲームシーケンスを開始するにはyesを入力し、プログラムを閉じるにはnoを入力するか、その他の入力はエラーメッセージになります。 3つの連続したエラーメッセージが発生した場合は、プログラムを終了する必要があります。プログラムをテストするとき、私はゲームを開始するためにyesと言う前に3つ全てがタイプされれば、プログラムが連続エラーを捕まえるだけであることに気づいた。以下は私のコードです:C++ Hangman Game

#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"; 
} 
+5

デバッガでコードをステップ実行しましたか? – pm100

+5

あなたの質問は?また、gotoを使う習慣から抜け出すことを強くお勧めします。ループや関数を使って 'goto'とそれが生成する[スパゲッティコード](https://en.wikipedia.org/wiki/Spaghetti_code)を置き換えることができます。 – NathanOliver

+3

私は、スパゲッティのコードを見たい人はほとんどいないと予測します。それはあなたがあなたのコードを「goto」ステートメントで謎めいているときに取るリスクです。助けようとする人はそこに座り、 'goto'コードを解くことを試みません。 – PaulMcKenzie

答えて

2

あなたが適切にリセットされなければならない(incorrectCountconsecutiveErrorsなど)のカウンターを持っています。例えば、新しいゲームの開始時に。