我一直在做一個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;
}
當你開始一個遊戲時,在你調用'gameSequence()' – infixed