【问题标题】:Text-based Game non branching if statements基于文本的游戏非分支 if 语句
【发布时间】:2013-07-12 19:43:02
【问题描述】:

我正在使用 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 语句的情况下实现这个概念?

【问题讨论】:

  • 好吧,为什么你可能需要两次(userChoice == 1),第二次查看 switch 语句
  • 将逻辑上分离的逻辑和动作组织成不同的功能。当然,您需要在其中包含条件,但是一旦您尝试过,您就会立即看到这样做的好处。
  • 您还可以研究 TADS 和 Inform,它们是有助于编写此类游戏的语言。

标签: c++ if-statement


【解决方案1】:

您可以通过使用数据驱动设计摆脱分支 if 语句。

在基础上,您有一个循环来检查输入并使用当前上下文评估输入,然后更改上下文。

这些 if 语句中的每一个都成为“场景”(或房间或状态,选择一个对您正在编写的程序有意义的名称)。每个场景都有一个 id、一个描述以及一组有效的输入和结果的场景编号。

你有一个代表结束的场景。

你有一个循环:

loop until current scene is the end
 Print current scene description
 Ask for 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!";
}

【讨论】:

  • 我有点困惑。您能否编辑并给我一个代码示例。
【解决方案2】:

请参阅 this questionmy answer

短版:

为每个“分支”使用单独的函数或对象。 使用std::map 代替“if/else”。将用户输入 ID 映射到处理它们的函数。

【讨论】:

    【解决方案3】:

    您描述的问题,似乎是异步编程(例如 Node JS)中的常见问题。

    您可以使用控制流编程来打破这些循环,例如序列/瀑布承诺

    注意:我之前没有使用过这个标头,但它应该类似于 JS 中的期货:Futures vs. Promises

    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);
    
    });
    

    【讨论】:

    • -1 Futures 是在 C++ 中解决这个问题的糟糕选择。此外,C++ 未来还没有.then(目前),这使得基本思想几乎无法使用。
    • 也许您可以进一步详细说明? IMO 这不值得投反对票!
    • C++ 中的期货不允许分派。相反,您可以将它们视为将值从一个线程传递到另一个线程的一次性通道。在没有多个线程的情况下,您甚至可以将所有 future&lt;T&gt;/promise&lt;T&gt; 对替换为 T 类型的变量(只要您不关心 only-one-write-allowed 属性)。建议的.then 样式分派可以在 C++ 中实现,但是 future 是错误的工具,尤其是因为 future 的主要设计目标是充当多线程原语。
    【解决方案4】:

    我会为每个选项创建一个单独的函数:

    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? */
    }
    

    【讨论】:

    • 我可以,但这会做同样的事情,但是使用函数,因为使用函数它会再次分支到 if 语句中,或者我必须创建许多函数。
    • 你最好做很多功能。您更有可能使您的代码美观且模块化,并使其更易于维护。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    • 2015-08-13
    • 2020-06-16
    • 2013-07-08
    • 2015-09-10
    相关资源
    最近更新 更多