C++ 刽子手游戏
C++ Hangman Game
我是编程初学者,正在通过制作刽子手游戏来练习。用户必须输入 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 PLAYnn";
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.nn";
else
{
cout << guessLetter << " is NOT in the word to guess.nn";
incorrectCount++;
}
if (incorrectCount == 6)
{
cout << " -------|n"
" | |n"
" O |n"
"-|- |n"
"/ \ |n"
" |n"
" -----nn";
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 PLAYnn";
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"
" -----nn";
else if (incorrectCount == 1)
cout << " -------|n"
" | |n"
" O |n"
" |n"
" |n"
" |n"
" -----nn";
else if (incorrectCount == 2)
cout << " -------|n"
" | |n"
" O |n"
" | |n"
" |n"
" |n"
" -----nn";
else if (incorrectCount == 3)
cout << " -------|n"
" | |n"
" O |n"
"-| |n"
" |n"
" |n"
" -----nn";
else if (incorrectCount == 4)
cout << " -------|n"
" | |n"
" O |n"
"-|- |n"
" |n"
" |n"
" -----nn";
else if (incorrectCount == 5)
cout << " -------|n"
" | |n"
" O |n"
"-|- |n"
"/ |n"
" |n"
" -----nn";
else
cout << " -------|n"
" | |n"
" O |n"
"-|- |n"
"/ \ |n"
" |n"
" -----nn";
}
您有计数器(如 incorrectCount
和 consecutiveErrors
),应在适当时重置;例如,在新游戏开始时。
相关文章:
- 在C++游戏中与库存系统作斗争
- 换位表导致测试失败(但在游戏中运行良好)
- Craps游戏问题,忽略if语句
- 矢量下标超出SFML游戏中的范围
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 游戏体验和升级
- 试图创建一个体面的基本刽子手游戏。猜出的字母不会取代短语中的字母
- 我正在制作一个刽子手游戏,我需要帮助弄清楚为什么它不能完全工作
- 刽子手游戏的验证问题
- 比较常量字符* 与字符(无法使用 strcmp 进行比较)刽子手游戏
- 为什么我的代码只检查我的刽子手游戏(C ++)中单词的第一个字母
- C++ 刽子手游戏,如何让"right"信粘住
- 对计算我的刽子手游戏分数背后的逻辑有问题
- C++简单的刽子手游戏
- 如何在 c++ 中使用 "int promptYN(string reply)" 结束刽子手游戏
- C++ 刽子手游戏
- C++ 重新启动我的刽子手游戏时,我的分数以及我不想要的玩家信息被删除
- 刽子手游戏 - 同时循环与条件"contained in an array"