C++ C++;刽子手游戏
我是一个编程初学者,正在通过制作一个刽子手游戏进行练习。用户必须输入“是”以开始游戏序列,输入“否”以关闭程序,或输入错误消息中的任何其他输入结果。如果连续出现三条错误消息,程序应关闭。在测试程序时,我注意到,如果在说“是”开始游戏之前输入了所有三个错误,则程序只捕获连续错误。下面是我的代码:C++ C++;刽子手游戏,c++,loops,C++,Loops,我是一个编程初学者,正在通过制作一个刽子手游戏进行练习。用户必须输入“是”以开始游戏序列,输入“否”以关闭程序,或输入错误消息中的任何其他输入结果。如果连续出现三条错误消息,程序应关闭。在测试程序时,我注意到,如果在说“是”开始游戏之前输入了所有三个错误,则程序只捕获连续错误。下面是我的代码: #include <iostream> #include <string> #include "MyFuncts.h" #include "randword.h" using na
#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";
}
#包括
#包括
#包括“MyFuncts.h”
#包括“randword.h”
使用名称空间std;
int incorrectCount=0;
int连续误差=0;
无效提款人(整数不正确计数);
int main()
{
getWords(“hangman.dat”);
字符串回复;
字符串字串;
字母字符;
做
{
位置2:if(连续错误==3)
打破
cout>回复;
promptYN(回复);
如果(提示(回复)=播放)
{
您是否有计数器(如不正确计数
和连续错误
),应在适当时重置这些计数器;例如,在新游戏开始时。您有计数器(如不正确计数
和连续错误
)在适当的时候应该重新设置;例如,在新游戏开始时。你是否用调试器逐步调试了代码?你的问题是?我强烈建议你摆脱使用goto
的习惯。你可以使用循环和函数来替换goto
,以及它产生的代码。我预测很少有人愿意看代码就是意大利面。当你用goto
语句来迷惑代码时,这就是你要冒的风险——那些试图帮助你的人不会坐在那里试图解开goto
代码。我希望即使这行得通,任何好的教授都会因为使用goto而扣分。@LightnessRacesinOrbit是使用goto
的有效理由。此代码不代表其中任何一个,而是代表对糟糕程序设计的补偿。您是否使用调试器逐步完成代码?您的问题是?我强烈建议您摆脱使用goto
的习惯。您可以使用循环和函数替换goto代码>和它产生的结果。我预测很少有人会想看意大利面上的代码。这就是当你用goto
语句来迷惑代码时你所冒的风险——那些试图帮助你的人不会坐在那里试图解开goto
代码。我预计,即使这样做有效,任何好的教授都会帮助你ke指出使用goto是错误的。@LightnessRacesinOrbit使用goto
是有正当理由的。此代码不代表任何goto,而是代表对程序设计不当的补偿。