2016-07-11 54 views
1

我一直在做一個hang子手遊戲,已經完成了幾乎所有的重碼。不過,我在測試連續輸入時遇到問題。如果用戶連續三次輸入「是」或「否」以外提示他們是否想玩hang子手,我想關閉該程序。如果用戶輸入了三個總計錯誤輸入,我只能想出一種方法來關閉程序。以下是我的代碼。任何幫助深表感謝!遊戲連續輸入檢查

#include <iostream> 
#include <string> 
#include "MyFuncts.h" 
#include "randword.h" 
using namespace std; 

void drawHangman(int incorrectCount); 
int askToPlay(); 
int response = 0; 
int incorrectCount = 0; 
void gameSequence(); 

int main() 
{ 
string reply = " "; 
int consecutiveErrors = 0; 
getWords("hangman.dat"); 
do 
{ 
    askToPlay(); 
    if (response == PLAY) 
    { 
     gameSequence(); 
    } 
    else if (response == STOP) 
    { 
     cout << "Goodbye\n"; 
     break; 
    } 
    else 
    { 
     consecutiveErrors++; 
     cout << "ERROR\n"; 
    } 
} while (getNextWord() != "" && consecutiveErrors < 3); 
system("pause"); 
} 

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"; 
} 

int askToPlay() 
{ 
string reply = " "; 
cout << "\nDo you want to play hangman? (y or n): "; 
cin >> reply; 
response = promptYN(reply); 
return response; 
} 

void gameSequence() 
{ 
string guessWord = " "; 
char guessLetter = ' '; 
cout << "Let's PLAY\n\n"; 
guessWord = strToUpper(getNextWord()); 
cout << "Word to Guess: " << guessWord << endl << endl; 

while (incorrectCount < 6) 
{ 
    drawHangman(incorrectCount); 
    cout << "Enter a letter to guess: "; 
    cin >> guessLetter; 
    guessLetter = toupper(guessLetter); 
    cout << "You entered: " << guessLetter << endl << endl; 
    if (guessWord.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) 
     { 
      drawHangman(incorrectCount); 
      cout << "Sorry you lose - the word was: " << guessWord << endl << endl; 
     } 
    } 
} 
incorrectCount = 0; 
} 
+0

當你開始一個遊戲時,在你調用'gameSequence()' – infixed

回答

0

只是重置consecutiveErrors0每當response等於PLAY

while循環的if語句將類似於以下:

if (response == PLAY) { 
    consecutiveErrors = 0; // add this line 
    gameSequence(); 
} else if (response == STOP) { 
    cout << "Goodbye\n"; 
    break; 
} else { 
    consecutiveErrors++; 
    cout << "ERROR\n"; 
} 
+0

之前,你有沒有考慮過將連貫的錯誤設置爲零?謝謝Jonny,那就是訣竅! –

+0

沒問題@WallaceMogerty如果這回答了您的問題,您可以將其標記爲接受的答案。 –

0

在你的主循環,你增加的每個錯誤的consecutiveErrors櫃檯,但你是不是在一個可接受的響應清除它。只要您獲得可接受的響應,只需將consecutiveErrors設置爲0即可。