0
저는 프로그래밍 초보자이며 행맨 게임을 연습하여 연습합니다. 사용자는 게임 순서를 시작하려면 예를 입력하고, 프로그램을 닫으려면 no를 입력해야하고, 다른 입력은 오류 메시지를 야기해야합니다. 3 개의 연속적인 오류 메시지가 발생하면 프로그램을 닫아야합니다. 프로그램을 테스트 할 때 게임을 시작하기 전에 세 단어를 모두 입력하면 프로그램에서 연속 오류 만 catch한다는 것을 알게되었습니다. 다음은 내 코드입니다 : 당신은 적절한 경우 재설정해야 카운터 (같은 incorrectCount
및 consecutiveErrors
)가C++ 행맨 게임
#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";
}
디버거로 코드를 단계별로 실행 했습니까? – pm100
그리고 귀하의 질문은 무엇입니까? 또한 나는 'goto'를 사용하는 습관을 버릴 것을 강력하게 제안합니다. 루프와 함수를 사용하여 'goto'와 생성 한 [스파게티 코드] (https://en.wikipedia.org/wiki/Spaghetti_code)를 대체 할 수 있습니다. – NathanOliver
나는 스파게티 코드를 보길 원하는 사람이 거의 없을 것이라고 예측합니다. 그것은 당신이'goto' 진술로 당신의 코드를 수수께끼로 만들 때 당신이 취할 위험입니다 - 도와 주려고하는 사람들은 거기 앉아서'goto' 코드를 푸는 시도를하지 않을 것입니다. – PaulMcKenzie