如何在 c++ 中使用 "int promptYN(string reply)" 结束刽子手游戏
How to end hangman game using "int promptYN(string reply)" in c++
在我的刽子手游戏中,我无法结束游戏。使用我定义的头文件...
#define PLAY 1
#define STOP 0
#define ERROR -1
。然后我定义了函数
int promptYN(string reply)
{
reply = strToUpper(reply);
if (reply == "YES", "OK", "SURE", "Y")
return PLAY;
else if (reply == "NO", "QUIT", "STOP", "TERMINATE", "N", "Q")
return STOP;
else
return ERROR;
}
。现在,无论用户键入什么,该函数都已在main中调用,即使我以"NO","QUIT","STOP","TERMINATE","N","Q"响应。
这是我用来尝试玩游戏的while循环,我只需要帮助弄清楚用户何时响应"Y",然后游戏播放,"N"然后游戏停止,或ERROR和游戏重置为再次提问。谢谢!
cout << "Do you want to play hangman? (y or n): ";
cin >> userReply;
gameStatus = promptYN(userReply);
while (gameStatus != STOP)
{
if (gameStatus == PLAY)
{
chances = 0;
cout << "Let's Playnn";
guessWord = getNextWord(); // Function call (randword.h)
guessWord = strToUpper(guessWord); // Function call (MyFuncts.h)
cout << "nWord to Guess: " << guessWord << endl;
cout << endl << h1;
while (wrong != 6) // Function to find out which hangman board to print to user
{
cout << "nEnter a letter to guess: ";
cin >> gLetter;
gLetter = toupper(gLetter);
cout << "You entered: " << gLetter << endl << endl;
cout << gLetter << " is NOT in the word to guess.";
wrong++;
if (wrong == 0)
cout << endl << h1 << endl;
if (wrong == 1)
cout << endl << h2 << endl;
if (wrong == 2)
cout << endl << h3 << endl;
if (wrong == 3)
cout << endl << h4 << endl;
if (wrong == 4)
cout << endl << h5 << endl;
if (wrong == 5)
cout << endl << h6 << endl;
if (wrong == 6)
cout << endl << h7 << endl;
}
}
else if (gameStatus == STOP)
{
cout << "Goodbyen";
}
else
{
cout << "Error - please enter (y or n)n";
}
cout << "Do you want to play hangman? (y or n): ";
cin >> userReply;
gameStatus = promptYN(userReply);
}
这一行:
if (reply == "YES", "OK", "SURE", "Y")
不会将reply
与这些字符串中的每一个进行比较。 为此,您必须单独比较字符串:
int promptYN(string reply)
{
if (reply == "YES" || reply =="OK" || reply == "SURE" || reply == "Y")
return PLAY;
//...
}
这将有效,但是在C++中设置的更好方法是使用类似std::set<std::string>
的东西并查看该值是否存在:
#include <set>
//...
int promptYN(string reply)
{
static std::set<std::string> OKResponse = {"YES", "OK", "SURE", "Y"};
static std::set<std::string> StopResponse = {"NO", "QUIT", "STOP",
"TERMINATE", "N", "Q"};
if ( OKResponse.count(reply) == 1 )
return PLAY;
else
if (StopResponse.count(reply) == 1)
return STOP;
return ERROR;
}
如果集合中存在该值,则 std::set::count 将返回 1
,否则返回 0。
要向"ok"或"stop"添加另一个响应,只需向每个set
值添加更多字符串。
现场示例
注意:确保您有一个兼容的C++11
编译器,用于设置std::set
,并确保static
以线程安全的方式工作。
相关文章:
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- 为std::string的某个索引赋值
- std中有类似find_last_of的函数,而string中没有
- 使用 std::string () const 函数启动线程或未来
- 使用char类型将decimal转换为string,将string转换为decimal
- 迭代和比较映射<字符串、矢量<string>> c++ 中的值
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 将向量解析<string>为字符串
- 'string.assign(string.data(), 5)' 是明确定义的还是 UB?
- 如何更改大小(std::string)
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- std::string 的对象真的可以移动吗?
- 与'operator='不匹配(操作数类型'String'且"void")
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 无法从 std::string 中提取C++ Unicode 符号
- std::string 构造函数如何处理固定大小的 char[]?
- <string> 使用 for 循环写入向量
- 如何在 c++ 中使用 "int promptYN(string reply)" 结束刽子手游戏