gpt4 book ai didi

c++ - 游戏连续输入检查

转载 作者:太空宇宙 更新时间:2023-11-04 13:14:04 25 4
gpt4 key购买 nike

我一直在开发一款刽子手游戏,几乎完成了所有繁重的编码工作。但是,我在测试连续输入时遇到问题。如果用户在系统提示他们想玩刽子手时 连续三次 输入了除"is"或“否”之外的其他内容,我想关闭程序。如果用户输入了三个 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";
}

关于c++ - 游戏连续输入检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38315109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com