基于文本的游戏非分支if语句

Text-based Game non branching if statements

本文关键字:游戏 分支 if 语句 于文本 文本      更新时间:2023-10-16

我正在用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);
});