C++ 我需要做一个void函数循环,不让它崩溃或跳过
当我使用除数字之外的任何其他按钮时,如何使我的选择不会导致递归 我采取了一种粗俗的方式,不审查具体的选择,而把它留给else声明 你们能给我举个例子吗 在void函数的最后一个语句中,else导致返回函数,它只是一起跳过函数,我不知道如何使它返回到选择 我试着添加一个if(!cin)来检查这个过程,但它只会让事情变得更糟C++ 我需要做一个void函数循环,不让它崩溃或跳过,c++,C++,当我使用除数字之外的任何其他按钮时,如何使我的选择不会导致递归 我采取了一种粗俗的方式,不审查具体的选择,而把它留给else声明 你们能给我举个例子吗 在void函数的最后一个语句中,else导致返回函数,它只是一起跳过函数,我不知道如何使它返回到选择 我试着添加一个if(!cin)来检查这个过程,但它只会让事情变得更糟 void forest() { cout << " ,@@@@@@@,\n"; cout << "
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;
}
void林(){
在你似乎拥有的C++知识水平上,解决这个任务很困难,但无论如何我还是要尝试。
试着先把注意力集中在最简单的游戏逻辑上,然后从最简单的游戏开始
- 我看到(猜测)您打算处理这些组件:
- 场景(附说明)
- 选项(在哪里做什么)
- 玩家(在给定时间出现在一个场景中)
- 玩家应该能够在场景之间移动
- 显示您所在场景的简短描述(例如:
Forrest.
)
- 显示特定于场景的选项,在循环中打印它们(自动添加按键输入)
- 设想一种退出游戏的方法(可能通过键入
“x”
),将其添加到每个选项列表中
- 获取用户输入(如果使用
std::string
变量进行读取,请与“1”
、“2”
等进行比较)
要管理游戏,您应该运行一个简单的循环,显示当前场景并获取用户输入。在此之前,您必须使用场景变量将其与选项连接,以构建游戏结构
下面是一个例子,这样一个简单的设置看起来像是希望让事情变得更清楚一点。请注意,我使用了struct
而不是class
,并用use namespace std;启动了代码use namespace std;
两者都保持了代码的小规模,但对于严肃的程序来说被认为是不好的风格
#包括
#包括
#包括
使用名称空间std;
结构场景;
结构选项
{
选项(常量字符串&t,场景*s):文本(t),场景(s){
字符串文本;
场景*场景;
};
结构场景
{
场景(常量字符串&t):文本(t){}
字符串文本;
矢量选项;
};
结构播放器
{
玩家(场景*s):情况{}
场景*情况;
//实际的游戏循环
享受
{
虽然(情况){
常量向量和选项=情况->选项;
你真的可以种植一个漂亮的森林吗:)为了找到答案,试着更好地区分不同的关注点,即绘图、显示选项、获取输入。你能提供给定的输入和输出吗?我之前使用了一个if语句,比如if(!cin或forChoice!=1或forChoice!=3等等),但当我输入字母g或任何其他字符时,程序会执行无限循环。@newtocode为了避免无限循环问题,您必须将forChoice
的类型更改为string,因为int
变量只能保存数字
#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;
}