gpt4 book ai didi

C++ 刽子手游戏

转载 作者:行者123 更新时间:2023-11-28 02:03:59 28 4
gpt4 key购买 nike

<分区>

我是编程初学者,正在通过制作刽子手游戏进行练习。用户必须输入 yes 才能开始游戏序列,输入 no 才能关闭程序,否则任何其他输入都会导致错误消息。如果连续出现三个错误消息,程序应该关闭。在测试程序时,我注意到如果在说是开始游戏之前输入了所有三个错误,程序只会捕获连续的错误。下面是我的代码:

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

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