c++ - 如何简化选择您自己的冒险游戏的代码?
问题描述
我受到“Black Mirror Bandersnatch”的启发,选择了自己的冒险情节,并决定自己尝试一下。到目前为止,我已经创建了一个功能性的骨架作为项目其余部分的模板。我想看看是否有任何优秀的程序员有关于简化 switch 语句的建议,这些语句随着你深入游戏而呈指数级增长。这是我到目前为止...
#include <iostream>
#include <Player.h>
using namespace std;
int main()
{
Player player;
cout << "Health: " << player.GetHealth() << endl;
int choice = 0;
cout << "You wake up on Jupiter. There is a tunnel with a mysterious blue light emitting from it in the distance. You look to the right and see a futuristic factory with strange crafts flying from it. You wonder how you got to Jupiter...\n" << endl;
cout << "Go to the tunnel(1) \t Go to the factory(2)\n>>> ";
cin >> choice;
switch(choice)
{
case 1:
cout << "You go towards the tunnel and get attacked by a flying saucer...\n";
player.DecreaseHealth();
cout << "Health: " << player.GetHealth() << "\n";
cout << "You see a secret nazi facility. Go and check it out(1). In the distance you see a TR3-B craft. Do you want to fly it? (2)\n";
//choice = 0;
cout << "Go to the secret nazi facility(1) \t Get inside TR3-B(2)\n>>> ";
cin >> choice;
switch(choice)
{
case 1:
cout << "You go to secret nazi facility...\n";
player.DecreaseHealth();
cout << "Health: " << player.GetHealth() << "\n";
break;
case 2:
cout << "You get inside TR3B... \n";
break;
}
break;
case 2:
cout << "You go towards the factory...\n";
cout << "Health: " << player.GetHealth() << "\n";
cout << "Do you want to steal element 117(1) or grab TR3-B top secret documents(2)\n";
cin >> choice;
switch(choice)
{
case 1:
cout << "You steal element 117...\n";
player.DecreaseHealth();
cout << "Health: " << player.GetHealth() << "\n";
break;
case 2:
cout << "You grab documents... \n";
break;
}
break;
}
return 0;
}
如您所见,我只有 2 条绝对路径和 2 条自己的路径。每个案例将再添加 2 个案例。当我到达树的第三层时,总共需要添加 16 个案例。在第三级之后是 32-64-128 等等。提前非常感谢。
解决方案
推荐阅读
- c++ - 找到一种在 C++ 中使用条件提取浮点数的方法
- email - 电子邮件帐户未显示在 CPanel 中,但文件管理器中列出的文件夹
- matlab - 分类器产生连续值而不是不同的标签
- java - 关闭线程池是强制性的吗?
- java - How can I add a method or make this code so that I am not repeating myself so many times?
- asp.net - 如何在 asp.net c# 中创建基于令牌的登录表单
- python - Python:用#字符分割文本文件并总结总数
- javascript - Convert nested object to array of object
- java - I cannot print the user input from my arraylist
- c++ - 我在 switch case 中输入了一个字母,但它没有输出我输入的字母