0

我是编程和 C++ 的新手,我开始制作一个有趣的字符串类型游戏,它在整个程序中为用户提供了两个选项,但在程序的最后部分,我无法让它为最终输入输出一个独特的选项(makeCure) - 我只想在最后输出而不是通过程序。希望我说得通:/ .Iv 尝试了又尝试了,我尝试的越多,我创造的问题就越多。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;
        }
    }
}
4

2 回答 2

1

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

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 种情况时很好有没有敌人,有没有数据。但是现在您正在添加另一个具有治愈功能的分支,这样做真的很复杂。

于 2013-06-03T04:17:33.623 回答
0

有点难以理解你需要什么,所以如果这不是你想要的,请告诉我。

一致地使用大括号和缩进可以真正帮助解决这个问题:

do {
    cout << "Shall Our Hero continue his Quest?\n"; 
    if (enemy) {
        cout << "1) Attack the " << enemyName << "\n";
    } else {
        cout << "1) venture further...."; 
    }
    if (data) {
        cout << "2) Pick up the " << dataName << "\n";
    }
    if (cure) {
        cout << "2) take the " << makeCure << "\n";
    }
    cin >> response;
} while (response < 1 || response > 2);

并在开关部分修复“案例2”:

 case 2:
        if (data) {
            data = false;
            cout << "You pick up the " <<  dataName << "\n";
        } else if (cure) {
            // fill in ...
        }
        break;

笔记:

  1. 您可以使用endl(from std) 代替 '\n'。cout << "hello" << endl;
  2. 您可以将许多全局变量作为参数传递,因此您不需要它们是全局的(一般来说,全局是不好的)。
  3. 你的大部分游戏都可以被压缩成数组和结构——被“数据驱动”和“表驱动”。我不知道你是否已经到达那里,但你可以尝试识别这些部分。
  4. if(enemy) ... else if(!enemy)你不需要这个!enemy部分。它是由 暗示的else
于 2013-06-03T04:14:17.673 回答