c++ - String Game,创建一个特定的循环? C++

标签 c++ file-io

我是编程和 C++ 的新手,为了好玩,我开始制作一个字符串类型的小游戏,它在整个程序中为用户提供了两个选项,但在程序的最后部分,我无法让它输出一个独特的选项最终输入(makeCure)-我只想在最后输出而不是整个程序。希望我是有道理的:/。我试了又试,我试的越多,我创造的问题就越多。 Iv 在我的代码中显示如下,我确定问题所在。任何建议将不胜感激。

#include<iostream>
#include<string>

using std::string;

bool intro(void);
void room(bool enemy, bool data, bool cure, string description);

//player stats
string Name = "";

//enemy states
string enemyName = "";

//data stats
string dataName = "";

//Cure - Option in room7 only
string makeCure = "";

//room descriptions(string constructs)

const string room1 = "You enter the first town of infected Zombies.";
const string room2 = "You are overwelmed by Zombies, and plunder into the sewers to escape.";
const string room3 = "You make your way to safety and find yourself in the Central Town Hall.";
const string room4 = "You decide to venture into the local forest to find the finalingrediants";
const string room5 = "You venture further for the final ingrediant, into a nearby Cave.";
const string room6 = "Its time for you to face the Zombie General!!!";
const string room7 = "You work day and Night in the Labs to make the Cure.";


int main(void)
{
    if(intro())
        return 0;
    dataName = "First Ingrediant- Zombie Rags with infected DNA";
    enemyName = "Zombie Soldior";
    room(true, true, false, room1);

    enemyName = "Massive Zombie Rat";
    room(true, false, false, room2);

    dataName = "Seconed Ingrediant- StemCells";
    enemyName = "Mutated Scientists";
    room(true, true, false, room3);

    dataName = "Third Magic-Mushrooms";
    room(false, true, false, room4);

    dataName = "Fourth Final Ingrediant - Coffee Beans";
    enemyName = "Commander Zombie";
    room(true, true, false, room5);

    enemyName = "Zombie General";
    room(false, true, false, room6);
    return 0;


    makeCure = "Elixier to Save the World";
    room(false, false, true, room7);
    return 0;
}

bool intro(void)
{
    using std::cout;
    using std::cin;

    cout << "Brave Soul!!! What is your name?\n";
    cin >> Name;
    cout << "Ahh... " << Name << " You say.." << "How about Zombie Slayer?.. Good, glad we agree!\n";

    cout << "Humanity is in need of your Help, "
        << "The world is being infected by the\n"
        << "ZD1678 ZOMBIE VIRUS \n"
        << "And we need to send you to Cape Town to stop the central spread.\n"
        << "Your task will be tough, but we know you can do it \n"
        << "Will you accept the challenge?\n\n";

    cout << "1)Yes. \n"
        << "2)No. \n\n";
    int response;
    cin >> response;
    return !(response ==1);
}

void room(bool enemy, bool data, bool cure, string description)
{
    using std::cout;
    using std:: cin;

    while(true)
    {
        cout << description.c_str() << "\n\n";
        int response = 0;
        do
        {
            cout << "Shall Our Hero continue his Quest?\n";
            if(enemy)
                cout << "1) Attack the "             
                << enemyName.c_str() << "\n";
            else if(!enemy)
                cout << "1) venture further...."; 

            if(data)
                cout << "2)Pick up the "    
                << dataName.c_str() << "\n";
            cin >> response;

            /* Trying to create the last if that only kicks in at room7( string makeCure )
            * that displays the option to make the cure
            * This is where my Problem is.
            * Iv tried anouther if
            * and else
            * and while and nothing works, its just messes up everything..
            * badly
            */

        } while(response < 1 || response > 2);

        switch(response)
        {
        case 1:
            if(enemy)
            {
                enemy = !enemy;
                cout << "You slay the deadly "
                    << enemyName.c_str() << "\n";
            }
            else if(!enemy)
                return;
            break;
        case 2:
            data = !data;
            cout << "You pick up the "
                <<  dataName.c_str() << "\n";
            break;
        }
    }
}

最佳答案

您可能想要做的是在每次写出列表并将其呈现给用户时动态生成一个可能事件的列表,然后您可以将响应与列表匹配以获得用户想要执行的操作。像这样:

enum EventType
{
    ET_Enemy,
    ET_Item,
    ET_Cure,
    ET_Continue,
    ET_MAX
};

void room(bool enemy, bool data, bool cure, string description)
{
 using std::cout;
 using std:: cin;

    int currentEventChoices[ET_MAX];
    int numEventChoices;

    while(true)
    {
        cout << description.c_str() << "\n\n";


        int response = 0;
        do
        {
            numEventChoices = 0;

            cout << "Shall Our Hero continue his Quest?\n";

            if(enemy)
            {
                cout << (numEventChoices+1) << ") Attack the "             
                        << enemyName.c_str() << "\n";
                currentEventChoices[numEventChoices] = ET_Enemy;
                numEventChoices++;
            }
            if(data)
            {
                 cout << (numEventChoices+1) << ") Pick up the "    
                        << dataName.c_str() << "\n";
                currentEventChoices[numEventChoices] = ET_Item;
                numEventChoices++;
            }
            if(cure)
            {
                cout << (numEventChoices+1) << ") cure related string "
                        << makeCure.c_str() << "\n";
                currentEventChoices[numEventChoices] = ET_Cure;
                numEventChoices++;
            }

            cout << (numEventChoices+1) << ") venture further....\n"; // note if this is only meant to be an option if there is no enemy, put it in an else after the if(enemy)
            numEventChoices++;

                cin >> response;



        } while(response < 1 || response > numEventChoices);

        switch(currentEventChoices[response-1])
        {


            case ET_Enemy:
                enemy = !enemy;
                    cout << "You slay the deadly "
                            << enemyName.c_str() << "\n";
                break;
            case ET_Item:
                 data = !data;
                cout << "You pick up the "
                        <<  dataName.c_str() << "\n";
                break;
            case ET_Cure:
                //do cure stuff
                break;
            case ET_Continue:
                return;
        }
   }
}

你遇到的问题是,每次你想将选项号与事件匹配时,只使用非常静态的 if/else 语句,它变得非常复杂和困惑,当只有有无敌人、无数据4种情况。但是现在您要添加另一个具有治愈功能的分支,这样做真的很复杂。

关于c++ - String Game,创建一个特定的循环? C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16889755/

相关文章:

r - 如何转义R字符串中的反斜杠

java - Java中如何从jar中读取文件?

c++ - 在不重载 RAM 的情况下写入巨大的 txt 文件

c++ - 如何使用 for 循环将元素添加到 vec 或 rowvec?

c++ - C/C++函数/方法修饰

c++ - post请求中的二进制数据

c++ - 似乎无法正确地将对象添加到 C++ 中的数组

python - 为什么我会得到 "TypeError: open() missing required argument ' 标志' (pos 2 )"or "TypeError : an integer is required (got type str)"when opening a file?

bash - 我是否需要生成第二个文件来对文件进行排序?

c++ - 如何将无符号整数分配给 C++ 中的枚举?