基于文本的游戏非分支if语句
Text-based Game non branching if statements
我正在用C++开发一款基于文本的游戏。我想知道,如果不嵌套这么多if语句,如何处理不同的事件。我所说的一个例子是…
if ( userChoice == 1 )
{
//do something
cout << "Pick 1 , 2, or 3" << endl;
if ( userChoice == 1 )
{
cout << "pick 1, 2, 3/ some storry line" << endl;
if ( userChoice == 3 )
{
cout << " storyline...123...." << endl;
if ( userChoice == 2 )
{
//etc
}
}
}
}
如果不分支if语句,我该如何实现这个概念?
您可以通过使用数据驱动的设计来消除分支if语句。
在底部有一个循环,用于检查输入,并使用当前上下文评估输入,然后更改上下文。
每一个if语句都变成了一个"场景"(或者房间或状态,选择一个对你正在编写的程序有意义的名称)。每个场景都有一个id、一个描述、一组有效的输入和结果为的场景编号
您有一个表示结束的场景。
你有一个循环:
loop until current scene is the end
Print current scene description
Ask for input
Evaluate input
Evaluate input根据当前场景的有效输入检查输入,如果有效,则将当前场景设置为指定场景。
程序将当前场景初始化为第一个场景,然后开始循环。
因此,对于你的例子(显然是不完整的,但应该给你所需要的数据的想法),你会有这样的场景:
id: 1
description: "Pick 1 , 2, or 3"
inputs: "1" => 2, "2" =>, "3" =>
id: 2
description: "pick 1, 2, 3/ some storry line"
inputs: "1" =>, "2" =>, "3" => 3
id: 3
description: " storyline...123...."
inputs: "1" =>, "2" =>, "3" =>
通常数据来自文件。
以下是一个示例(尚未编译或调试):
struct Scene
{
Scene(int id_, int description_)
: id(id_)
, description(description_)
{
}
int id;
std::string description;
std::map<std::string, int> inputToNextScene;
};
void main(int, char **)
{
std::map<int, Scene> scenes;
int ids = [1,2,3];
std::string descriptions = ["first", "second", "third"];
int nextScenes [3][3] = [ [1, 2, 3], [1, 3, 2], [1, 2, 0]];
std::string input[3] = ["1", "2", "3"];
for (int i = 0; i != 3; ++i)
{
scenes[ ids[i] ] = Scene(ids[i], descriptions);
Scene& scene = scenes.at(ids[i]);
for (int j = 0; j != 3; ++j)
{
scene.inputToNextScene[ input[j] ] = nextScenes[i][j];
}
}
int currentScene = 1;
std::string input;
while (currentScene != 0)
{
Scene& scene = scenes.at(currentScene);
std::cout << scene.description;
//Maybe put in a prompt and check currentscene against previous before printing description
std::cin >> input;
if (scene.inputToNextScene.find(input) != scene.inputToNextScene.end())
{
currentScene = scene.inputToNextScene.at(input);
}
}
cout << "The end!";
}
看看这个问题和我的答案。
简称:
对每个"分支"使用单独的函数或对象。使用std::map
而不是"if/else"。将用户输入id映射到处理它们的函数。
我会为每个选项制作一个单独的函数:
void doStoryLine1() {
cout << "Options: ..." << endl;
if(userChoice == 1)
fightDragon();
else if(userChoice == 2)
fightOrcs();
else if(userChoice == 3)
marryPrincess();
else
/* invalid option? */
}
void doStoryLine2() {
...
}
void selectStoryLine() {
cout << "Options: ..." << endl;
if(userChoice == 1)
doStoryLine1();
else if(userChoice == 2)
doStoryLine2();
else if(userChoice == 3)
doStoryLine3();
else
/* invalid option? */
}
您描述的问题似乎是异步编程中的常见问题(例如Node JS)。
您可以使用控制流编程来打破这些循环,例如序列/瀑布和承诺。
注意:我以前没有使用过这个标题,但它的工作方式应该类似于JS中的期货:期货与承诺
JS中的代码(您可以在C++中获得想法)
/** Sequence **/
new Sequence()
.then(function(next, arg1, arg2) {
// stage 1...
next(arg1, arg2);
})
.then(function(next) {
// stage 2...
next();
});
/** Promise **/
var a = Promise();
function stage1() {
// capture input 1...
a.fulfill(input1);
}();
a.when(function(err, data) {
console.log(data);
});
相关文章:
- IPC使用多个管道和分支进程来运行Python程序
- 如何删除peer if else分支中的冗长句子
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 在C++游戏中与库存系统作斗争
- 换位表导致测试失败(但在游戏中运行良好)
- 如何正确地将分支添加到已存在的树中
- Craps游戏问题,忽略if语句
- 如何将分支添加到已存在的TTree:ROOT
- 矢量下标超出SFML游戏中的范围
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 游戏体验和升级
- 如何删除 LLVM 中的不规则分支?
- 我的代码运行良好,但在游戏循环中中断
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 我可以使用任何好的逻辑来阻止计算机将 O 放在井字游戏中的现有 X 上
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- 在猜谜游戏 c++ 中不计算尝试次数
- 基于文本的游戏非分支if语句