游戏连续输入检查
Game Consecutive Input Check
我一直在开发一款刽子手游戏,几乎完成了所有繁重的编码工作。但是,我在测试连续输入时遇到问题。如果用户输入 "yes" 或 "no" 以外的内容连续三次 如果他们想玩刽子手,我想关闭程序。如果用户输入了三个 total 错误输入,我只能想出一种关闭程序的方法。下面是我的代码。任何帮助深表感谢!
#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;
}
每当 response
等于 PLAY
时,只需将 consecutiveErrors
重置为 0
。
while
循环中的 if
语句类似于以下内容:
if (response == PLAY) {
consecutiveErrors = 0; // add this line
gameSequence();
} else if (response == STOP) {
cout << "Goodbye\n";
break;
} else {
consecutiveErrors++;
cout << "ERROR\n";
}
在您的主循环中,您会在每个错误上递增 consecutiveErrors
计数器,但您不会在收到可接受的响应后将其清除。当您得到可接受的响应时,只需将 consecutiveErrors
设置为 0。
我一直在开发一款刽子手游戏,几乎完成了所有繁重的编码工作。但是,我在测试连续输入时遇到问题。如果用户输入 "yes" 或 "no" 以外的内容连续三次 如果他们想玩刽子手,我想关闭程序。如果用户输入了三个 total 错误输入,我只能想出一种关闭程序的方法。下面是我的代码。任何帮助深表感谢!
#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;
}
每当 response
等于 PLAY
时,只需将 consecutiveErrors
重置为 0
。
while
循环中的 if
语句类似于以下内容:
if (response == PLAY) {
consecutiveErrors = 0; // add this line
gameSequence();
} else if (response == STOP) {
cout << "Goodbye\n";
break;
} else {
consecutiveErrors++;
cout << "ERROR\n";
}
在您的主循环中,您会在每个错误上递增 consecutiveErrors
计数器,但您不会在收到可接受的响应后将其清除。当您得到可接受的响应时,只需将 consecutiveErrors
设置为 0。