首页 > 解决方案 > 如何简化选择您自己的冒险游戏的代码?

问题描述

我受到“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++oopbandersnatch

解决方案


推荐阅读