c++ - 我需要使一个 void 函数循环返回而不使其崩溃或跳过
问题描述
当我除数字之外的任何其他按钮时,如何使我的选择不会导致递归?
我采取了一种俗气的方式来做这件事,不审查具体的选择,把它留给 else 声明。
你们能给我一个例子来让它工作吗?
在 void 函数的最后一条语句中,else 导致返回函数,它只是一起跳过函数,我不知道如何让它回到选择。
我尝试添加一个 if(!cin) 来审查这个过程,但这只会让事情变得更糟。
void forest() {
cout << " ,@@@@@@@,\n";
cout << " ,,,. ,@@@@@@/@@, .oo8888o.\n";
cout << " ,&%%&%&&%,@@@@@/@@@@@@,8888 8/8o" << endl;
cout << " ,%&%&&%&&%,@@@ @@@/@@@88 88888/88'\n";
cout << " %&&%&%&/%&&%@@ @@/ /@@@88888 88888'\n";
cout << " %&&%/ %&%%&&@@ V /@@' `88 8 `/88'\n";
cout << " `&% ` /%&' |.| '|8'\n";
cout << " |o| | | | |\n";
cout << " |.| | | | |\n";
cout << " / ._ /_/__ / , _/ __ /. _/__/_\n";
int forChoice = 0;
int goFurther = 0;
cout << "Choose the following paths\n";
cout << "1. Go north.\n2. Go south.\n3. Go east.\nPress any to go to where the Goddess pointed.\n";
cin >> forChoice;
cout << endl;
if (forChoice == 1) {
cout << "I find the forrest to be denser, and I hear loud rustling noises in the dark.\n";
cout << "I decide to turn back.\n";
forest();
} else if (forChoice == 2) {
cout << "I appear to be heading towards a cliff.\n";
cout << "The moon lights the whole landscape and the spectacular view of the terrain is something to behold.\n";
cout << "There is nothing left to do for me, but to turn back.\n";
forest();
} else if (forChoice == 3) {
cout << "I head east to see if there is anything to be found in that direction.\n";
cout << "I hear wolves howling in the distance.\n";
cout << "Do I continue, or turn back?\n1. Continue or press any other key to turn back\n";
cin >> goFurther;
if (goFurther == 1) {
cout << "I hear a rustling noise as I venture on. A wolf lunges form behind me and bites my thigh.\n";
cout << "I am now immobilized, and bleeding, as I see my vision fade away.\n";
die();
} else {
cout << "I decided to turn back to evade uncertain dangers\n";
forest();
}
}
}
int main() {
int explore = 0;
cout << "I have the option to explore the surrounding area. Do I explore or head straight to my task?\n";
cout << "1. Explore OR Press any to go to task.\n";
cin >> explore;
if (explore == 1) {
forest();
}
return 0;
}
解决方案
在您似乎拥有的 C++ 知识水平上,解决此任务非常困难,但无论如何我都会尝试。
尝试首先专注于最简单的游戏逻辑,并从极简游戏开始。
- 我看到(猜想)您打算处理这些组件:
- 场景(带描述)
- 选项(在哪里做什么)
- 玩家(在给定时间在一个场景中)
- 玩家应该能够在场景之间移动
- 显示您所在场景的简短描述(例如
Forrest.
:) - 显示特定场景的选项,
for
循环打印(自动添加键输入) - 想象一种退出游戏的方法(可能通过键入
"x"
),将其添加到每个选项列表中 - 获取用户输入(如果您使用
std::string
变量来读取、比较"1"
等"2"
)
要管理游戏,您应该运行一个简单的循环来显示当前场景并获取用户输入。在此之前,您必须通过使用场景变量将它们与选项连接起来来构建游戏结构。
这是一个示例,这种简单的设置看起来如何希望使事情更清楚一些。请注意,我使用struct
而不是class
,并以use namespace std;
两者开头的代码使代码保持较小,但对于严肃的程序来说被认为是不好的风格。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Scene;
struct Option
{
Option(const string& t, Scene* s): text(t), scene(s) {}
string text;
Scene* scene;
};
struct Scene
{
Scene(const string& t): text(t) {}
string text;
vector<Option> options;
};
struct Player
{
Player(Scene* s): situation(s) {}
Scene* situation;
// the actual game loop
void enjoy()
{
while (situation) {
const vector<Option>& options = situation->options;
cout << "you are here: " << situation->text << endl;
for (size_t i=0; i<options.size(); ++i) {
cout << i+1 << ": " << options[i].text << endl;
}
cout << "x: to exit game" << endl;
string choice;
cin >> choice;
if (choice == "x") {
return;
} else {
int opt = atoi(choice.c_str());
if ((0 < opt) && (opt <= int(options.size()))) {
situation = options[opt-1].scene;
} else {
cout << "still ";
}
}
}
}
};
int main()
{
// create some scenes
Scene forest("forest");
Scene field("field");
Scene hill("hill");
// add options to scenes (and link scenes):
forest.options.push_back(Option("Go to field", &field));
forest.options.push_back(Option("Go to hill", &hill));
field.options.push_back(Option("Go to forest", &forest));
field.options.push_back(Option("Go to hill", &hill));
hill.options.push_back(Option("Go to forest", &forest));
// add player, placed in one scene
Player player(&forest);
// actually run the game
player.enjoy();
return 0;
}
推荐阅读
- sql - 用左连接计数
- r - 基于 str_detect 使用 dplyr 的 mutate
- r - 如何修复错误消息“必须从色调调色板中请求至少一种颜色”?
- python - apache+mod-wsgi:ImportError: No module named site
- swiftui - 如何使用 NavigationDestinationLink 转到另一个视图?
- javascript - MediaWiki 的 PHP 脚注机制的 JavaScript 替代方案
- countdown - 创建倒数计时器js
- c++ - c++ unordered_set的迭代器begin()是否总是指向同一个元素
- javascript - 如何通过 javascript 或 jq 将标题和正文发布到 URL?
- python - 如何生成BOM结构?